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

Update guzzle6 adapater with httplug new contract #5

Merged
merged 4 commits into from
Oct 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ cache:
- $HOME/.composer/cache

php:
- 5.4
- 5.5
- 5.6
- 7.0
Expand All @@ -20,9 +19,10 @@ env:
matrix:
allow_failures:
- php: 7.0
- php: hhvm
fast_finish: true
include:
- php: 5.4
- php: 5.5
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" COVERAGE=true TEST_COMMAND="composer test-ci"

before_install:
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
}
],
"require": {
"php": ">=5.4",
"php": ">=5.5.0",
"php-http/httplug": "^1.0@dev",
"php-http/utils": "^0.1@dev",
"guzzlehttp/guzzle": "^6.0"
},
"require-dev": {
"ext-curl": "*",
"php-http/adapter-integration-tests": "^0.1"
"php-http/adapter-integration-tests": "^0.2@dev"
},
"provide": {
"php-http/client-implementation": "1.0"
Expand Down
86 changes: 27 additions & 59 deletions src/Guzzle6HttpAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,27 @@
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Pool;
use Http\Client\Exception\BatchException;
use Http\Client\Exception\HttpException;
use Http\Client\Exception\NetworkException;
use Http\Client\Exception;
use Http\Client\HttpClient;
use Http\Client\Utils\BatchResult;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

/**
* @author David de Boer <[email protected]>
*/
class Guzzle6HttpAdapter implements HttpAdapter
class Guzzle6HttpAdapter implements HttpClient
{
/**
* @var ClientInterface
*/
private $client;

/**
* @param ClientInterface|null $client
* @param ClientInterface|null $client Guzzle client
*/
public function __construct(ClientInterface $client = null)
{
Expand All @@ -39,12 +45,10 @@ public function __construct(ClientInterface $client = null)
/**
* {@inheritdoc}
*/
public function sendRequest(RequestInterface $request, array $options = [])
public function sendRequest(RequestInterface $request)
{
$options = $this->buildOptions($options);

try {
return $this->client->send($request, $options);
return $this->client->send($request);
} catch (RequestException $e) {
throw $this->createException($e);
}
Expand All @@ -53,77 +57,41 @@ public function sendRequest(RequestInterface $request, array $options = [])
/**
* {@inheritdoc}
*/
public function sendRequests(array $requests, array $options = [])
public function sendRequests(array $requests)
{
$options = $this->buildOptions($options);

$results = Pool::batch($this->client, $requests, $options);

$exceptions = [];
$responses = [];
$poolResult = Pool::batch($this->client, $requests);
$batchResult = new BatchResult();

foreach ($results as $result) {
foreach ($poolResult as $index => $result) {
if ($result instanceof ResponseInterface) {
$responses[] = $result;
} elseif ($result instanceof RequestException) {
$exceptions[] = $this->createException($result);
$batchResult = $batchResult->addResponse($requests[$index], $result);
}
}

if (count($exceptions) > 0) {
throw new Exception\MultiHttpAdapterException($exceptions, $responses);
if ($result instanceof RequestException) {
$batchResult = $batchResult->addException($requests[$index], $this->createException($result));
}
}

return $results;
}
if ($batchResult->hasExceptions()) {
throw new BatchException($batchResult);
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'guzzle6';
return $batchResult;
}

/**
* Converts a Guzzle exception into an HttpAdapter exception
* Converts a Guzzle exception into an Httplug exception
*
* @param RequestException $exception
*
* @return Exception\HttpAdapterException
* @return Exception
*/
private function createException(RequestException $exception)
{
$adapterException = new Exception\HttpAdapterException(
$exception->getMessage(),
0,
$exception
);
$adapterException->setResponse($exception->getResponse());
$adapterException->setRequest($exception->getRequest());

return $adapterException;
}

/**
* Builds options for Guzzle
*
* @param array $options
*
* @return array
*/
private function buildOptions(array $options)
{
$guzzleOptions = [
'http_errors' => false,
'allow_redirects' => false,
];

if (isset($options['timeout'])) {
$guzzleOptions['connect_timeout'] = $options['timeout'];
$guzzleOptions['timeout'] = $options['timeout'];
if ($exception->hasResponse()) {
return new HttpException($exception->getMessage(), $exception->getRequest(), $exception->getResponse(), $exception);
}

return $guzzleOptions;
return new NetworkException($exception->getMessage(), $exception->getRequest(), $exception);
}
}
5 changes: 0 additions & 5 deletions tests/Guzzle6HttpAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@
*/
abstract class Guzzle6HttpAdapterTest extends HttpAdapterTest
{
public function testGetName()
{
$this->assertSame('guzzle6', $this->httpAdapter->getName());
}

/**
* {@inheritdoc}
*/
Expand Down