This repository was archived by the owner on Jan 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
[WIP] Implement Guzzle 6 adapter #1
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,13 +6,14 @@ | |
"homepage": "http://php-http.org", | ||
"authors": [ | ||
{ | ||
"name": "Eric GELOEN", | ||
"email": "[email protected]" | ||
"name": "David de Boer", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.4", | ||
"php-http/adapter-core": "dev-internal_separation" | ||
"php-http/adapter-core": "dev-internal_separation", | ||
"guzzlehttp/guzzle": "6.x-dev" | ||
}, | ||
"require-dev": { | ||
"ext-curl": "*", | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing file header, check guzzle5-adapter There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added. |
||
/** | ||
* This file is part of the Http Adapter package. | ||
* | ||
* (c) Eric GELOEN <[email protected]> | ||
* | ||
* For the full copyright and license information, please read the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Http\Adapter; | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Exception\RequestException; | ||
use GuzzleHttp\Pool; | ||
use Http\Adapter\Common\Exception\HttpAdapterException; | ||
use Http\Adapter\Common\Exception\MultiHttpAdapterException; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
/** | ||
* Guzzle 6 HTTP adapter | ||
* | ||
* @author David de Boer <[email protected]> | ||
*/ | ||
class Guzzle6HttpAdapter implements PsrHttpAdapter | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add author information here |
||
{ | ||
/** | ||
* @param Client $client | ||
*/ | ||
public function __construct(Client $client = null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add docblock without a description but with the parameter. |
||
{ | ||
$this->client = $client ?: new Client(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function sendRequest(RequestInterface $request) | ||
{ | ||
try { | ||
return $this->client->send($request); | ||
} catch (RequestException $exception) { | ||
throw new $this->createException($exception); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function sendRequests(array $requests) | ||
{ | ||
$results = Pool::batch( | ||
$this->client, | ||
$requests | ||
); | ||
|
||
$exceptions = []; | ||
foreach ($results as $result) { | ||
if ($result instanceof TransferException) { | ||
$exceptions[] = $this->createException($result); | ||
} | ||
} | ||
|
||
if (count($exceptions) > 0) { | ||
throw new MultiHttpAdapterException($exceptions); | ||
} | ||
|
||
return $results; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return 'guzzle6'; | ||
} | ||
|
||
/** | ||
* Convert Guzzle exception into HttpAdapter exception | ||
* | ||
* @param RequestException $exception | ||
* | ||
* @return HttpAdapterException | ||
*/ | ||
private function createException(RequestException $exception) | ||
{ | ||
$adapterException = new HttpAdapterException( | ||
$exception->getMessage(), | ||
0, | ||
$exception | ||
); | ||
$adapterException->setResponse($exception->getResponse()); | ||
$adapterException->setRequest($exception->getRequest()); | ||
|
||
return $adapterException; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be changed to
~6.0
when Guzzle 6 has been released.