Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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