From db728eaa3864012aa8e375c03f3bb9c8ab647c0e Mon Sep 17 00:00:00 2001 From: Martin Hoch Date: Tue, 27 Feb 2024 09:48:11 +0100 Subject: [PATCH] Fix Content retrieval in HttpTransport getContent does not return the content of the body but the remaining content of the body. If you were to OpenAI::factory withHttpClient and i.e. a logging middlewere getContent would return an empty string since the content was already read by the log middleware. See: https://www.php-fig.org/psr/psr-7/ https://stackoverflow.com/questions/30549226/guzzlehttp-how-get-the-body-of-a-response-from-guzzle-6 --- src/Transporters/HttpTransporter.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Transporters/HttpTransporter.php b/src/Transporters/HttpTransporter.php index 37ed0d7c..e2e067e6 100644 --- a/src/Transporters/HttpTransporter.php +++ b/src/Transporters/HttpTransporter.php @@ -48,7 +48,7 @@ public function requestObject(Payload $payload): Response $response = $this->sendRequest(fn (): \Psr\Http\Message\ResponseInterface => $this->client->sendRequest($request)); - $contents = $response->getBody()->getContents(); + $contents = (string) $response->getBody(); if (str_contains($response->getHeaderLine('Content-Type'), ContentType::TEXT_PLAIN->value)) { return Response::from($contents, $response->getHeaders()); @@ -75,7 +75,7 @@ public function requestContent(Payload $payload): string $response = $this->sendRequest(fn (): \Psr\Http\Message\ResponseInterface => $this->client->sendRequest($request)); - $contents = $response->getBody()->getContents(); + $contents = (string) $response->getBody(); $this->throwIfJsonError($response, $contents); @@ -102,7 +102,7 @@ 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(), (string) $clientException->getResponse()->getBody()); } throw new TransporterException($clientException); @@ -120,7 +120,7 @@ private function throwIfJsonError(ResponseInterface $response, string|ResponseIn } if ($contents instanceof ResponseInterface) { - $contents = $contents->getBody()->getContents(); + $contents = (string) $contents->getBody(); } try {