Skip to content

Commit

Permalink
Refactor client handling to use custom async task management and add …
Browse files Browse the repository at this point in the history
…fluent API
  • Loading branch information
Thavarshan committed Sep 29, 2024
1 parent 1c036fb commit 87484c1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 33 deletions.
4 changes: 4 additions & 0 deletions src/Fetch/Http/ClientHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ public static function handle(string $method, string $uri, array $options = [])
*/
protected function applyOptions(array $options): void
{
if (isset($options['client'])) {
$this->setSyncClient($options['client']);
}

$this->options = array_merge($this->options, $options);

if (isset($options['timeout'])) {
Expand Down
44 changes: 11 additions & 33 deletions tests/Unit/FetchTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

use Fetch\Http\ClientHandler;
use Fetch\Http\Response;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
Expand All @@ -22,10 +21,7 @@
->andReturn(new Response(200, [], json_encode(['success' => true])));
});

$clientHandler = new ClientHandler();
$clientHandler->setSyncClient($mockClient);

$response = fetch('https://example.com');
$response = fetch('https://example.com', ['client' => $mockClient]);

expect($response->json())->toBe(['success' => true]);
expect($response->getStatusCode())->toBe(200);
Expand All @@ -42,10 +38,7 @@
->andReturn(new Response(200, [], json_encode(['async' => 'result'])));
});

$clientHandler = new ClientHandler();
$clientHandler->setSyncClient($mockClient);

async(fn () => fetch('https://example.com', ['async' => true]))
async(fn () => fetch('https://example.com', ['client' => $mockClient]))
->then(function (Response $response) {
expect($response->json())->toBe(['async' => 'result']);
expect($response->getStatusCode())->toBe(200);
Expand All @@ -68,11 +61,9 @@
->andReturn(new Response(200, [], 'Headers checked'));
});

$clientHandler = new ClientHandler();
$clientHandler->setSyncClient($mockClient);

$response = fetch('https://example.com', [
'headers' => ['Authorization' => 'Bearer token']
'headers' => ['Authorization' => 'Bearer token'],
'client' => $mockClient
]);

expect($response->text())->toBe('Headers checked');
Expand All @@ -91,11 +82,9 @@
->andReturn(new Response(200, [], 'Query params checked'));
});

$clientHandler = new ClientHandler();
$clientHandler->setSyncClient($mockClient);

$response = fetch('https://example.com', [
'query' => ['foo' => 'bar', 'baz' => 'qux']
'query' => ['foo' => 'bar', 'baz' => 'qux'],
'client' => $mockClient
]);

expect($response->text())->toBe('Query params checked');
Expand All @@ -114,11 +103,8 @@
->andThrow(new RequestException('Timeout', new Request('GET', 'https://example.com')));
});

$clientHandler = new ClientHandler();
$clientHandler->setSyncClient($mockClient);

try {
fetch('https://example.com', ['timeout' => 1]);
fetch('https://example.com', ['timeout' => 1, 'client' => $mockClient]);
} catch (RequestException $e) {
expect($e->getMessage())->toContain('Timeout');
}
Expand All @@ -139,10 +125,7 @@
->andReturn(new Response(200, [], 'Success after retry'));
});

$clientHandler = new ClientHandler();
$clientHandler->setSyncClient($mockClient);

$response = fetch('https://example.com', ['retries' => 2]);
$response = fetch('https://example.com', ['retries' => 2, 'client' => $mockClient]);

expect($response->text())->toBe('Success after retry');
});
Expand All @@ -160,12 +143,10 @@
->andReturn(new Response(201, [], 'Created'));
});

$clientHandler = new ClientHandler();
$clientHandler->setSyncClient($mockClient);

$response = fetch('https://example.com/users', [
'method' => 'POST',
'body' => json_encode(['name' => 'John'])
'body' => json_encode(['name' => 'John']),
'client' => $mockClient
]);

expect($response->getStatusCode())->toBe(201);
Expand All @@ -187,10 +168,7 @@
->andReturn(new Response(200, [], 'Success after retry'));
});

$clientHandler = new ClientHandler();
$clientHandler->setSyncClient($mockClient);

async(fn () => fetch('https://example.com', ['retries' => 2, 'async' => true]))
async(fn () => fetch('https://example.com', ['retries' => 2, 'client' => $mockClient]))
->then(function (Response $response) {
expect($response->text())->toBe('Success after retry');
})
Expand Down

0 comments on commit 87484c1

Please sign in to comment.