antelucangizmos / guzzle-retry-handler
test guzzle client with retry support
Installs: 13
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Type:project
Requires
- guzzlehttp/guzzle: ^7.8
Requires (Dev)
- phpunit/phpunit: ^11.2
README
APIs might implement rate limiting, and if they do your clients might experience 429 Too Many Requests
responses with a Retry-After
header, informing your client how long it should wait before making the next request.
Guzzle includes a retry middleware class that can be used to handle this.
The implementation in this project is a PoC, so feel free to build upon it, and comment if you think something should be added / removed.
Example usage
require dirname(__DIR__, 2) . '/vendor/autoload.php'; use Ag\Guzzle\Handler\Retry; use GuzzleHttp\Client; use GuzzleHttp\Exception\ConnectException; use GuzzleHttp\Handler\CurlHandler; use GuzzleHttp\HandlerStack; use GuzzleHttp\Middleware; class TestRetry { public function test(int $maxRetries = 5) { $handlerStack = HandlerStack::create(new CurlHandler()); $retry = new Retry($maxRetries); $handlerStack->push(Middleware::retry($retry->retryDecider(), $retry->retryDelay())); $client = new Client(array('handler' => $handlerStack)); $response = $client->request( 'GET', // @ToDo replace to a real url!!! 'https://some-url-here' )->getBody()->getContents(); return print_r($response, true); } } try { $TestRetry = new TestRetry(); $response = $TestRetry->test(5); echo $response . PHP_EOL; } catch (ConnectException $e) { echo $e->getMessage() . PHP_EOL; }