Skip to content

Commit 4b14997

Browse files
committed
ICL: Additional check added.
1 parent 6c90d82 commit 4b14997

File tree

1 file changed

+52
-50
lines changed

1 file changed

+52
-50
lines changed

integrations/guzzle.php

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,67 @@
44
use Psr\Http\Message\RequestInterface;
55
use Psr\Log\LoggerInterface;
66

7-
function iclogger_guzzle_middleware(LoggerInterface $log, $type = 'raw')
8-
{
9-
return function (callable $handler) use ($log, $type) {
10-
return function (RequestInterface $request, array $options) use ($handler, $log, $type) {
11-
$method = (string) $request->getMethod();
12-
$uri = (string) $request->getUri();
13-
$body = (string) $request->getBody();
14-
15-
if (empty($body)) {
16-
$message = "[{$method}] Calling `{$uri}`.";
17-
$context = [];
18-
} else {
19-
$message = "[{$method}] Calling `{$uri}` with body:";
20-
switch ($type) {
21-
case 'json':
22-
$context = json_decode($body, true);
23-
break;
24-
25-
case 'raw':
26-
default:
27-
$message .= "\n{$body}";
28-
$context = [];
29-
break;
30-
}
31-
}
32-
$log->info($message, $context);
33-
34-
return $handler($request, $options)->then(
35-
function ($response) use ($log, $type) {
36-
$body = (string) $response->getBody();
37-
$code = $response->getStatusCode();
38-
39-
$message = "[{$code}] Response:";
7+
if (!function_exists('iclogger_guzzle_middleware')) {
8+
function iclogger_guzzle_middleware(LoggerInterface $log, $type = 'raw')
9+
{
10+
return function (callable $handler) use ($log, $type) {
11+
return function (RequestInterface $request, array $options) use ($handler, $log, $type) {
12+
$method = (string) $request->getMethod();
13+
$uri = (string) $request->getUri();
14+
$body = (string) $request->getBody();
15+
16+
if (empty($body)) {
17+
$message = "[{$method}] Calling `{$uri}`.";
18+
$context = [];
19+
} else {
20+
$message = "[{$method}] Calling `{$uri}` with body:";
4021
switch ($type) {
4122
case 'json':
42-
$context = is_json($body, true);
43-
if (empty($context)) {
44-
throw new RuntimeException('Bad response, json expected.', ['response' => $body]);
45-
}
23+
$context = json_decode($body, true);
4624
break;
47-
25+
4826
case 'raw':
4927
default:
5028
$message .= "\n{$body}";
5129
$context = [];
5230
break;
5331
}
54-
if (!empty($context)) {
55-
$response->iclParsedBody = $context;
56-
}
57-
58-
$log->info($message, $context);
59-
60-
return $response;
61-
},
62-
function ($reason) {
63-
return \GuzzleHttp\Promise\rejection_for($reason);
6432
}
65-
);
33+
$log->info($message, $context);
34+
35+
return $handler($request, $options)->then(
36+
function ($response) use ($log, $type) {
37+
$body = (string) $response->getBody();
38+
$code = $response->getStatusCode();
39+
40+
$message = "[{$code}] Response:";
41+
switch ($type) {
42+
case 'json':
43+
$context = is_json($body, true);
44+
if (empty($context)) {
45+
throw new RuntimeException('Bad response, json expected.', ['response' => $body]);
46+
}
47+
break;
48+
49+
case 'raw':
50+
default:
51+
$message .= "\n{$body}";
52+
$context = [];
53+
break;
54+
}
55+
if (!empty($context)) {
56+
$response->iclParsedBody = $context;
57+
}
58+
59+
$log->info($message, $context);
60+
61+
return $response;
62+
},
63+
function ($reason) {
64+
return \GuzzleHttp\Promise\rejection_for($reason);
65+
}
66+
);
67+
};
6668
};
67-
};
69+
}
6870
}

0 commit comments

Comments
 (0)