Skip to content

Commit

Permalink
force whole response body to string conversion as in pull request ope…
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Klimes committed May 2, 2024
1 parent 0f755fa commit 1e7a34e
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/Transporters/HttpTransporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ public function requestObject(Payload $payload): Response

$response = $this->sendRequest(fn (): \Psr\Http\Message\ResponseInterface => $this->client->sendRequest($request));

$contents = $response->getBody()->getContents();
$contents = $this->getContents($response);

if (str_contains($response->getHeaderLine('Content-Type'), ContentType::TEXT_PLAIN->value)) {
return Response::from($contents, $response->getHeaders());
}

$this->throwIfJsonError($response, $contents);
$this->throwIfJsonError($response);

try {
/** @var array{error?: array{message: string, type: string, code: string}} $data */
Expand All @@ -75,9 +75,9 @@ public function requestContent(Payload $payload): string

$response = $this->sendRequest(fn (): \Psr\Http\Message\ResponseInterface => $this->client->sendRequest($request));

$contents = $response->getBody()->getContents();
$contents = $this->getContents($response);

$this->throwIfJsonError($response, $contents);
$this->throwIfJsonError($response);

return $contents;
}
Expand All @@ -91,7 +91,7 @@ public function requestStream(Payload $payload): ResponseInterface

$response = $this->sendRequest(fn () => ($this->streamHandler)($request));

$this->throwIfJsonError($response, $response);
$this->throwIfJsonError($response);

return $response;
}
Expand All @@ -102,14 +102,19 @@ private function sendRequest(Closure $callable): ResponseInterface
return $callable();
} catch (ClientExceptionInterface $clientException) {
if ($clientException instanceof ClientException) {
$this->throwIfJsonError($clientException->getResponse(), $clientException->getResponse()->getBody()->getContents());
$this->throwIfJsonError($clientException->getResponse());
}

throw new TransporterException($clientException);
}
}

private function throwIfJsonError(ResponseInterface $response, string|ResponseInterface $contents): void
private function getContents(ResponseInterface $response): string
{
return (string) $response->getBody();
}

private function throwIfJsonError(ResponseInterface $response): void
{
if ($response->getStatusCode() < 400) {
return;
Expand All @@ -119,9 +124,7 @@ private function throwIfJsonError(ResponseInterface $response, string|ResponseIn
return;
}

if ($contents instanceof ResponseInterface) {
$contents = $contents->getBody()->getContents();
}
$contents = $this->getContents($response);

try {
/** @var array{error?: array{message: string|array<int, string>, type: string, code: string}} $response */
Expand Down

0 comments on commit 1e7a34e

Please sign in to comment.