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

Commit e1a319d

Browse files
committed
Merge pull request #5 from Nyholm/nyholm
Updated the client to reflect changes in httplug
2 parents 8afaf49 + 104e4dd commit e1a319d

File tree

4 files changed

+69
-126
lines changed

4 files changed

+69
-126
lines changed

.travis.yml

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,41 @@
11
language: php
22

3+
sudo: false
4+
5+
cache:
6+
directories:
7+
- $HOME/.composer/cache
8+
39
php:
410
- 5.4
511
- 5.5
612
- 5.6
713
- 7.0
814
- hhvm
915

16+
env:
17+
global:
18+
- TEST_COMMAND="composer test"
19+
1020
matrix:
1121
allow_failures:
1222
- php: 7.0
1323
- php: hhvm
1424
include:
1525
- php: 5.4
16-
env:
17-
- COMPOSER_FLAGS="--prefer-stable --prefer-lowest"
18-
- COVERAGE=true
19-
- PHPUNIT_FLAGS="--coverage-clover build/coverage.xml"
26+
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" COVERAGE=true TEST_COMMAND="composer test-ci"
2027

21-
install:
28+
before_install:
2229
- travis_retry composer self-update
30+
31+
install:
2332
- travis_retry composer update ${COMPOSER_FLAGS} --prefer-source --no-interaction
2433

25-
before_script: vendor/bin/http_test_server > /dev/null 2>&1 &
34+
before_script:
35+
- vendor/bin/http_test_server > /dev/null 2>&1 &
2636

27-
script: vendor/bin/phpunit ${PHPUNIT_FLAGS}
37+
script:
38+
- $TEST_COMMAND
2839

2940
after_script:
3041
- if [[ "$COVERAGE" = true ]]; then wget https://scrutinizer-ci.com/ocular.phar; fi

