Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #35407 [HttpClient] collect the body of responses when possib…
…le (nicolas-grekas) This PR was merged into the 5.1-dev branch. Discussion ---------- [HttpClient] collect the body of responses when possible | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Part of #33311 | License | MIT | Doc PR | - This is missing one thing: the HTML part in the profiler. ![image](https://user-images.githubusercontent.com/243674/72798816-29813e00-3c44-11ea-9586-99c2c6b91640.png) ![image](https://user-images.githubusercontent.com/243674/72798851-3f8efe80-3c44-11ea-973b-7ecc64a5a542.png) Commits ------- 121f728 [HttpClient] collect the body of responses when possible
- Loading branch information
Showing
9 changed files
with
172 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
src/Symfony/Component/HttpClient/Response/TraceableResponse.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpClient\Response; | ||
|
||
use Symfony\Component\HttpClient\Exception\ClientException; | ||
use Symfony\Component\HttpClient\Exception\RedirectionException; | ||
use Symfony\Component\HttpClient\Exception\ServerException; | ||
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; | ||
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; | ||
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; | ||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
use Symfony\Contracts\HttpClient\ResponseInterface; | ||
|
||
/** | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
* | ||
* @internal | ||
*/ | ||
class TraceableResponse implements ResponseInterface | ||
{ | ||
private $client; | ||
private $response; | ||
private $content; | ||
|
||
public function __construct(HttpClientInterface $client, ResponseInterface $response, &$content) | ||
{ | ||
$this->client = $client; | ||
$this->response = $response; | ||
$this->content = &$content; | ||
} | ||
|
||
public function getStatusCode(): int | ||
{ | ||
return $this->response->getStatusCode(); | ||
} | ||
|
||
public function getHeaders(bool $throw = true): array | ||
{ | ||
return $this->response->getHeaders($throw); | ||
} | ||
|
||
public function getContent(bool $throw = true): string | ||
{ | ||
$this->content = $this->response->getContent(false); | ||
|
||
if ($throw) { | ||
$this->checkStatusCode($this->response->getStatusCode()); | ||
} | ||
|
||
return $this->content; | ||
} | ||
|
||
public function toArray(bool $throw = true): array | ||
{ | ||
$this->content = $this->response->toArray(false); | ||
|
||
if ($throw) { | ||
$this->checkStatusCode($this->response->getStatusCode()); | ||
} | ||
|
||
return $this->content; | ||
} | ||
|
||
public function cancel(): void | ||
{ | ||
$this->response->cancel(); | ||
} | ||
|
||
public function getInfo(string $type = null) | ||
{ | ||
return $this->response->getInfo($type); | ||
} | ||
|
||
/** | ||
* Casts the response to a PHP stream resource. | ||
* | ||
* @return resource | ||
* | ||
* @throws TransportExceptionInterface When a network error occurs | ||
* @throws RedirectionExceptionInterface On a 3xx when $throw is true and the "max_redirects" option has been reached | ||
* @throws ClientExceptionInterface On a 4xx when $throw is true | ||
* @throws ServerExceptionInterface On a 5xx when $throw is true | ||
*/ | ||
public function toStream(bool $throw = true) | ||
{ | ||
if ($throw) { | ||
// Ensure headers arrived | ||
$this->response->getHeaders(true); | ||
} | ||
|
||
if (\is_callable([$this->response, 'toStream'])) { | ||
return $this->response->toStream(false); | ||
} | ||
|
||
return StreamWrapper::createResource($this->response, $this->client); | ||
} | ||
|
||
private function checkStatusCode($code) | ||
{ | ||
if (500 <= $code) { | ||
throw new ServerException($this); | ||
} | ||
|
||
if (400 <= $code) { | ||
throw new ClientException($this); | ||
} | ||
|
||
if (300 <= $code) { | ||
throw new RedirectionException($this); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters