8000 bug #36773 [HttpClient] preserve the identity of responses streamed b… · symfony/symfony@1de42a5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1de42a5

Browse files
committed
bug #36773 [HttpClient] preserve the identity of responses streamed by TraceableHttpClient (nicolas-grekas)
This PR was merged into the 5.1-dev branch. Discussion ---------- [HttpClient] preserve the identity of responses streamed by TraceableHttpClient | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Commits ------- afc44da [HttpClient] preserve the identity of responses streamed by TraceableHttpClient
2 parents 3acc28f + afc44da commit 1de42a5

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

src/Symfony/Component/HttpClient/Response/TraceableResponse.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\HttpClient\Exception\ClientException;
1515
use Symfony\Component\HttpClient\Exception\RedirectionException;
1616
use Symfony\Component\HttpClient\Exception\ServerException;
17+
use Symfony\Component\HttpClient\TraceableHttpClient;
1718
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
1819
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
1920
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
@@ -105,6 +106,28 @@ public function toStream(bool $throw = true)
105106
return StreamWrapper::createResource($this->response, $this->client);
106107
}
107108

109+
/**
110+
* @internal
111+
*/
112+
public static function stream(HttpClientInterface $client, iterab 10000 le $responses, ?float $timeout): \Generator
113+
{
114+
$wrappedResponses = [];
115+
$traceableMap = new \SplObjectStorage();
116+
117+
foreach ($responses as $r) {
118+
if (!$r instanceof self) {
119+
throw new \TypeError(sprintf('"%s::stream()" expects parameter 1 to be an iterable of TraceableResponse objects, "%s" given.', TraceableHttpClient::class, get_debug_type($r)));
120+
}
121+
122+
$traceableMap[$r->response] = $r;
123+
$wrappedResponses[] = $r->response;
124+
}
125+
126+
foreach ($client->stream($wrappedResponses, $timeout) as $r => $chunk) {
127+
yield $traceableMap[$r] => $chunk;
128+
}
129+
}
130+
108131
private function checkStatusCode($code)
109132
{
110133
if (500 <= $code) {

src/Symfony/Component/HttpClient/Tests/TraceableHttpClientTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,12 @@ public function testStream()
8888
TestHttpServer::start();
8989

9090
$sut = new TraceableHttpClient(new NativeHttpClient());
91-
$chunked = $sut->request('GET', 'http://localhost:8057/chunked');
91+
$response = $sut->request('GET', 'http://localhost:8057/chunked');
9292
$chunks = [];
93-
foreach ($sut->stream($chunked) as $response) {
94-
$chunks[] = $response->getContent();
93+
foreach ($sut->stream($response) as $r => $chunk) {
94+
$chunks[] = $chunk->getContent();
9595
}
96+
$this->assertSame($response, $r);
9697
$this->assertGreaterThan(1, \count($chunks));
9798
$this->assertSame('Symfony is awesome!', implode('', $chunks));
9899
}

src/Symfony/Component/HttpClient/TraceableHttpClient.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Psr\Log\LoggerAwareInterface;
1515
use Psr\Log\LoggerInterface;
16+
use Symfony\Component\HttpClient\Response\ResponseStream;
1617
use Symfony\Component\HttpClient\Response\TraceableResponse;
1718
use Symfony\Contracts\HttpClient\HttpClientInterface;
1819
use Symfony\Contracts\HttpClient\ResponseInterface;
@@ -70,15 +71,7 @@ public function stream($responses, float $timeout = null): ResponseStreamInterfa
7071
throw new \TypeError(sprintf('"%s()" expects parameter 1 to be an iterable of TraceableResponse objects, "%s" given.', __METHOD__, get_debug_type($responses)));
7172
}
7273

73-
return $this->client->stream(\Closure::bind(static function () use ($responses) {
74-
foreach ($responses as $k => $r) {
75-
if (!$r instanceof TraceableResponse) {
76-
throw new \TypeError(sprintf('"%s()" expects parameter 1 to be an iterable of TraceableResponse objects, "%s" given.', __METHOD__, get_debug_type($r)));
77-
}
78-
79-
yield $k => $r->response;
80-
}
81-
}, null, TraceableResponse::class)(), $timeout);
74+
return new ResponseStream(TraceableResponse::stream($this->client, $responses, $timeout));
8275
}
8376

8477
public function getTracedRequests(): array

0 commit comments

Comments
 (0)
0