diff --git a/composer.json b/composer.json index f891662..38ac052 100644 --- a/composer.json +++ b/composer.json @@ -6,13 +6,14 @@ "homepage": "http://php-http.org", "authors": [ { - "name": "Eric GELOEN", - "email": "geloen.eric@gmail.com" + "name": "David de Boer", + "email": "david@ddeboer.nl" } ], "require": { "php": ">=5.4", - "php-http/adapter-core": "dev-internal_separation" + "php-http/adapter-core": "dev-internal_separation", + "guzzlehttp/guzzle": "~6.0" }, "require-dev": { "ext-curl": "*", diff --git a/src/Guzzle6HttpAdapter.php b/src/Guzzle6HttpAdapter.php new file mode 100644 index 0000000..08e1c49 --- /dev/null +++ b/src/Guzzle6HttpAdapter.php @@ -0,0 +1,99 @@ + + * + * For the full copyright and license information, please read the LICENSE + * file that was distributed with this source code. + */ + +namespace Http\Adapter; + +use GuzzleHttp\Client; +use GuzzleHttp\Exception\RequestException; +use GuzzleHttp\Pool; +use Http\Adapter\Common\Exception\HttpAdapterException; +use Http\Adapter\Common\Exception\MultiHttpAdapterException; +use Psr\Http\Message\RequestInterface; + +/** + * Guzzle 6 HTTP adapter + * + * @author David de Boer + */ +class Guzzle6HttpAdapter implements PsrHttpAdapter +{ + /** + * @param Client $client + */ + public function __construct(Client $client = null) + { + $this->client = $client ?: new Client(); + } + + /** + * {@inheritdoc} + */ + public function sendRequest(RequestInterface $request) + { + try { + return $this->client->send($request); + } catch (RequestException $exception) { + throw new $this->createException($exception); + } + } + + /** + * {@inheritdoc} + */ + public function sendRequests(array $requests) + { + $results = Pool::batch( + $this->client, + $requests + ); + + $exceptions = []; + foreach ($results as $result) { + if ($result instanceof TransferException) { + $exceptions[] = $this->createException($result); + } + } + + if (count($exceptions) > 0) { + throw new MultiHttpAdapterException($exceptions); + } + + return $results; + } + + /** + * {@inheritdoc} + */ + public function getName() + { + return 'guzzle6'; + } + + /** + * Convert Guzzle exception into HttpAdapter exception + * + * @param RequestException $exception + * + * @return HttpAdapterException + */ + private function createException(RequestException $exception) + { + $adapterException = new HttpAdapterException( + $exception->getMessage(), + 0, + $exception + ); + $adapterException->setResponse($exception->getResponse()); + $adapterException->setRequest($exception->getRequest()); + + return $adapterException; + } +}