Skip to content

Commit 9168aad

Browse files
authored
Move StreamableBody to AsyncAws\Core\Stream (#348)
* Move StreamableBody to AsyncAws\Core\Stream * Rebase fix * Bugfix * minor
1 parent 9697776 commit 9168aad

20 files changed

+209
-155
lines changed

src/Request.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace AsyncAws\Core;
44

55
use AsyncAws\Core\Exception\LogicException;
6-
use AsyncAws\Core\Stream\Stream;
6+
use AsyncAws\Core\Stream\RequestStream;
77

88
/**
99
* Representation of an HTTP Request.
@@ -31,7 +31,7 @@ class Request
3131
/**
3232
* @param string[]|string[][] $headers
3333
*/
34-
public function __construct(string $method, string $uri, array $query, array $headers, Stream $body)
34+
public function __construct(string $method, string $uri, array $query, array $headers, RequestStream $body)
3535
{
3636
$this->method = $method;
3737
$this->uri = $uri;
@@ -87,12 +87,12 @@ public function removeHeader(string $name): void
8787
unset($this->headers[strtolower($name)]);
8888
}
8989

90-
public function getBody(): Stream
90+
public function getBody(): RequestStream
9191
{
9292
return $this->body;
9393
}
9494

95-
public function setBody(Stream $body)
95+
public function setBody(RequestStream $body)
9696
{
9797
$this->body = $body;
9898
}

src/Response.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use AsyncAws\Core\Exception\Http\RedirectionException;
1111
use AsyncAws\Core\Exception\Http\ServerException;
1212
use AsyncAws\Core\Exception\RuntimeException;
13+
use AsyncAws\Core\Stream\ResponseBodyStream;
14+
use AsyncAws\Core\Stream\ResultStream;
1315
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
1416
use Symfony\Contracts\HttpClient\HttpClientInterface;
1517
use Symfony\Contracts\HttpClient\ResponseInterface;
@@ -151,8 +153,8 @@ public function getStatusCode(): int
151153
return $this->response->getStatusCode();
152154
}
153155

154-
public function toStream(): StreamableBody
156+
public function toStream(): ResultStream
155157
{
156-
return new StreamableBody($this->httpClient->stream($this->response));
158+
return new ResponseBodyStream($this->httpClient->stream($this->response));
157159
}
158160
}

src/Signer/SignerV4.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use AsyncAws\Core\RequestContext;
99
use AsyncAws\Core\Stream\FixedSizeStream;
1010
use AsyncAws\Core\Stream\IterableStream;
11-
use AsyncAws\Core\Stream\Stream;
11+
use AsyncAws\Core\Stream\RequestStream;
1212
use AsyncAws\Core\Stream\StringStream;
1313

