From 188ecb32d25b440e8f58dde8724724bd5b6b586a Mon Sep 17 00:00:00 2001 From: Thomas Ploch Date: Wed, 18 Jul 2018 14:19:31 +0200 Subject: [PATCH] [FX] Use correct way of retrieving the contents of a Guzzle stream --- src/Registry/PromisingRegistry.php | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/Registry/PromisingRegistry.php b/src/Registry/PromisingRegistry.php index cb54fb1..8a9ef17 100644 --- a/src/Registry/PromisingRegistry.php +++ b/src/Registry/PromisingRegistry.php @@ -52,7 +52,7 @@ public function register(string $subject, AvroSchema $schema, callable $requestC $request = registerNewSchemaVersionWithSubjectRequest((string) $schema, $subject); $onFulfilled = function (ResponseInterface $response) { - $schemaId = \GuzzleHttp\json_decode($response->getBody()->getContents(), true)['id']; + $schemaId = $this->getJsonFromResponseBody($response)['id']; return $schemaId; }; @@ -70,7 +70,7 @@ public function schemaId(string $subject, AvroSchema $schema, callable $requestC $request = checkIfSubjectHasSchemaRegisteredRequest($subject, (string) $schema); $onFulfilled = function (ResponseInterface $response) { - $decodedResponse = \GuzzleHttp\json_decode($response->getBody()->getContents(), true); + $decodedResponse = $this->getJsonFromResponseBody($response); return $decodedResponse['id']; }; @@ -89,7 +89,7 @@ public function schemaForId(int $schemaId, callable $requestCallback = null): Pr $onFulfilled = function (ResponseInterface $response) { $schema = AvroSchema::parse( - \GuzzleHttp\json_decode($response->getBody()->getContents(), true)['schema'] + $this->getJsonFromResponseBody($response)['schema'] ); return $schema; @@ -109,7 +109,7 @@ public function schemaForSubjectAndVersion(string $subject, int $version, callab $onFulfilled = function (ResponseInterface $response) { $schema = AvroSchema::parse( - \GuzzleHttp\json_decode($response->getBody()->getContents(), true)['schema'] + $this->getJsonFromResponseBody($response)['schema'] ); return $schema; @@ -128,7 +128,7 @@ public function schemaVersion(string $subject, AvroSchema $schema, callable $req $request = checkIfSubjectHasSchemaRegisteredRequest($subject, (string) $schema); $onFulfilled = function (ResponseInterface $response) { - return \GuzzleHttp\json_decode($response->getBody()->getContents(), true)['version']; + return $this->getJsonFromResponseBody($response)['version']; }; return $this->makeRequest($request, $onFulfilled, $requestCallback); @@ -145,7 +145,7 @@ public function latestVersion(string $subject, callable $requestCallback = null) $onFulfilled = function (ResponseInterface $response) { $schema = AvroSchema::parse( - \GuzzleHttp\json_decode($response->getBody()->getContents(), true)['schema'] + $this->getJsonFromResponseBody($response)['schema'] ); return $schema; @@ -167,4 +167,19 @@ private function makeRequest(RequestInterface $request, callable $onFulfilled, c ->sendAsync(null !== $requestCallback ? $requestCallback($request) : $request) ->then($onFulfilled, $this->rejectedCallback); } + + private function getJsonFromResponseBody(ResponseInterface $response): array + { + $body = (string) $response->getBody(); + + try { + return \GuzzleHttp\json_decode($body, true); + } catch (\InvalidArgumentException $e) { + throw new \InvalidArgumentException( + \sprintf('%s - with content "%s"', $e->getMessage(), $body), + $e->getCode(), + $e + ); + } + } }