Skip to content

Commit 775b046

Browse files
committed
added toArray() method to convert JSON request/response data into an array for use in the logger's request, response contexts and request_body in info #1
1 parent 4101a1d commit 775b046

12 files changed

+257
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
- added `toArray()` method to convert JSON request/response data into an array for use in the logger's `request`, `response` contexts and `request_body` in info
6+
37
## 2.1.1 - 2024-11-21
48

59
- added PHP 8.4 support

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ $loggableHttpClient->setLogger(new \MyLogger());
3232

3333
$response = $loggableHttpClient->request('GET', 'https://example.com');
3434

35+
$response->getContent(); // standard response content as string
36+
$response->toArray(); // standard response content as array
37+
38+
$requestBody = $response->getInfo('request_body');
39+
if (isset($requestBody) && ($requestBody instanceof \Liborm85\LoggableHttpClient\Body\BodyInterface)) {
40+
$requestBody->getContent(); // request content body as string
41+
(string)$requestBody; // is Stringable, request content body as string
42+
$requestBody->toStream(); // request content body as PHP stream
43+
$requestBody->toArray(); // request content body as array (if is possible, otherwise null)
44+
}
45+
46+
$response->getInfo('response_time'); // the time when the response was received
47+
3548

3649
class MyLogger extends \Psr\Log\AbstractLogger
3750
{
@@ -42,6 +55,7 @@ class MyLogger extends \Psr\Log\AbstractLogger
4255
$context['request']->getContent(); // request content body as string
4356
(string)$context['request']; // is Stringable, request content body as string
4457
$context['request']->toStream(); // request content body as PHP stream
58+
$context['request']->toArray(); // request content body as array (if is possible, otherwise null)
4559
$context['request']->getHeadersAsString(); // request headers as string
4660
$context['request']->getHeaders(); // request headers as array (string[][])
4761
$context['request']->getRequestTime(); // request time as DateTimeInterface
@@ -53,6 +67,7 @@ class MyLogger extends \Psr\Log\AbstractLogger
5367
$context['response']->getContent(); // response content body as string
5468
(string)$context['response']; // is Stringable, response content body as string
5569
$context['response']->toStream(); // response content body as PHP stream
70+
$context['response']->toArray(); // response content body as array (if is possible, otherwise null)
5671
$context['response']->getHeadersAsString(); // response headers as string
5772
$context['response']->getHeaders(); // response headers as array (string[][])
5873
$context['response']->getResponseTime(); // response time as DateTimeInterface

src/Body/BodyInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ interface BodyInterface extends \Stringable
77

88
public function getContent(): ?string;
99

10+
public function toArray(): ?array;
11+
1012
/**
1113
* @return resource|null
1214
*/

src/Body/RequestBody.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,26 @@ public function getContent(): ?string
2323
return $this->getBodyAsString($this->body);
2424
}
2525

26+
public function toArray(): ?array
27+
{
28+
$content = $this->getContent();
29+
if ($content === null || $content === '') {
30+
return null;
31+
}
32+
33+
try {
34+
$content = json_decode($content, true, 512, \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR);
35+
} catch (\JsonException) {
36+
return null;
37+
}
38+
39+
if (!is_array($content)) {
40+
return null;
41+
}
42+
43+
return $content;
44+
}
45+
2646
/**
2747
* @return resource|null
2848
*/

src/Context/RequestContext.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,20 @@ public function getContent(): ?string
8888
}
8989
}
9090

91+
public function toArray(): ?array
92+
{
93+
$requestBody = $this->getRequestBody();
94+
if ($requestBody === null) {
95+
return null;
96+
}
97+
98+
try {
99+
return $requestBody->toArray();
100+
} catch (\Throwable) {
101+
return null;
102+
}
103+
}
104+
91105
/**
92106
* @return resource|null
93107
*/

src/Context/RequestContextInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public function getHeadersAsString(): ?string;
1717

1818
public function getContent(): ?string;
1919

20+
public function toArray(): ?array;
21+
2022
/**
2123
* @return resource|null
2224
*/

src/Context/ResponseContext.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ public function getContent(): ?string
6767
}
6868
}
6969

70+
public function toArray(): ?array
71+
{
72+
try {
73+
return $this->response->toArray(false);
74+
} catch (\Throwable) {
75+
return null;
76+
}
77+
}
78+
7079
public function __toString(): string
7180
{
7281
return $this->getContent() ?? '';

src/Context/ResponseContextInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public function getHeadersAsString(): ?string;
1515

1616
public function getContent(): ?string;
1717

18+
public function toArray(): ?array;
19+
1820
/**
1921
* @return resource|null
2022
*/

tests/AmpHttpClientTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ public function testGetContentStringBody(): void
6060
'abc' => 'def',
6161
'REQUEST_METHOD' => 'POST',
6262
],
63+
'response-content-toarray' => [
64+
'abc' => 'def',
65+
'REQUEST_METHOD' => 'POST',
66+
],
6367
],
6468
];
6569
$this->assertSameResponseContentLog($expected, $logger->logs);
@@ -101,6 +105,10 @@ public function testGetContentIterableYieldBody(): void
101105
'abc' => 'def',
102106
'REQUEST_METHOD' => 'POST',
103107
],
108+
'response-content-toarray' => [
109+
'abc' => 'def',
110+
'REQUEST_METHOD' => 'POST',
111+
],
104112
],
105113
];
106114
$this->assertSameResponseContentLog($expected, $logger->logs);
@@ -134,10 +142,16 @@ public function testGetContentJsonBody(): void
134142
[
135143
'message' => 'Response content: "200 http://127.0.0.1:8057/post"',
136144
'request-content' => '{"abc":"def"}',
145+
'request-content-json' => ['abc' => 'def'],
146+
'request-content-toarray' => ['abc' => 'def'],
137147
'response-content-json' => [
138148
'{"abc":"def"}' => '',
139149
'REQUEST_METHOD' => 'POST',
140150
],
151+
'response-content-toarray' => [
152+
'{"abc":"def"}' => '',
153+
'REQUEST_METHOD' => 'POST',
154+
],
141155
],
142156
];
143157

