diff --git a/CHANGELOG.md b/CHANGELOG.md index 0075387..d76124f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,16 @@ # Change Log +## [Unreleased] + +### Fixed + +- Issue with `react/http-client` v0.4.13 about body handling as StreamInterface. +This change was introduce in https://github.com/reactphp/http-client/pull/66. + +### Changed + +- Client now require a Stream factory to handle body properly. + ## 0.2.2 - 2016-07-18 diff --git a/composer.json b/composer.json index 5a11461..7a4e27a 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,8 @@ "php-http/discovery": "^1.0" }, "require-dev": { - "php-http/client-integration-tests": "^0.5.1" + "php-http/client-integration-tests": "^0.5.1", + "php-http/message": "^1.0" }, "autoload": { "psr-4": { diff --git a/src/Client.php b/src/Client.php index 3a26b26..628ef55 100644 --- a/src/Client.php +++ b/src/Client.php @@ -7,7 +7,9 @@ use Http\Client\Exception\HttpException; use Http\Client\Exception\RequestException; use Http\Discovery\MessageFactoryDiscovery; +use Http\Discovery\StreamFactoryDiscovery; use Http\Message\ResponseFactory; +use Http\Message\StreamFactory; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\StreamInterface; @@ -43,17 +45,24 @@ class Client implements HttpClient, HttpAsyncClient */ private $responseFactory; + /** + * @var StreamFactory + */ + private $streamFactory; + /** * Initialize the React client. * * @param ResponseFactory|null $responseFactory * @param LoopInterface|null $loop * @param ReactClient|null $client + * @param StreamFactory|null $streamFactory */ public function __construct( ResponseFactory $responseFactory = null, LoopInterface $loop = null, - ReactClient $client = null + ReactClient $client = null, + StreamFactory $streamFactory = null ) { if (null !== $client && null === $loop) { throw new \RuntimeException( @@ -65,6 +74,7 @@ public function __construct( $this->client = $client ?: ReactFactory::buildHttpClient($this->loop); $this->responseFactory = $responseFactory ?: MessageFactoryDiscovery::find(); + $this->streamFactory = $streamFactory ?: StreamFactoryDiscovery::find(); } /** @@ -94,17 +104,12 @@ public function sendAsyncRequest(RequestInterface $request) }); $reactRequest->on('response', function (ReactResponse $reactResponse = null) use ($deferred, $reactRequest, $request) { - $bodyStream = null; + $bodyStream = $this->streamFactory->createStream(); $reactResponse->on('data', function ($data) use (&$bodyStream) { - if ($data instanceof StreamInterface) { - $bodyStream = $data; - } else { - $bodyStream->write($data); - } + $bodyStream->write((string) $data); }); $reactResponse->on('end', function (\Exception $error = null) use ($deferred, $request, $reactResponse, &$bodyStream) { - $bodyStream->rewind(); $response = $this->buildResponse( $reactResponse, $bodyStream @@ -158,7 +163,8 @@ private function buildReactRequest(RequestInterface $request) /** * Transform a React Response to a valid PSR7 ResponseInterface instance. * - * @param ReactResponse $response + * @param ReactResponse $response + * @param StreamInterface $body * * @return ResponseInterface */