This repository has been archived by the owner on Jan 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae43658
commit 2b7632a
Showing
5 changed files
with
255 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,171 @@ | ||
<?php | ||
|
||
/* | ||
* 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\ClientInterface; | ||
use GuzzleHttp\Event\CompleteEvent; | ||
use GuzzleHttp\Event\ErrorEvent; | ||
use GuzzleHttp\Message\RequestInterface; | ||
use GuzzleHttp\Pool; | ||
use Http\Adapter\Message\InternalRequestInterface; | ||
use Http\Adapter\Normalizer\BodyNormalizer; | ||
|
||
/** | ||
* @author GeLo <[email protected]> | ||
*/ | ||
class Guzzle5HttpAdapter extends CurlHttpAdapter | ||
{ | ||
/** | ||
* @var ClientInterface | ||
*/ | ||
private $client; | ||
|
||
/** | ||
* | ||
* @param ClientInterface|null $client | ||
* @param ConfigurationInterface|null $configuration | ||
*/ | ||
public function __construct(ClientInterface $client = null, ConfigurationInterface $configuration = null) | ||
{ | ||
parent::__construct($configuration); | ||
|
||
$this->client = $client ?: new Client(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return 'guzzle5'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function sendInternalRequest(InternalRequestInterface $internalRequest) | ||
{ | ||
try { | ||
$response = $this->client->send($this->createRequest($internalRequest)); | ||
} catch (\Exception $e) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
egeloen
Contributor
|
||
var_dump($e); exit; | ||
throw HttpAdapterException::cannotFetchUri( | ||
$e->getRequest()->getUrl(), | ||
$this->getName(), | ||
$e->getMessage() | ||
); | ||
} | ||
|
||
return $this->getConfiguration()->getMessageFactory()->createResponse( | ||
(integer) $response->getStatusCode(), | ||
$response->getProtocolVersion(), | ||
$response->getHeaders(), | ||
BodyNormalizer::normalize( | ||
function () use ($response) { | ||
return $response->getBody()->detach(); | ||
}, | ||
$internalRequest->getMethod() | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function sendInternalRequests(array $internalRequests, $success, $error) | ||
{ | ||
$requests = []; | ||
foreach ($internalRequests as $internalRequest) { | ||
$requests[] = $this->createRequest($internalRequest, $success, $error); | ||
} | ||
|
||
Pool::batch($this->client, $requests); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function createFile($file) | ||
{ | ||
return fopen($file, 'r'); | ||
} | ||
|
||
/** | ||
* Creates a request. | ||
* | ||
* @param InternalRequestInterface $internalRequest | ||
* @param callable|null $success | ||
* @param callable|null $error | ||
* | ||
* @return RequestInterface | ||
*/ | ||
private function createRequest(InternalRequestInterface $internalRequest, callable $success = null, callable $error = null) | ||
{ | ||
$request = $this->client->createRequest( | ||
$internalRequest->getMethod(), | ||
(string) $internalRequest->getUri(), | ||
[ | ||
'exceptions' => false, | ||
'allow_redirects' => false, | ||
'timeout' => $this->getConfiguration()->getTimeout(), | ||
'connect_timeout' => $this->getConfiguration()->getTimeout(), | ||
'version' => $internalRequest->getProtocolVersion(), | ||
'headers' => $this->prepareHeaders($internalRequest), | ||
'body' => $this->prepareContent($internalRequest), | ||
] | ||
); | ||
|
||
if (isset($success)) { | ||
$messageFactory = $this->getConfiguration()->getMessageFactory(); | ||
|
||
$request->getEmitter()->on( | ||
'complete', | ||
function (CompleteEvent $event) use ($success, $internalRequest, $messageFactory) { | ||
$response = $messageFactory->createResponse( | ||
(integer) $event->getResponse()->getStatusCode(), | ||
$event->getResponse()->getProtocolVersion(), | ||
$event->getResponse()->getHeaders(), | ||
BodyNormalizer::normalize( | ||
function () use ($event) { | ||
return $event->getResponse()->getBody()->detach(); | ||
}, | ||
$internalRequest->getMethod() | ||
) | ||
); | ||
|
||
$response = $response->withParameter('request', $internalRequest); | ||
call_user_func($success, $response); | ||
} | ||
); | ||
} | ||
|
||
if (isset($error)) { | ||
$httpAdapterName = $this->getName(); | ||
|
||
$request->getEmitter()->on( | ||
'error', | ||
function (ErrorEvent $event) use ($error, $internalRequest, $httpAdapterName) { | ||
$exception = HttpAdapterException::cannotFetchUri( | ||
$event->getException()->getRequest()->getUrl(), | ||
$httpAdapterName, | ||
$event->getException()->getMessage() | ||
); | ||
$exception->setRequest($internalRequest); | ||
call_user_func($error, $exception); | ||
} | ||
); | ||
} | ||
|
||
return $request; | ||
} | ||
} |
This file contains 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,43 @@ | ||
<?php | ||
|
||
/* | ||
* 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\Tests; | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Ring\Client\CurlHandler; | ||
use Http\Adapter\Guzzle5HttpAdapter; | ||
|
||
/** | ||
* @author GeLo <[email protected]> | ||
*/ | ||
class Guzzle5HttpAdapterTest extends HttpAdapterTest | ||
{ | ||
public function testGetName() | ||
{ | ||
$this->assertSame('guzzle5', $this->httpAdapter->getName()); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function createHttpAdapter() | ||
{ | ||
return new Guzzle5HttpAdapter(new Client(['handler' => $this->createHandler()])); | ||
} | ||
|
||
/** | ||
* Returns a handler for the client | ||
*/ | ||
protected function createHandler() | ||
{ | ||
return new CurlHandler(); | ||
} | ||
} |
This file contains 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,28 @@ | ||
<?php | ||
|
||
/* | ||
* 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\Tests; | ||
|
||
use GuzzleHttp\Ring\Client\CurlMultiHandler; | ||
|
||
/** | ||
* @author GeLo <[email protected]> | ||
*/ | ||
class Guzzle5MultiHttpAdapterTest extends Guzzle5HttpAdapterTest | ||
{ | ||
/** | ||
* Returns a handler for the client | ||
*/ | ||
protected function createHandler() | ||
{ | ||
return new CurlMultiHandler(); | ||
} | ||
} |
Shouldn't we typehint against a specific exception which we can make sure that has a
getRequest
method?