Skip to content
This repository was archived by the owner on Jan 6, 2024. It is now read-only.

Commit f14404f

Browse files
committed
Merge pull request #1 from ddeboer/implementation
Implement Guzzle 6 adapter
2 parents 99a00f1 + 5483a9b commit f14404f

File tree

2 files changed

+103
-3
lines changed

2 files changed

+103
-3
lines changed

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
"homepage": "http://php-http.org",
77
"authors": [
88
{
9-
"name": "Eric GELOEN",
10-
"email": "[email protected]"
9+
"name": "David de Boer",
10+
"email": "[email protected]"
1111
}
1212
],
1313
"require": {
1414
"php": ">=5.4",
15-
"php-http/adapter-core": "dev-internal_separation"
15+
"php-http/adapter-core": "dev-internal_separation",
16+
"guzzlehttp/guzzle": "~6.0"
1617
},
1718
"require-dev": {
1819
"ext-curl": "*",

src/Guzzle6HttpAdapter.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Http Adapter package.
5+
*
6+
* (c) Eric GELOEN <[email protected]>
7+
*
8+
* For the full copyright and license information, please read the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Http\Adapter;
13+
14+
use GuzzleHttp\Client;
15+
use GuzzleHttp\Exception\RequestException;
16+
use GuzzleHttp\Pool;
17+
use Http\Adapter\Common\Exception\HttpAdapterException;
18+
use Http\Adapter\Common\Exception\MultiHttpAdapterException;
19+
use Psr\Http\Message\RequestInterface;
20+
21+
/**
22+
* Guzzle 6 HTTP adapter
23+
*
24+
* @author David de Boer <[email protected]>
25+
*/
26+
class Guzzle6HttpAdapter implements PsrHttpAdapter
27+
{
28+
/**
29+
* @param Client $client
30+
*/
31+
public function __construct(Client $client = null)
32+
{
33+
$this->client = $client ?: new Client();
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public function sendRequest(RequestInterface $request)
40+
{
41+
try {
42+
return $this->client->send($request);
43+
} catch (RequestException $exception) {
44+
throw new $this->createException($exception);
45+
}
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
public function sendRequests(array $requests)
52+
{
53+
$results = Pool::batch(
54+
$this->client,
55+
$requests
56+
);
57+
58+
$exceptions = [];
59+
foreach ($results as $result) {
60+
if ($result instanceof TransferException) {
61+
$exceptions[] = $this->createException($result);
62+
}
63+
}
64+
65+
if (count($exceptions) > 0) {
66+
throw new MultiHttpAdapterException($exceptions);
67+
}
68+
69+
return $results;
70+
}
71+
72+
/**
73+
* {@inheritdoc}
74+
*/
75+
public function getName()
76+
{
77+
return 'guzzle6';
78+
}
79+
80+
/**
81+
* Convert Guzzle exception into HttpAdapter exception
82+
*
83+
* @param RequestException $exception
84+
*
85+
* @return HttpAdapterException
86+
*/
87+
private function createException(RequestException $exception)
88+
{
89+
$adapterException = new HttpAdapterException(
90+
$exception->getMessage(),
91+
0,
92+
$exception
93+
);
94+
$adapterException->setResponse($exception->getResponse());
95+
$adapterException->setRequest($exception->getRequest());
96+
97+
return $adapterException;
98+
}
99+
}

0 commit comments

Comments
 (0)