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

Commit d940ae4

Browse files
committed
Update guzzle6 adapater with httplug new contract
1 parent afb3854 commit d940ae4

File tree

3 files changed

+47
-48
lines changed

3 files changed

+47
-48
lines changed

composer.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,22 @@
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/message-factory": "dev-master as 0.1.1",
26+
"php-http/adapter-integration-tests": "dev-feature/httplug"
2527
},
28+
"repositories": [
29+
{
30+
"type": "vcs",
31+
"url": "https://github.com/joelwurtz/adapter-integration-tests"
32+
}
33+
],
2634
"provide": {
2735
"php-http/client-implementation": "1.0"
2836
},

src/Guzzle6HttpAdapter.php

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,46 @@
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\HttpClient;
22+
use Http\Client\Utils\BatchResult;
1823
use Psr\Http\Message\RequestInterface;
1924
use Psr\Http\Message\ResponseInterface;
2025

2126
/**
2227
* @author David de Boer <[email protected]>
2328
*/
24-
class Guzzle6HttpAdapter implements HttpAdapter
29+
class Guzzle6HttpAdapter implements HttpClient
2530
{
2631
/**
2732
* @var ClientInterface
2833
*/
2934
private $client;
3035

3136
/**
32-
* @param ClientInterface|null $client
37+
* @var array Options to pass when sending one or multiple requests with guzzle
3338
*/
34-
public function __construct(ClientInterface $client = null)
39+
private $options;
40+
41+
/**
42+
* @param ClientInterface|null $client Guzzle client
43+
* @param array $options Options to pass when sending one or multiple requests with guzzle
44+
*/
45+
public function __construct(ClientInterface $client = null, array $options = [])
3546
{
36-
$this->client = $client ?: new Client();
47+
$this->client = $client ?: new Client();
48+
$this->options = $this->buildOptions($options);
3749
}
3850

3951
/**
4052
* {@inheritdoc}
4153
*/
42-
public function sendRequest(RequestInterface $request, array $options = [])
54+
public function sendRequest(RequestInterface $request)
4355
{
44-
$options = $this->buildOptions($options);
45-
4656
try {
47-
return $this->client->send($request, $options);
57+
return $this->client->send($request, $this->options);
4858
} catch (RequestException $e) {
4959
throw $this->createException($e);
5060
}
@@ -53,56 +63,42 @@ public function sendRequest(RequestInterface $request, array $options = [])
5363
/**
5464
* {@inheritdoc}
5565
*/
56-
public function sendRequests(array $requests, array $options = [])
66+
public function sendRequests(array $requests)
5767
{
58-
$options = $this->buildOptions($options);
68+
$poolResult = Pool::batch($this->client, $requests, ['options' => $this->options]);
69+
$batchResult = new BatchResult();
5970

60-
$results = Pool::batch($this->client, $requests, $options);
61-
62-
$exceptions = [];
63-
$responses = [];
64-
65-
foreach ($results as $result) {
71+
foreach ($poolResult as $index => $result) {
6672
if ($result instanceof ResponseInterface) {
67-
$responses[] = $result;
68-
} elseif ($result instanceof RequestException) {
69-
$exceptions[] = $this->createException($result);
73+
$batchResult = $batchResult->addResponse($requests[$index], $result);
7074
}
71-
}
7275

73-
if (count($exceptions) > 0) {
74-
throw new Exception\MultiHttpAdapterException($exceptions, $responses);
76+
if ($result instanceof RequestException) {
77+
$batchResult = $batchResult->addException($requests[$index], $this->createException($result));
78+
}
7579
}
7680

77-
return $results;
78-
}
81+
if ($batchResult->hasExceptions()) {
82+
throw new BatchException($batchResult);
83+
}
7984

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

8888
/**
89-
* Converts a Guzzle exception into an HttpAdapter exception
89+
* Converts a Guzzle exception into an Httplug exception
9090
*
9191
* @param RequestException $exception
9292
*
93-
* @return Exception\HttpAdapterException
93+
* @return HttpException|NetworkException Return an HttpException if response is available, NetworkException otherwise
9494
*/
9595
private function createException(RequestException $exception)
9696
{
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;
97+
if ($exception->hasResponse()) {
98+
return new HttpException($exception->getMessage(), $exception->getRequest(), $exception->getResponse(), $exception);
99+
}
100+
101+
return new NetworkException($exception->getMessage(), $exception->getRequest(), $exception);
106102
}
107103

108104
/**

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)