Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

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

## 1.0.4

- Add tests for `WatiResponse`

## 1.0.3

- Add `WatiResponse` - Base response class
Expand Down
5 changes: 2 additions & 3 deletions src/WatiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\ServerException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Wati\Http\Exceptions\AuthenticationException;
use Wati\Http\Exceptions\RateLimitException;
use Wati\Http\Exceptions\ValidationException;
Expand Down Expand Up @@ -58,7 +57,7 @@ public function __construct(
* @throws WatiApiException
* @throws WatiException
*/
public function send(RequestInterface $request): ResponseInterface
public function send(RequestInterface $request): WatiResponse
{
$request = $this->normalizeRequestPath($request);

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

try {
return $this->client->send($request);
return WatiResponse::fromResponse($this->client->send($request));
} catch (ClientException $e) {
$response = $e->getResponse();
$statusCode = $response->getStatusCode();
Expand Down
30 changes: 29 additions & 1 deletion src/WatiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,32 @@
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ResponseInterface;

abstract class WatiResponse extends Response implements ResponseInterface {}
final class WatiResponse extends Response implements ResponseInterface
{
public static function fromResponse(ResponseInterface $response): self
{
return new self(
$response->getStatusCode(),
$response->getHeaders(),
$response->getBody(),
$response->getProtocolVersion(),
$response->getReasonPhrase()
);
}

public function json(): mixed
{
return json_decode($this->getBody()->getContents(), true);
}

public function isSuccessful(): bool
{
$json = $this->json();

if (! is_array($json)) {
return false;
}

return ($json['result'] ?? null) === 'success';
}
}
67 changes: 67 additions & 0 deletions tests/WatiResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace Tests\Http;

use GuzzleHttp\Psr7\Response;
use Wati\Http\WatiResponse;

it('can create from response', function (): void {
$response = new Response(200, ['Content-Type' => 'application/json'], '{"key":"value"}');
$watiResponse = WatiResponse::fromResponse($response);

expect($watiResponse->getStatusCode())->toBe(200)
->and($watiResponse->getHeaderLine('Content-Type'))->toBe('application/json')
->and($watiResponse->getBody()->getContents())->toBe('{"key":"value"}');
});

it('can parse json body', function (): void {
$response = new Response(200, [], '{"name":"John","age":30}');
$watiResponse = WatiResponse::fromResponse($response);

expect($watiResponse->json())->toBe(['name' => 'John', 'age' => 30]);
});

it('returns null for invalid json', function (): void {
$response = new Response(200, [], 'not valid json');
$watiResponse = WatiResponse::fromResponse($response);

expect($watiResponse->json())->toBeNull();
});

it('returns true when result is success', function (): void {
$response = new Response(200, [], '{"result":"success","data":[]}');
$watiResponse = WatiResponse::fromResponse($response);

expect($watiResponse->isSuccessful())->toBeTrue();
});

it('returns false when result is not success', function (): void {
$response = new Response(200, [], '{"result":"error","message":"Failed"}');
$watiResponse = WatiResponse::fromResponse($response);

expect($watiResponse->isSuccessful())->toBeFalse();
});

it('returns false when result key is missing', function (): void {
$response = new Response(200, [], '{"status":"ok","data":[]}');
$watiResponse = WatiResponse::fromResponse($response);

expect($watiResponse->isSuccessful())->toBeFalse();
});

it('returns false for non array json', function (): void {
$response = new Response(200, [], '"just a string"');
$watiResponse = WatiResponse::fromResponse($response);

expect($watiResponse->isSuccessful())->toBeFalse();
});

it('preserves protocol version and reason phrase', function (): void {
$response = new Response(404, [], 'Not Found', '1.1', 'Not Found');
$watiResponse = WatiResponse::fromResponse($response);

expect($watiResponse->getProtocolVersion())->toBe('1.1')
->and($watiResponse->getReasonPhrase())->toBe('Not Found');
});