Skip to content

Commit

Permalink
Add support for guzzle 7 (#64)
Browse files Browse the repository at this point in the history
Once the UriTemplate class was heavily used and it had been removed from
Guzzle 7 it was not possible to add support for the next guzzle major
version.

Replace usages of UriTemplate with sprintf calls and update
composer.json with support for guzzle 7.

Fixes #57
Fixes #55
  • Loading branch information
xico42 authored Sep 22, 2021
1 parent d9c4dec commit ad1a796
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 26 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
"php": "^7.3|^8.0",
"ext-curl": "*",
"ext-json": "*",
"guzzlehttp/guzzle": "~6.3",
"guzzlehttp/guzzle": "~6.3|^7.0",
"guzzlehttp/promises": "^1.4.0",
"guzzlehttp/psr7": "<1.7",
"guzzlehttp/psr7": "^1.7",
"beberlei/assert": "~2.7|~3.0",
"flix-tech/avro-php": "^4.1"
},
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/ExceptionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private function guardAgainstMissingErrorCode(ResponseInterface $response): arra
try {
$decodedBody = \GuzzleHttp\json_decode((string) $response->getBody(), true);

if (!array_key_exists(self::ERROR_CODE_FIELD_NAME, $decodedBody)) {
if (!is_array($decodedBody) || !array_key_exists(self::ERROR_CODE_FIELD_NAME, $decodedBody)) {
throw new RuntimeException(
sprintf(
'Invalid message body received - cannot find "error_code" field in response body "%s"',
Expand Down
10 changes: 9 additions & 1 deletion src/Registry/PromisingRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,15 @@ private function getJsonFromResponseBody(ResponseInterface $response): array
$body = (string) $response->getBody();

try {
return \GuzzleHttp\json_decode($body, true);
$decoded = \GuzzleHttp\json_decode($body, true);

if (!is_array($decoded)) {
throw new InvalidArgumentException(
sprintf('response content "%s" is not a valid json object', $body)
);
}

return $decoded;
} catch (InvalidArgumentException $e) {
throw new InvalidArgumentException(
sprintf('%s - with content "%s"', $e->getMessage(), $body),
Expand Down
31 changes: 16 additions & 15 deletions src/Requests/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Assert\Assert;
use FlixTech\SchemaRegistryApi\Schema\AvroReference;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\UriTemplate;
use Psr\Http\Message\RequestInterface;
use const FlixTech\SchemaRegistryApi\Constants\ACCEPT_HEADER;
use const FlixTech\SchemaRegistryApi\Constants\COMPATIBILITY_BACKWARD;
Expand All @@ -32,7 +31,7 @@ function allSubjectVersionsRequest(string $subjectName): RequestInterface
{
return new Request(
'GET',
(new UriTemplate())->expand('subjects/{name}/versions', ['name' => $subjectName]),
sprintf('subjects/%s/versions', $subjectName),
ACCEPT_HEADER
);
}
Expand All @@ -41,9 +40,10 @@ function singleSubjectVersionRequest(string $subjectName, string $versionId): Re
{
return new Request(
'GET',
(new UriTemplate())->expand(
'subjects/{name}/versions/{id}',
['name' => $subjectName, 'id' => $versionId]
sprintf(
'subjects/%s/versions/%s',
$subjectName,
$versionId,
),
ACCEPT_HEADER
);
Expand All @@ -53,7 +53,7 @@ function registerNewSchemaVersionWithSubjectRequest(string $schema, string $subj
{
return new Request(
'POST',
(new UriTemplate())->expand('subjects/{name}/versions', ['name' => $subjectName]),
sprintf('subjects/%s/versions', $subjectName),
CONTENT_TYPE_HEADER + ACCEPT_HEADER,
prepareJsonSchemaForTransfer(validateSchemaStringAsJson($schema), ...$references)
);
Expand All @@ -63,9 +63,10 @@ function checkSchemaCompatibilityAgainstVersionRequest(string $schema, string $s
{
return new Request(
'POST',
(new UriTemplate())->expand(
'compatibility/subjects/{name}/versions/{version}',
['name' => $subjectName, 'version' => $versionId]
sprintf(
'compatibility/subjects/%s/versions/%s',
$subjectName,
$versionId,
),
CONTENT_TYPE_HEADER + ACCEPT_HEADER,
prepareJsonSchemaForTransfer(validateSchemaStringAsJson($schema))
Expand All @@ -76,7 +77,7 @@ function checkIfSubjectHasSchemaRegisteredRequest(string $subjectName, string $s
{
return new Request(
'POST',
(new UriTemplate())->expand('subjects/{name}', ['name' => $subjectName]),
sprintf('subjects/%s', $subjectName),
CONTENT_TYPE_HEADER + ACCEPT_HEADER,
prepareJsonSchemaForTransfer(validateSchemaStringAsJson($schema))
);
Expand All @@ -86,7 +87,7 @@ function schemaRequest(string $id): RequestInterface
{
return new Request(
'GET',
(new UriTemplate())->expand('schemas/ids/{id}', ['id' => $id]),
sprintf('schemas/ids/%s', $id),
ACCEPT_HEADER
);
}
Expand Down Expand Up @@ -114,7 +115,7 @@ function subjectCompatibilityLevelRequest(string $subjectName): RequestInterface
{
return new Request(
'GET',
(new UriTemplate())->expand('config/{subject}', ['subject' => $subjectName]),
sprintf('config/%s', $subjectName),
ACCEPT_HEADER
);
}
Expand All @@ -123,7 +124,7 @@ function changeSubjectCompatibilityLevelRequest(string $subjectName, string $lev
{
return new Request(
'PUT',
(new UriTemplate())->expand('config/{subject}', ['subject' => $subjectName]),
sprintf('config/%s', $subjectName),
ACCEPT_HEADER,
prepareCompatibilityLevelForTransport(validateCompatibilityLevel($level))
);
Expand Down Expand Up @@ -208,7 +209,7 @@ function deleteSubjectRequest(string $subjectName): RequestInterface
{
return new Request(
'DELETE',
(new UriTemplate())->expand('subjects/{name}', ['name' => $subjectName]),
sprintf('subjects/%s', $subjectName),
ACCEPT_HEADER
);
}
Expand All @@ -222,7 +223,7 @@ function deleteSubjectVersionRequest(string $subjectName, string $versionId): Re
{
return new Request(
'DELETE',
(new UriTemplate())->expand('subjects/{name}/versions/{version}', ['name' => $subjectName, 'version' => $versionId]),
sprintf('subjects/%s/versions/%s', $subjectName, $versionId),
ACCEPT_HEADER
);
}
11 changes: 4 additions & 7 deletions test/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\UriTemplate;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use const FlixTech\SchemaRegistryApi\Constants\COMPATIBILITY_BACKWARD;
Expand Down Expand Up @@ -105,12 +104,10 @@ protected function setUp(): void
}

$this->client = new Client([
'base_uri' => (new UriTemplate())->expand(
'http://{host}:{port}',
[
'host' => getenv('TEST_SCHEMA_REGISTRY_HOST'),
'port' => getenv('TEST_SCHEMA_REGISTRY_PORT'),
]
'base_uri' => sprintf(
'http://%s:%s',
getenv('TEST_SCHEMA_REGISTRY_HOST'),
getenv('TEST_SCHEMA_REGISTRY_PORT')
)
]);
}
Expand Down

0 comments on commit ad1a796

Please sign in to comment.