Skip to content

Add Guzzle 7 support #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
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
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