Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions src/Registry/PromisingRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand All @@ -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'];
};
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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
);
}
}
}