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

Commit ed80bb0

Browse files
committed
Merge pull request #5 from joelwurtz/feature/link-httplug
Update guzzle6 adapater with httplug new contract
2 parents afb3854 + 8d12420 commit ed80bb0

File tree

4 files changed

+32
-68
lines changed

4 files changed

+32
-68
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ cache:
77
- $HOME/.composer/cache
88

99
php:
10-
- 5.4
1110
- 5.5
1211
- 5.6
1312
- 7.0
@@ -20,9 +19,10 @@ env:
2019
matrix:
2120
allow_failures:
2221
- php: 7.0
22+
- php: hhvm
2323
fast_finish: true
2424
include:
25-
- php: 5.4
25+
- php: 5.5
2626
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" COVERAGE=true TEST_COMMAND="composer test-ci"
2727

2828
before_install:

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
}
1616
],
1717
"require": {
18-
"php": ">=5.4",
18+
"php": ">=5.5.0",
1919
"php-http/httplug": "^1.0@dev",
20+
"php-http/utils": "^0.1@dev",
2021
"guzzlehttp/guzzle": "^6.0"
2122
},
2223
"require-dev": {
2324
"ext-curl": "*",
24-
"php-http/adapter-integration-tests": "^0.1"
25+
"php-http/adapter-integration-tests": "^0.2@dev"
2526
},
2627
"provide": {
2728
"php-http/client-implementation": "1.0"

src/Guzzle6HttpAdapter.php

Lines changed: 27 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,27 @@
1515
use GuzzleHttp\ClientInterface;
1616
use GuzzleHttp\Exception\RequestException;
1717
use GuzzleHttp\Pool;
18+
use Http\Client\Exception\BatchException;
19+
use Http\Client\Exception\HttpException;
20+
use Http\Client\Exception\NetworkException;
21+
use Http\Client\Exception;
22+
use Http\Client\HttpClient;
23+
use Http\Client\Utils\BatchResult;
1824
use Psr\Http\Message\RequestInterface;
1925
use Psr\Http\Message\ResponseInterface;
2026

2127
/**
2228
* @author David de Boer <[email protected]>
2329
*/
24-
class Guzzle6HttpAdapter implements HttpAdapter
30+
class Guzzle6HttpAdapter implements HttpClient
2531
{
2632
/**
2733
* @var ClientInterface
2834
*/
2935
private $client;
3036

3137
/**
32-
* @param ClientInterface|null $client
38+
* @param ClientInterface|null $client Guzzle client
3339
*/
3440
public function __construct(ClientInterface $client = null)
3541
{
@@ -39,12 +45,10 @@ public function __construct(ClientInterface $client = null)
3945
/**
4046
* {@inheritdoc}
4147
*/
42-
public function sendRequest(RequestInterface $request, array $options = [])
48+
public function sendRequest(RequestInterface $request)
4349
{
44-
$options = $this->buildOptions($options);
45-
4650
try {
47-
return $this->client->send($request, $options);
51+
return $this->client->send($request);
4852
} catch (RequestException $e) {
4953
throw $this->createException($e);
5054
}
@@ -53,77 +57,41 @@ public function sendRequest(RequestInterface $request, array $options = [])
5357
/**
5458
* {@inheritdoc}
5559
*/
56-
public function sendRequests(array $requests, array $options = [])
60+
public function sendRequests(array $requests)
5761
{
58-
$options = $this->buildOptions($options);
59-
60-
$results = Pool::batch($this->client, $requests, $options);
61-
62-
$exceptions = [];
63-
$responses = [];
62+
$poolResult = Pool::batch($this->client, $requests);
63+
$batchResult = new BatchResult();
6464

65-
foreach ($results as $result) {
65+
foreach ($poolResult as $index => $result) {
6666
if ($result instanceof ResponseInterface) {
67-
$responses[] = $result;
68-
} elseif ($result instanceof RequestException) {
69-
$exceptions[] = $this->createException($result);
67+
$batchResult = $batchResult->addResponse($requests[$index], $result);
7068
}
71-
}
7269

73-
if (count($exceptions) > 0) {
74-
throw new Exception\MultiHttpAdapterException($exceptions, $responses);
70+
if ($result instanceof RequestException) {
71+
$batchResult = $batchResult->addException($requests[$index], $this->createException($result));
72+
}
7573
}
7674

77-
return $results;
78-
}
75+
if ($batchResult->hasExceptions()) {
76+
throw new BatchException($batchResult);
77+
}
7978

80-
/**
81-
* {@inheritdoc}
82-
*/
83-
public function getName()
84-
{
85-
return 'guzzle6';
79+
return $batchResult;
8680
}
8781

8882
/**
89-
* Converts a Guzzle exception into an HttpAdapter exception
83+
* Converts a Guzzle exception into an Httplug exception
9084
*
9185
* @param RequestException $exception
9286
*
93-
* @return Exception\HttpAdapterException
87+
* @return Exception
9488
*/
9589
private function createException(RequestException $exception)
9690
{
97-
$adapterException = new Exception\HttpAdapterException(
98-
$exception->getMessage(),
99-
0,
100-
$exception
101-
);
102-
$adapterException->setResponse($exception->getResponse());
103-
$adapterException->setRequest($exception->getRequest());
104-
105-
return $adapterException;
106-
}
107-
108-
/**
109-
* Builds options for Guzzle
110-
*
111-
* @param array $options
112-
*
113-
* @return array
114-
*/
115-
private function buildOptions(array $options)
116-
{
117-
$guzzleOptions = [
118-
'http_errors' => false,
119-
'allow_redirects' => false,
120-
];
121-
122-
if (isset($options['timeout'])) {
123-
$guzzleOptions['connect_timeout'] = $options['timeout'];
124-
$guzzleOptions['timeout'] = $options['timeout'];
91+
if ($exception->hasResponse()) {
92+
return new HttpException($exception->getMessage(), $exception->getRequest(), $exception->getResponse(), $exception);
12593
}
12694

127-
return $guzzleOptions;
95+
return new NetworkException($exception->getMessage(), $exception->getRequest(), $exception);
12896
}
12997
}

tests/Guzzle6HttpAdapterTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@
1919
*/
2020
abstract class Guzzle6HttpAdapterTest extends HttpAdapterTest
2121
{
22-
public function testGetName()
23-
{
24-
$this->assertSame('guzzle6', $this->httpAdapter->getName());
25-
}
26-
2722
/**
2823
* {@inheritdoc}
2924
*/

0 commit comments

Comments
 (0)