From 49b6a579eea5ef0669d6ee1cf57d01c0abce3870 Mon Sep 17 00:00:00 2001 From: Mahmood Dehghani Date: Sun, 27 Apr 2025 15:18:44 +0330 Subject: [PATCH 1/2] add headers and cookies support to response class --- src/Client.php | 9 ++++++++- src/Response.php | 19 +++++++++++++++++++ src/ResponseInterface.php | 12 ++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/Client.php b/src/Client.php index a0f1a4f..8183bbb 100644 --- a/src/Client.php +++ b/src/Client.php @@ -55,7 +55,14 @@ protected function convertResponse( LaravelHttpResponse $response, float $duration, ): ResponseInterface { - return new Response($request->getUrl(), $response->body(), $response->status(), $duration); + return new Response( + $request->getUrl(), + $response->body(), + $response->headers(), + $response->cookies(), + $response->status(), + $duration, + ); } /** diff --git a/src/Response.php b/src/Response.php index 39a35ff..646f60c 100644 --- a/src/Response.php +++ b/src/Response.php @@ -3,6 +3,7 @@ namespace Imahmood\HttpClient; +use GuzzleHttp\Cookie\CookieJarInterface; use Illuminate\Support\Str; use Illuminate\Validation\ValidationException; use Imahmood\HttpClient\Exceptions\ClientException; @@ -15,6 +16,8 @@ class Response implements ResponseInterface public function __construct( private readonly string $url, private readonly string $body, + private readonly array $headers, + private readonly CookieJarInterface $cookies, private readonly int $statusCode, private readonly float $duration, ) { @@ -40,6 +43,22 @@ public function toArray(): array return $this->decodedBody; } + /** + * {@inheritDoc} + */ + public function headers(): array + { + return $this->headers; + } + + /** + * {@inheritDoc} + */ + public function cookies(): CookieJarInterface + { + return $this->cookies; + } + /** * {@inheritDoc} */ diff --git a/src/ResponseInterface.php b/src/ResponseInterface.php index 49d9b88..11d7d7e 100644 --- a/src/ResponseInterface.php +++ b/src/ResponseInterface.php @@ -3,6 +3,8 @@ namespace Imahmood\HttpClient; +use GuzzleHttp\Cookie\CookieJarInterface; + interface ResponseInterface { /** @@ -17,6 +19,16 @@ public function body(): string; */ public function toArray(): array; + /** + * Get the response headers. + */ + public function headers(): array; + + /** + * Get the cookies from the response. + */ + public function cookies(): CookieJarInterface; + /** * Check if the response content is JSON. */ From 9115be29109a27eb78b9265b0b1f324bb1a1ee3b Mon Sep 17 00:00:00 2001 From: Mahmood Dehghani Date: Sun, 27 Apr 2025 15:32:23 +0330 Subject: [PATCH 2/2] fix pint errors --- phpstan.neon | 4 +++- src/Client.php | 3 +-- src/ClientFactory.php | 2 +- src/Exceptions/ClientException.php | 4 +--- src/Exceptions/ServerException.php | 4 +--- src/Request.php | 3 +-- src/Response.php | 3 +-- tests/ClientTest.php | 18 +++++++++--------- tests/TestCase.php | 4 +--- 9 files changed, 19 insertions(+), 26 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index f69a29e..640682a 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -2,4 +2,6 @@ parameters: level: 9 paths: - src - checkMissingIterableValueType: false + ignoreErrors: + - + identifier: missingType.iterableValue diff --git a/src/Client.php b/src/Client.php index 8183bbb..4aa677d 100644 --- a/src/Client.php +++ b/src/Client.php @@ -22,8 +22,7 @@ class Client implements ClientInterface public function __construct( protected readonly array $options = [], protected readonly array $headers = [], - ) { - } + ) {} /** * {@inheritDoc} diff --git a/src/ClientFactory.php b/src/ClientFactory.php index 22a85a1..ca5e774 100644 --- a/src/ClientFactory.php +++ b/src/ClientFactory.php @@ -10,7 +10,7 @@ class ClientFactory */ public static function create(): ClientInterface { - return new Client(); + return new Client; } /** diff --git a/src/Exceptions/ClientException.php b/src/Exceptions/ClientException.php index 4cfb26a..10a2e89 100644 --- a/src/Exceptions/ClientException.php +++ b/src/Exceptions/ClientException.php @@ -5,6 +5,4 @@ use Exception; -class ClientException extends Exception -{ -} +class ClientException extends Exception {} diff --git a/src/Exceptions/ServerException.php b/src/Exceptions/ServerException.php index 20c78d3..5099e58 100644 --- a/src/Exceptions/ServerException.php +++ b/src/Exceptions/ServerException.php @@ -5,6 +5,4 @@ use Exception; -class ServerException extends Exception -{ -} +class ServerException extends Exception {} diff --git a/src/Request.php b/src/Request.php index 4bd2e4e..296924d 100644 --- a/src/Request.php +++ b/src/Request.php @@ -16,8 +16,7 @@ public function __construct( protected HttpMethod $method, protected string $url, protected array $body = [], - ) { - } + ) {} /** * @return $this diff --git a/src/Response.php b/src/Response.php index 646f60c..edbdf50 100644 --- a/src/Response.php +++ b/src/Response.php @@ -20,8 +20,7 @@ public function __construct( private readonly CookieJarInterface $cookies, private readonly int $statusCode, private readonly float $duration, - ) { - } + ) {} /** * {@inheritDoc} diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 41e41f1..74be3ce 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -14,7 +14,7 @@ class ClientTest extends TestCase { - public function testGetRequest(): void + public function test_get_request(): void { Http::fake([ 'https://reqres.in/*' => Http::response('ok', 200), @@ -28,7 +28,7 @@ public function testGetRequest(): void $this->assertEquals('ok', $response->body()); } - public function testPostRequest(): void + public function test_post_request(): void { $body = [ 'id' => 100, @@ -47,7 +47,7 @@ public function testPostRequest(): void $this->assertEquals($body, $response->toArray()); } - public function testPutRequest(): void + public function test_put_request(): void { $body = [ 'name' => 'user name', @@ -65,7 +65,7 @@ public function testPutRequest(): void $this->assertEquals($body, $response->toArray()); } - public function testPatchRequest(): void + public function test_patch_request(): void { $body = [ 'name' => 'user name', @@ -83,7 +83,7 @@ public function testPatchRequest(): void $this->assertEquals($body, $response->toArray()); } - public function testDeleteRequest(): void + public function test_delete_request(): void { Http::fake([ 'https://reqres.in/*' => Http::response(null, 204), @@ -96,7 +96,7 @@ public function testDeleteRequest(): void $this->assertEquals(204, $response->statusCode()); } - public function testClientErrorResponses(): void + public function test_client_error_responses(): void { Http::fake([ 'https://reqres.in/*' => Http::response([], 401), @@ -115,7 +115,7 @@ public function testClientErrorResponses(): void $response->validate(); } - public function testServerErrorResponses(): void + public function test_server_error_responses(): void { Http::fake([ 'https://reqres.in/*' => Http::response([], 500), @@ -134,7 +134,7 @@ public function testServerErrorResponses(): void $response->validate(); } - public function testValidationErrorResponses(): void + public function test_validation_error_responses(): void { Http::fake([ 'https://reqres.in/*' => Http::response(['errors' => ['username' => []]], 422), @@ -153,7 +153,7 @@ public function testValidationErrorResponses(): void $response->validate(); } - public function testUploadFile(): void + public function test_upload_file(): void { Http::fake([ 'https://reqres.in/*' => Http::response([], 200), diff --git a/tests/TestCase.php b/tests/TestCase.php index ba932d3..933c645 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -5,6 +5,4 @@ use Orchestra\Testbench\TestCase as Orchestra; -abstract class TestCase extends Orchestra -{ -} +abstract class TestCase extends Orchestra {}