Conversation
Nyholm
left a comment
There was a problem hiding this comment.
Thank you for this PR.
Since 1.5.0 of php-http/message no stram factories rewind streams. We discussed and decided that the HTTP client should be responsible of rewinding the stream before the send a HTTP request.
Version 1.5 changed the behavior here when using Zend (not Guzzle). Should we help the user here by rewind the Response's Stream or should we say that the user is responsible for this? (e.g. use ->toStream() instead of ->getContents().)
|
IMO, cache plugin should not be responsible to rewind stream. If someone read the stream and know that other lib / class will use the stream, then this code should rewind it after it has read it. However that's the theory and in practice not everyone follow it so i can understand the need behind this PR. IMO we should simply create a RewindPlugin that does this job, and then we can put it / use it when we cannot trust another lib / client, WDYT ? |
|
Nevermind my comment i read thing too fast, i think it make sense here to rewind it |
| $response = $data['response']; | ||
| $response = $response->withBody($this->streamFactory->createStream($data['body'])); | ||
| $stream = $this->streamFactory->createStream($data['body']); | ||
| if ($stream->isSeekable()) { |
There was a problem hiding this comment.
if the stream is not seekable then it's not usable, what do you think about throwing an exception if the stream is not rewindable ?
There was a problem hiding this comment.
I can, what kind of exception would you like to throw in this situation? I'm thinking of an InvalidArgumentException, as it would be a configuration issue IMO.
There was a problem hiding this comment.
If we should thrown an exception we could skip the if ($stream->isSeekable()). Calling rewind on a non-seekable stream will throw a RuntimeException according to PSR7.
There was a problem hiding this comment.
👍 however we should still encapsulate it with a Httplug Exception to respect the contract
There was a problem hiding this comment.
Ping @theofidry Can you change this to
try {
$stream->rewind();
} catch (\Exception $e) {
throw new RewindStreamException('foo', 0, $e);
}And also create that exception?
There was a problem hiding this comment.
will try to do this weekend
6272312 to
ab338df
Compare
Nyholm
left a comment
There was a problem hiding this comment.
Thank you for the update. I had some minor comments
| @@ -0,0 +1,12 @@ | |||
| <?php | |||
|
|
|||
| declare(strict_types=1); | |||
There was a problem hiding this comment.
definitely not, fixed it
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Http\Client\Exception; |
There was a problem hiding this comment.
The namespace to src/ is Http\Client\Common\Plugin
But lets put this exception in Http\Client\Common\Plugin\Exception
| @@ -0,0 +1,12 @@ | |||
| <?php | |||
|
|
|||
| namespace Http\Client\Common\Plugin; | |||
There was a problem hiding this comment.
given how few classes we have i think its ok to have the exception in the main namespace. though the result when using this package together client-common will be that there is a lot of classes in this namespace: https://github.com/php-http/client-common/tree/master/src/Plugin
if we ever add exceptions for specific plugins in client-common we are likely to place them in an Exception sub namespace. for that reason i would prefer to also put this exception into a sub namespace Http\Client\Common\Plugin\Exception.
There was a problem hiding this comment.
Agreed. Putting the exception in Http\Client\Common\Plugin\Exception\RewindStreamException is preferred.
|
Thank you @theofidry. Sorry for the confusion. I will tag a new release shortly. |
One may consume a response body by doing the following:
When the
$responsecomes from the cache, this no longer works because someStreamFactoryimplementations, e.g.DiactorosStreamFactory, don't rewind the stream. As a result, the code above will result in an empty string if the response comes from the cache.