Skip to content

Commit

Permalink
Merge pull request #18 from shulard/issue/17
Browse files Browse the repository at this point in the history
Fix react/http-client 0.4.13 response body issue
  • Loading branch information
shulard authored Oct 31, 2016
2 parents 6bf127c + 6707576 commit 104f3c4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
24 changes: 15 additions & 9 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -65,6 +74,7 @@ public function __construct(
$this->client = $client ?: ReactFactory::buildHttpClient($this->loop);

$this->responseFactory = $responseFactory ?: MessageFactoryDiscovery::find();
$this->streamFactory = $streamFactory ?: StreamFactoryDiscovery::find();
}

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit 104f3c4

Please sign in to comment.