composer.json

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@
1616
],
1717
"require": {
1818
"php": ">=5.4",
19-
"php-http/adapter": "^0.1",
20-
"php-http/discovery": "^0.1",
21-
"guzzlehttp/guzzle": "^5.1",
22-
"guzzlehttp/ringphp": "^1.1"
19+
"php-http/httplug": "^1.0@dev",
20+
"php-http/discovery": "^0.2",
21+
"guzzlehttp/guzzle": "^5.1"
2322
},
2423
"require-dev": {
2524
"ext-curl": "*",
26-
"php-http/adapter-integration-tests": "^0.1"
25+
"guzzlehttp/ringphp": "^1.1",
26+
"php-http/adapter-integration-tests": "dev-master"
2727
},
2828
"provide": {
29-
"php-http/adapter-implementation": "0.1"
29+
"php-http/client-implementation": "1.0"
3030
},
3131
"autoload": {
3232
"psr-4": {
@@ -38,6 +38,10 @@
3838
"Http\\Adapter\\Tests\\": "tests/"
3939
}
4040
},
41+
"scripts": {
42+
"test": "vendor/bin/phpunit",
43+
"test-ci": "vendor/bin/phpunit --coverage-text --coverage-clover=build/coverage.xml"
44+
},
4145
"extra": {
4246
"branch-alias": {
4347
"dev-master": "0.2-dev"

src/Guzzle5HttpAdapter.php

Lines changed: 39 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,23 @@
11
<?php
22

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-
123
namespace Http\Adapter;
134

145
use GuzzleHttp\Client;
156
use GuzzleHttp\ClientInterface;
16-
use GuzzleHttp\Exception\RequestException;
177
use GuzzleHttp\Message\RequestInterface as GuzzleRequest;
188
use GuzzleHttp\Message\ResponseInterface as GuzzleResponse;
19-
use GuzzleHttp\Pool;
9+
use Http\Client\HttpClient;
2010
use Http\Discovery\MessageFactoryDiscovery;
2111
use Http\Message\MessageFactory;
2212
use Psr\Http\Message\RequestInterface;
2313
use Psr\Http\Message\ResponseInterface;
14+
use Http\Client\Exception as HttplugException;
15+
use GuzzleHttp\Exception as GuzzleExceptions;
2416

2517
/**
2618
* @author GeLo <[email protected]>
2719
*/
28-
class Guzzle5HttpAdapter implements HttpAdapter
20+
class Guzzle5HttpAdapter implements HttpClient
2921
{
3022
/**
3123
* @var ClientInterface
@@ -38,89 +30,48 @@ class Guzzle5HttpAdapter implements HttpAdapter
3830
private $messageFactory;
3931

4032
/**
41-
*
4233
* @param ClientInterface|null $client
4334
* @param MessageFactory|null $messageFactory
4435
*/
4536
public function __construct(ClientInterface $client = null, MessageFactory $messageFactory = null)
4637
{
4738
$this->client = $client ?: new Client();
4839
$this->messageFactory = $messageFactory ?: MessageFactoryDiscovery::find();
49-
5040
}
5141

5242
/**
5343
* {@inheritdoc}
5444
*/
55-
public function sendRequest(RequestInterface $request, array $options = [])
45+
public function sendRequest(RequestInterface $request)
5646
{
57-
$guzzleRequest = $this->createRequest($request, $options);
47+
$guzzleRequest = $this->createRequest($request);
5848

5949
try {
6050
$response = $this->client->send($guzzleRequest);
61-
} catch (RequestException $e) {
62-
throw $this->createException($e, $request);
51+
} catch (GuzzleExceptions\TransferException $e) {
52+
throw $this->handleException($e, $request);
6353
}
6454

6555
return $this->createResponse($response);
6656
}
6757

6858
/**
69-
* {@inheritdoc}
70-
*/
71-
public function sendRequests(array $requests, array $options = [])
72-
{
73-
$requests = array_values($requests);
74-
$guzzleRequests = [];
75-
76-
foreach ($requests as $request) {
77-
$guzzleRequests[] = $this->createRequest($request, $options);
78-
}
79-
80-
$results = Pool::batch($this->client, $guzzleRequests);
81-
82-
$exceptions = [];
83-
$responses = [];
84-
85-
foreach ($guzzleRequests as $key => $guzzleRequest) {
86-
$result = $results->getResult($guzzleRequest);
87-
88-
if ($result instanceof GuzzleResponse) {
89-
$responses[] = $this->createResponse($result);
90-
} elseif ($result instanceof RequestException) {
91-
$exceptions[] = $this->createException($result, $requests[$key]);
92-
}
93-
}
94-
95-
if (count($exceptions) > 0) {
96-
throw new Exception\MultiHttpAdapterException($exceptions, $responses);
97-
}
98-
99-
return $responses;
100-
}
101-
102-
/**
103-
* {@inheritdoc}
104-
*/
105-
public function getName()
106-
{
107-
return 'guzzle5';
108-
}
109-
110-
/**
111-
* Converts a PSR request into a Guzzle request
59+
* Converts a PSR request into a Guzzle request.
11260
*
11361
* @param RequestInterface $request
11462
*
11563
* @return GuzzleRequest
11664
*/
117-
private function createRequest(RequestInterface $request, array $options = [])
65+
private function createRequest(RequestInterface $request)
11866
{
119-
$options = $this->buildOptions($options);
67+
$options = [
68+
'exceptions' => false,
69+
'allow_redirects' => false,
70+
];
12071

12172
$options['version'] = $request->getProtocolVersion();
12273
$options['headers'] = $request->getHeaders();
123-
$options['body'] = (string) $request->getBody();
74+
$options['body'] = (string) $request->getBody();
12475

12576
return $this->client->createRequest(
12677
$request->getMethod(),
@@ -130,7 +81,7 @@ private function createRequest(RequestInterface $request, array $options = [])
13081
}
13182

13283
/**
133-
* Converts a Guzzle response into a PSR response
84+
* Converts a Guzzle response into a PSR response.
13485
*
13586
* @param GuzzleResponse $response
13687
*
@@ -143,60 +94,41 @@ private function createResponse(GuzzleResponse $response)
14394
return $this->messageFactory->createResponse(
14495
$response->getStatusCode(),
14596
null,
146-
$response->getProtocolVersion(),
14797
$response->getHeaders(),
148-
isset($body) ? $body->detach() : null
98+
isset($body) ? $body->detach() : null,
99+
$response->getProtocolVersion()
149100
);
150101
}
151102

152103
/**
153-
* Converts a Guzzle exception into an HttpAdapter exception
104+
* Converts a Guzzle exception into an Httplug exception.
154105
*
155-
* @param RequestException $exception
106+
* @param GuzzleExceptions\TransferException $exception
107+
* @param RequestInterface $request
156108
*
157-
* @return Exception\HttpAdapterException
109+
* @return HttplugException
158110
*/
159-
private function createException(
160-
RequestException $exception,
161-
RequestInterface $originalRequest
162-
) {
163-
$adapterException = new Exception\HttpAdapterException(
164-
$exception->getMessage(),
165-
0,
166-
$exception
167-
);
168-
169-
$response = null;
170-
171-
if ($exception->hasResponse()) {
172-
$response = $this->createResponse($exception->getResponse());
111+
private function handleException(GuzzleExceptions\TransferException $exception, RequestInterface $request)
112+
{
113+
if ($exception instanceof GuzzleExceptions\ConnectException) {
114+
return new HttplugException\NetworkException($exception->getMessage(), $request, $exception);
173115
}
174116

175-
$adapterException->setResponse($response);
176-
$adapterException->setRequest($originalRequest);
177-
178-
return $adapterException;
179-
}
180-
181-
/**
182-
* Builds options for Guzzle
183-
*
184-
* @param array $options
185-
*
186-
* @return array
187-
*/
188-
private function buildOptions(array $options)
189-
{
190-
$guzzleOptions = [
191-
'exceptions' => false,
192-
'allow_redirects' => false,
193-
];
117+
if ($exception instanceof GuzzleExceptions\RequestException) {
118+
// Make sure we have a response for the HttpException
119+
if ($exception->hasResponse()) {
120+
$psr7Response = $this->createResponse($exception->getResponse());
121+
return new HttplugException\HttpException(
122+
$exception->getMessage(),
123+
$request,
124+
$psr7Response,
125+
$exception
126+
);
127+
}
194128

195-
if (isset($options['timeout'])) {
196-
$guzzleOptions['connect_timeout'] = $options['timeout'];
197-
$guzzleOptions['timeout'] = $options['timeout'];
129+
return new HttplugException\RequestException($exception->getMessage(), $request, $exception);
198130
}
199131

200-
return $guzzleOptions;
132+
return new HttplugException\TransferException($exception->getMessage(), 0, $exception);
201133
}
202134
}

tests/Guzzle5HttpAdapterTest.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,13 @@
1313

1414
use GuzzleHttp\Client;
1515
use Http\Adapter\Guzzle5HttpAdapter;
16+
use Http\Client\Tests\HttpClientTest;
1617

1718
/**
1819
* @author GeLo <[email protected]>
1920
*/
20-
abstract class Guzzle5HttpAdapterTest extends HttpAdapterTest
21+
abstract class Guzzle5HttpAdapterTest extends HttpClientTest
2122
{
22-
public function testGetName()
23-
{
24-
$this->assertSame('guzzle5', $this->httpAdapter->getName());
25-
}
26-
2723
/**
2824
* {@inheritdoc}
2925
*/

0 commit comments

Comments
 (0)