@@ -176,6 +190,10 @@ public function testToArray(): void
176190
'abc' => 'def',
177191
'REQUEST_METHOD' => 'POST',
178192
],
193+
'response-content-toarray' => [
194+
'abc' => 'def',
195+
'REQUEST_METHOD' => 'POST',
196+
],
179197
],
180198
];
181199
$this->assertSameResponseContentLog($expected, $logger->logs);
@@ -225,6 +243,10 @@ public function testStream(): void
225243
'abc' => 'def',
226244
'REQUEST_METHOD' => 'POST',
227245
],
246+
'response-content-toarray' => [
247+
'abc' => 'def',
248+
'REQUEST_METHOD' => 'POST',
249+
],
228250
],
229251
];
230252
$this->assertSameResponseContentLog($expected, $logger->logs);
@@ -267,6 +289,10 @@ public function testToStream(): void
267289
'abc' => 'def',
268290
'REQUEST_METHOD' => 'POST',
269291
],
292+
'response-content-toarray' => [
293+
'abc' => 'def',
294+
'REQUEST_METHOD' => 'POST',
295+
],
270296
],
271297
];
272298
$this->assertSameResponseContentLog($expected, $logger->logs);
@@ -302,6 +328,10 @@ public function testDestroyResponse(): void
302328
'abc' => 'def',
303329
'REQUEST_METHOD' => 'POST',
304330
],
331+
'response-content-toarray' => [
332+
'abc' => 'def',
333+
'REQUEST_METHOD' => 'POST',
334+
],
305335
],
306336
];
307337
$this->assertSameResponseContentLog($expected, $logger->logs);
@@ -400,6 +430,10 @@ public function testCancelResponseAfterGetContent(): void
400430
'abc' => 'def',
401431
'REQUEST_METHOD' => 'POST',
402432
],
433+
'response-content-toarray' => [
434+
'abc' => 'def',
435+
'REQUEST_METHOD' => 'POST',
436+
],
403437
'response-headers-content-type' => ['application/json'],
404438
'info-canceled' => false,
405439
],
@@ -491,6 +525,18 @@ public function testHttp404(): void
491525
'HTTP_ACCEPT_ENCODING' => 'gzip',
492526
'HTTP_HOST' => '127.0.0.1:8057',
493527
],
528+
'response-content-toarray' => [
529+
'SERVER_PROTOCOL' => 'HTTP/1.1',
530+
'SERVER_NAME' => '127.0.0.1',
531+
'REQUEST_URI' => '/404',
532+
'REQUEST_METHOD' => 'POST',
533+
'HTTP_ACCEPT' => '*/*',
534+
'HTTP_CONTENT_LENGTH' => '7',
535+
'HTTP_CONTENT_TYPE' => 'application/x-www-form-urlencoded',
536+
'HTTP_USER_AGENT' => 'Symfony HttpClient (Amp)',
537+
'HTTP_ACCEPT_ENCODING' => 'gzip',
538+
'HTTP_HOST' => '127.0.0.1:8057',
539+
],
494540
],
495541
];
496542
$this->assertSameResponseContentLog($expected, $logger->logs);
@@ -541,6 +587,18 @@ public function testHttp404ThrowHttpException(): void
541587
'HTTP_ACCEPT_ENCODING' => 'gzip',
542588
'HTTP_HOST' => '127.0.0.1:8057',
543589
],
590+
'response-content-toarray' => [
591+
'SERVER_PROTOCOL' => 'HTTP/1.1',
592+
'SERVER_NAME' => '127.0.0.1',
593+
'REQUEST_URI' => '/404',
594+
'REQUEST_METHOD' => 'POST',
595+
'HTTP_ACCEPT' => '*/*',
596+
'HTTP_CONTENT_LENGTH' => '7',
597+
'HTTP_CONTENT_TYPE' => 'application/x-www-form-urlencoded',
598+
'HTTP_USER_AGENT' => 'Symfony HttpClient (Amp)',
599+
'HTTP_ACCEPT_ENCODING' => 'gzip',
600+
'HTTP_HOST' => '127.0.0.1:8057',
601+
],
544602
],
545603
];
546604
$this->assertSameResponseContentLog($expected, $logger->logs);

0 commit comments

Comments
 (0)