Skip to content

add headers and cookies support to response class #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 27, 2025
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: 3 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ parameters:
level: 9
paths:
- src
checkMissingIterableValueType: false
ignoreErrors:
-
identifier: missingType.iterableValue
12 changes: 9 additions & 3 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ class Client implements ClientInterface
public function __construct(
protected readonly array $options = [],
protected readonly array $headers = [],
) {
}
) {}

/**
* {@inheritDoc}
Expand Down Expand Up @@ -55,7 +54,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,
);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ClientFactory
*/
public static function create(): ClientInterface
{
return new Client();
return new Client;
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/Exceptions/ClientException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@

use Exception;

class ClientException extends Exception
{
}
class ClientException extends Exception {}
4 changes: 1 addition & 3 deletions src/Exceptions/ServerException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@

use Exception;

class ServerException extends Exception
{
}
class ServerException extends Exception {}
3 changes: 1 addition & 2 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ public function __construct(
protected HttpMethod $method,
protected string $url,
protected array $body = [],
) {
}
) {}

/**
* @return $this
Expand Down
22 changes: 20 additions & 2 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Imahmood\HttpClient;

use GuzzleHttp\Cookie\CookieJarInterface;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Imahmood\HttpClient\Exceptions\ClientException;
Expand All @@ -15,10 +16,11 @@ 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,
) {
}
) {}

/**
* {@inheritDoc}
Expand All @@ -40,6 +42,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}
*/
Expand Down
12 changes: 12 additions & 0 deletions src/ResponseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace Imahmood\HttpClient;

use GuzzleHttp\Cookie\CookieJarInterface;

interface ResponseInterface
{
/**
Expand All @@ -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.
*/
Expand Down
18 changes: 9 additions & 9 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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,
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand Down
4 changes: 1 addition & 3 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@

use Orchestra\Testbench\TestCase as Orchestra;

abstract class TestCase extends Orchestra
{
}
abstract class TestCase extends Orchestra {}