Skip to content

Commit fa81533

Browse files
committed
Added response and request logging
1 parent aeaeef8 commit fa81533

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

src/OpenAPI/Client/Rest/BaseAbstract.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ public function __construct(
6060
throw new InvalidArgumentException('Param $apiName is required!');
6161
}
6262

63-
$this->api = $this->createApi($apiName, $lifeCycleToken, $config);
6463
$this->dataTransfer = $dataTransfer;
6564
$this->logger = $logger;
65+
$this->api = $this->createApi($apiName, $lifeCycleToken, $config);
6666
}
6767

6868
/**
@@ -77,7 +77,13 @@ protected function createApi(
7777
string $lifeCycleToken,
7878
?ConfigurationInterface $config = null
7979
): ApiInterface {
80-
return new $apiName(new Client(['headers' => ['LifeCycleToken' => $lifeCycleToken]]), $config);
80+
$api = new $apiName(new Client(['headers' => ['LifeCycleToken' => $lifeCycleToken]]), $config);
81+
82+
if (method_exists($api, 'setLogger')) {
83+
$api->setLogger($this->logger);
84+
}
85+
86+
return $api;
8187
}
8288

8389
/**

template/client/api.mustache

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use OpenAPI\Client\ApiException;
3131
use {{invokerPackage}}\Configuration;
3232
use OpenAPI\Client\HeaderSelector;
3333
use OpenAPI\Client\ObjectSerializer;
34+
use Psr\Log\LoggerInterface;
3435

3536
/**
3637
* {{classname}} Class Doc Comment
@@ -63,21 +64,34 @@ use OpenAPI\Client\ObjectSerializer;
6364
protected $hostIndex;
6465
6566
/**
66-
* @param ClientInterface $client
67-
* @param Configuration $config
68-
* @param HeaderSelector $selector
69-
* @param int $hostIndex (Optional) host index to select the list of hosts if defined in the OpenAPI spec
67+
* @var LoggerInterface|null
68+
*/
69+
private $logger;
70+
71+
/**
72+
* @param ClientInterface|null $client
73+
* @param Configuration|null $config
74+
* @param HeaderSelector|null $selector
75+
* @param int $hostIndex (Optional) host index to select the list of hosts if defined in the OpenAPI spec
76+
* @param LoggerInterface|null $logger
7077
*/
7178
public function __construct(
7279
ClientInterface $client = null,
7380
Configuration $config = null,
7481
HeaderSelector $selector = null,
75-
$hostIndex = 0
82+
$hostIndex = 0,
83+
LoggerInterface $logger = null
7684
) {
7785
$this->client = $client ?: new Client();
7886
$this->config = $config ?: Configuration::getDefaultConfiguration();
7987
$this->headerSelector = $selector ?: new HeaderSelector();
8088
$this->hostIndex = $hostIndex;
89+
$this->logger = $logger;
90+
}
91+
92+
public function setLogger(LoggerInterface $logger): void
93+
{
94+
$this->logger = $logger;
8195
}
8296

8397
/**
@@ -189,15 +203,32 @@ use OpenAPI\Client\ObjectSerializer;
189203
{
190204
$request = $this->{{operationId}}Request({{^vendorExtensions.x-group-parameters}}{{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}$associative_array{{/vendorExtensions.x-group-parameters}});
191205

206+
$this->log('info', 'Openapi send request.', [
207+
'requestBody' => (string)$request->getBody(),
208+
'requestUri' => (string)$request->getUri()
209+
]);
210+
192211
try {
193212
$options = $this->createHttpClientOption();
194213
try {
195214
$response = $this->client->send($request, $options);
215+
216+
$this->log('info', 'Openapi response successfully received.', [
217+
'class' => self::class,
218+
'responseBody' => (string)$response->getBody(),
219+
'responseStatusCode' => $response->getStatusCode()
220+
]);
196221
} catch (RequestException $e) {
197222
if (!$e->hasResponse()) {
198223
throw $e;
199224
}
200225
$response = $e->getResponse();
226+
227+
$this->log('info', 'Openapi not 2xx response received.', [
228+
'class' => self::class,
229+
'responseBody' => (string)$response->getBody(),
230+
'responseStatusCode' => $response->getStatusCode()
231+
]);
201232
}
202233

203234
$statusCode = $response->getStatusCode();
@@ -667,5 +698,12 @@ use OpenAPI\Client\ObjectSerializer;
667698
];
668699
}
669700
}
701+
702+
protected function log(string $level, string $message, array $context): void
703+
{
704+
if ($this->logger) {
705+
$this->logger->log($level, $message, $context);
706+
}
707+
}
670708
}
671709
{{/operations}}

0 commit comments

Comments
 (0)