1414
/**
@@ -263,7 +263,7 @@ private function convertBodyToStream(Request $request, \DateTimeInterface $now,
263263
$metaLength = \strlen(";chunk-signature=\r\n\r\n") + 64;
264264
$request->setHeader('content-length', $contentLength + $fullChunkCount * ($metaLength + \strlen((string) dechex(self::CHUNK_SIZE))) + ($chunkCount - $fullChunkCount) * ($metaLength + \strlen((string) dechex($contentLength % self::CHUNK_SIZE))) + $metaLength + 1);
265265

266-
$body = IterableStream::create((function (Stream $body) use ($now, $credentialString, $signingKey, &$signature): iterable {
266+
$body = IterableStream::create((function (RequestStream $body) use ($now, $credentialString, $signingKey, &$signature): iterable {
267267
foreach (FixedSizeStream::create($body, self::CHUNK_SIZE) as $chunk) {
268268
$stringToSign = $this->buildChunkStringToSign($now, $credentialString, $signature, $chunk);
269269
$signature = $this->buildSignature($stringToSign, $signingKey);

src/Stream/CallableStream.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*
1313
* @author Jérémy Derussé <[email protected]>
1414
*/
15-
final class CallableStream implements Stream
15+
final class CallableStream implements RequestStream
1616
{
1717
private $content;
1818

src/Stream/FixedSizeStream.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
*
1212
* @author Jérémy Derussé <[email protected]>
1313
*/
14-
final class FixedSizeStream implements Stream
14+
final class FixedSizeStream implements RequestStream
1515
{
1616
private $content;
1717

1818
private $chunkSize;
1919

20-
private function __construct(Stream $content, int $chunkSize = 64 * 1024)
20+
private function __construct(RequestStream $content, int $chunkSize = 64 * 1024)
2121
{
2222
$this->content = $content;
2323
$this->chunkSize = $chunkSize;
@@ -28,11 +28,11 @@ public static function create($content, int $chunkSize = 64 * 1024): FixedSizeSt
2828
if ($content instanceof self) {
2929
return $content;
3030
}
31-
if ($content instanceof Stream) {
31+
if ($content instanceof RequestStream) {
3232
return new self($content, $chunkSize);
3333
}
3434

35-
throw new InvalidArgument(sprintf('Expect content to be an instance of "%s". "%s" given.', Stream::class, \is_object($content) ? \get_class($content) : \gettype($content)));
35+
throw new InvalidArgument(sprintf('Expect content to be an instance of "%s". "%s" given.', RequestStream::class, \is_object($content) ? \get_class($content) : \gettype($content)));
3636
}
3737

3838
public function length(): ?int

src/Stream/IterableStream.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
* @author Jérémy Derussé <[email protected]>
1313
*/
14-
final class IterableStream implements Stream
14+
final class IterableStream implements RequestStream
1515
{
1616
private $content;
1717

src/Stream/RequestStream.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace AsyncAws\Core\Stream;
4+
5+
/**
6+
* Provides method to convert a input into string or chunks.
7+
*
8+
* @internal
9+
*
10+
* @author Jérémy Derussé <[email protected]>
11+
*/
12+
interface RequestStream extends \IteratorAggregate
13+
{
14+
/**
15+
* Length in bytes.
16+
*/
17+
public function length(): ?int;
18+
19+
public function stringify(): string;
20+
21+
public function hash(string $algo = 'sha256', bool $raw = false): string;
22+
}

src/Stream/ResourceStream.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
* @author Jérémy Derussé <[email protected]>
1313
*/
14-
final class ResourceStream implements Stream
14+
final class ResourceStream implements RequestStream
1515
{
1616
/**
1717
* @var resource

src/Stream/ResponseBodyStream.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AsyncAws\Core\Stream;
6+
7+
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
8+
9+
/**
10+
* Stream a HTTP response body.
11+
*
12+
* @author Tobias Nyholm <[email protected]>
13+
*/
14+
class ResponseBodyStream implements ResultStream
15+
{
16+
/**
17+
* @var ResponseStreamInterface
18+
*/
19+
private $responseStream;
20+
21+
public function __construct(ResponseStreamInterface $responseStream)
22+
{
23+
$this->responseStream = $responseStream;
24+
}
25+
26+
public function __toString()
27+
{
28+
return $this->getContentAsString();
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
public function getChunks(): iterable
35+
{
36+
foreach ($this->responseStream as $chunk) {
37+
yield $chunk->getContent();
38+
}
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public function getContentAsString(): string
45+
{
46+
$resource = $this->getContentAsResource();
47+
48+
try {
49+
return \stream_get_contents($resource);
50+
} finally {
51+
\fclose($resource);
52+
}
53+
}
54+
55+
/**
56+
* {@inheritdoc}
57+
*/
58+
public function getContentAsResource()
59+
{
60+
$resource = \fopen('php://temp', 'rw+');
61+
62+
try {
63+
foreach ($this->responseStream as $chunk) {
64+
fwrite($resource, $chunk->getContent());
65+
}
66+
67+
// Rewind
68+
\fseek($resource, 0, \SEEK_SET);
69+
70+
return $resource;
71+
} catch (\Throwable $e) {
72+
\fclose($resource);
73+
74+
throw $e;
75+
}
76+
}
77+
}

src/Stream/ResultStream.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AsyncAws\Core\Stream;
6+
7+
use AsyncAws\Core\StreamableBodyInterface;
8+
9+
/**
10+
* Stream for a Result.
11+
*/
12+
interface ResultStream extends StreamableBodyInterface
13+
{
14+
}

0 commit comments

Comments
 (0)