Skip to content

Commit db8ff3d

Browse files
authored
Change client send return type to WatiResponse (#4)
1 parent bbff87f commit db8ff3d

4 files changed

Lines changed: 102 additions & 4 deletions

File tree

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to `phpjuice/wati-http-client` will be documented in this file.
44

5+
## 1.0.4
6+
7+
- Add tests for `WatiResponse`
8+
59
## 1.0.3
610

711
- Add `WatiResponse` - Base response class

src/WatiClient.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use GuzzleHttp\Exception\GuzzleException;
1111
use GuzzleHttp\Exception\ServerException;
1212
use Psr\Http\Message\RequestInterface;
13-
use Psr\Http\Message\ResponseInterface;
1413
use Wati\Http\Exceptions\AuthenticationException;
1514
use Wati\Http\Exceptions\RateLimitException;
1615
use Wati\Http\Exceptions\ValidationException;
@@ -58,7 +57,7 @@ public function __construct(
5857
* @throws WatiApiException
5958
* @throws WatiException
6059
*/
61-
public function send(RequestInterface $request): ResponseInterface
60+
public function send(RequestInterface $request): WatiResponse
6261
{
6362
$request = $this->normalizeRequestPath($request);
6463

@@ -70,7 +69,7 @@ public function send(RequestInterface $request): ResponseInterface
7069
$request = $this->injectSdkHeaders($request);
7170

7271
try {
73-
return $this->client->send($request);
72+
return WatiResponse::fromResponse($this->client->send($request));
7473
} catch (ClientException $e) {
7574
$response = $e->getResponse();
7675
$statusCode = $response->getStatusCode();

src/WatiResponse.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,32 @@
77
use GuzzleHttp\Psr7\Response;
88
use Psr\Http\Message\ResponseInterface;
99

10-
abstract class WatiResponse extends Response implements ResponseInterface {}
10+
final class WatiResponse extends Response implements ResponseInterface
11+
{
12+
public static function fromResponse(ResponseInterface $response): self
13+
{
14+
return new self(
15+
$response->getStatusCode(),
16+
$response->getHeaders(),
17+
$response->getBody(),
18+
$response->getProtocolVersion(),
19+
$response->getReasonPhrase()
20+
);
21+
}
22+
23+
public function json(): mixed
24+
{
25+
return json_decode($this->getBody()->getContents(), true);
26+
}
27+
28+
public function isSuccessful(): bool
29+
{
30+
$json = $this->json();
31+
32+
if (! is_array($json)) {
33+
return false;
34+
}
35+
36+
return ($json['result'] ?? null) === 'success';
37+
}
38+
}

tests/WatiResponseTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Http;
6+
7+
use GuzzleHttp\Psr7\Response;
8+
use Wati\Http\WatiResponse;
9+
10+
it('can create from response', function (): void {
11+
$response = new Response(200, ['Content-Type' => 'application/json'], '{"key":"value"}');
12+
$watiResponse = WatiResponse::fromResponse($response);
13+
14+
expect($watiResponse->getStatusCode())->toBe(200)
15+
->and($watiResponse->getHeaderLine('Content-Type'))->toBe('application/json')
16+
->and($watiResponse->getBody()->getContents())->toBe('{"key":"value"}');
17+
});
18+
19+
it('can parse json body', function (): void {
20+
$response = new Response(200, [], '{"name":"John","age":30}');
21+
$watiResponse = WatiResponse::fromResponse($response);
22+
23+
expect($watiResponse->json())->toBe(['name' => 'John', 'age' => 30]);
24+
});
25+
26+
it('returns null for invalid json', function (): void {
27+
$response = new Response(200, [], 'not valid json');
28+
$watiResponse = WatiResponse::fromResponse($response);
29+
30+
expect($watiResponse->json())->toBeNull();
31+
});
32+
33+
it('returns true when result is success', function (): void {
34+
$response = new Response(200, [], '{"result":"success","data":[]}');
35+
$watiResponse = WatiResponse::fromResponse($response);
36+
37+
expect($watiResponse->isSuccessful())->toBeTrue();
38+
});
39+
40+
it('returns false when result is not success', function (): void {
41+
$response = new Response(200, [], '{"result":"error","message":"Failed"}');
42+
$watiResponse = WatiResponse::fromResponse($response);
43+
44+
expect($watiResponse->isSuccessful())->toBeFalse();
45+
});
46+
47+
it('returns false when result key is missing', function (): void {
48+
$response = new Response(200, [], '{"status":"ok","data":[]}');
49+
$watiResponse = WatiResponse::fromResponse($response);
50+
51+
expect($watiResponse->isSuccessful())->toBeFalse();
52+
});
53+
54+
it('returns false for non array json', function (): void {
55+
$response = new Response(200, [], '"just a string"');
56+
$watiResponse = WatiResponse::fromResponse($response);
57+
58+
expect($watiResponse->isSuccessful())->toBeFalse();
59+
});
60+
61+
it('preserves protocol version and reason phrase', function (): void {
62+
$response = new Response(404, [], 'Not Found', '1.1', 'Not Found');
63+
$watiResponse = WatiResponse::fromResponse($response);
64+
65+
expect($watiResponse->getProtocolVersion())->toBe('1.1')
66+
->and($watiResponse->getReasonPhrase())->toBe('Not Found');
67+
});

0 commit comments

Comments
 (0)