bohan/promise-http-client

This package is abandoned and no longer maintained. The author suggests using the manyou/promise-http-client package instead.

Make a Symfony HttpClient to return Guzzle promises

v0.4.0 2021-03-15 14:58 UTC

This package is auto-updated.

Last update: 2022-11-08 07:53:37 UTC


README

Install

composer require bohan/promise-http-client

If you don't have a symfony/http-client-implementation yet:

composer require symfony/http-client

Example usage

use Bohan\PromiseHttpClient\PromiseHttpClient;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Contracts\HttpClient\ResponseInterface;

$client = new PromiseHttpClient(HttpClient::create());
$promise = $client->request('GET', 'https://httpbin.org/status/429')->then(function (ResponseInterface $response) use ($client) {
    if ($response->getStatusCode() < 300) {
        return $response;
    }

    return $client->request('GET', 'https://httpbin.org/get');
});

/** @var ResponseInterface $response */
$response = $promise->wait();

echo $response->getStatusCode(); // 200

Credits

Most code of

comes from the HttplugClient of the Symfony HttpClient component.