Skip to content

Commit 836303b

Browse files
committed
Add support for guzzle 7
The UriTemplate class was removed on Guzzle 7, make it not possible to add support for it. Replace usages of UriTemplate with sprintf calls and update composer.json with support for guzzle 7. Fixes #57 and #55.
1 parent d9c4dec commit 836303b

File tree

5 files changed

+32
-26
lines changed

5 files changed

+32
-26
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
"php": "^7.3|^8.0",
1515
"ext-curl": "*",
1616
"ext-json": "*",
17-
"guzzlehttp/guzzle": "~6.3",
17+
"guzzlehttp/guzzle": "~6.3|^7.0",
1818
"guzzlehttp/promises": "^1.4.0",
19-
"guzzlehttp/psr7": "<1.7",
19+
"guzzlehttp/psr7": "^1.7",
2020
"beberlei/assert": "~2.7|~3.0",
2121
"flix-tech/avro-php": "^4.1"
2222
},

src/Exception/ExceptionMap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private function guardAgainstMissingErrorCode(ResponseInterface $response): arra
7474
try {
7575
$decodedBody = \GuzzleHttp\json_decode((string) $response->getBody(), true);
7676

77-
if (!array_key_exists(self::ERROR_CODE_FIELD_NAME, $decodedBody)) {
77+
if (!is_array($decodedBody) || !array_key_exists(self::ERROR_CODE_FIELD_NAME, $decodedBody)) {
7878
throw new RuntimeException(
7979
sprintf(
8080
'Invalid message body received - cannot find "error_code" field in response body "%s"',

src/Registry/PromisingRegistry.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,15 @@ private function getJsonFromResponseBody(ResponseInterface $response): array
174174
$body = (string) $response->getBody();
175175

176176
try {
177-
return \GuzzleHttp\json_decode($body, true);
177+
$decoded = \GuzzleHttp\json_decode($body, true);
178+
179+
if (!is_array($decoded)) {
180+
throw new InvalidArgumentException(
181+
sprintf('response content "%s" is not a valid json object', $body)
182+
);
183+
}
184+
185+
return $decoded;
178186
} catch (InvalidArgumentException $e) {
179187
throw new InvalidArgumentException(
180188
sprintf('%s - with content "%s"', $e->getMessage(), $body),

src/Requests/Functions.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Assert\Assert;
66
use FlixTech\SchemaRegistryApi\Schema\AvroReference;
77
use GuzzleHttp\Psr7\Request;
8-
use GuzzleHttp\UriTemplate;
98
use Psr\Http\Message\RequestInterface;
109
use const FlixTech\SchemaRegistryApi\Constants\ACCEPT_HEADER;
1110
use const FlixTech\SchemaRegistryApi\Constants\COMPATIBILITY_BACKWARD;
@@ -32,7 +31,7 @@ function allSubjectVersionsRequest(string $subjectName): RequestInterface
3231
{
3332
return new Request(
3433
'GET',
35-
(new UriTemplate())->expand('subjects/{name}/versions', ['name' => $subjectName]),
34+
sprintf('subjects/%s/versions', $subjectName),
3635
ACCEPT_HEADER
3736
);
3837
}
@@ -41,9 +40,10 @@ function singleSubjectVersionRequest(string $subjectName, string $versionId): Re
4140
{
4241
return new Request(
4342
'GET',
44-
(new UriTemplate())->expand(
45-
'subjects/{name}/versions/{id}',
46-
['name' => $subjectName, 'id' => $versionId]
43+
sprintf(
44+
'subjects/%s/versions/%s',
45+
$subjectName,
46+
$versionId,
4747
),
4848
ACCEPT_HEADER
4949
);
@@ -53,7 +53,7 @@ function registerNewSchemaVersionWithSubjectRequest(string $schema, string $subj
5353
{
5454
return new Request(
5555
'POST',
56-
(new UriTemplate())->expand('subjects/{name}/versions', ['name' => $subjectName]),
56+
sprintf('subjects/%s/versions', $subjectName),
5757
CONTENT_TYPE_HEADER + ACCEPT_HEADER,
5858
prepareJsonSchemaForTransfer(validateSchemaStringAsJson($schema), ...$references)
5959
);
@@ -63,9 +63,10 @@ function checkSchemaCompatibilityAgainstVersionRequest(string $schema, string $s
6363
{
6464
return new Request(
6565
'POST',
66-
(new UriTemplate())->expand(
67-
'compatibility/subjects/{name}/versions/{version}',
68-
['name' => $subjectName, 'version' => $versionId]
66+
sprintf(
67+
'compatibility/subjects/%s/versions/%s',
68+
$subjectName,
69+
$versionId,
6970
),
7071
CONTENT_TYPE_HEADER + ACCEPT_HEADER,
7172
prepareJsonSchemaForTransfer(validateSchemaStringAsJson($schema))
@@ -76,7 +77,7 @@ function checkIfSubjectHasSchemaRegisteredRequest(string $subjectName, string $s
7677
{
7778
return new Request(
7879
'POST',
79-
(new UriTemplate())->expand('subjects/{name}', ['name' => $subjectName]),
80+
sprintf('subjects/%s', $subjectName),
8081
CONTENT_TYPE_HEADER + ACCEPT_HEADER,
8182
prepareJsonSchemaForTransfer(validateSchemaStringAsJson($schema))
8283
);
@@ -86,7 +87,7 @@ function schemaRequest(string $id): RequestInterface
8687
{
8788
return new Request(
8889
'GET',
89-
(new UriTemplate())->expand('schemas/ids/{id}', ['id' => $id]),
90+
sprintf('schemas/ids/%s', $id),
9091
ACCEPT_HEADER
9192
);
9293
}
@@ -114,7 +115,7 @@ function subjectCompatibilityLevelRequest(string $subjectName): RequestInterface
114115
{
115116
return new Request(
116117
'GET',
117-
(new UriTemplate())->expand('config/{subject}', ['subject' => $subjectName]),
118+
sprintf('config/%s', $subjectName),
118119
ACCEPT_HEADER
119120
);
120121
}
@@ -123,7 +124,7 @@ function changeSubjectCompatibilityLevelRequest(string $subjectName, string $lev
123124
{
124125
return new Request(
125126
'PUT',
126-
(new UriTemplate())->expand('config/{subject}', ['subject' => $subjectName]),
127+
sprintf('config/%s', $subjectName),
127128
ACCEPT_HEADER,
128129
prepareCompatibilityLevelForTransport(validateCompatibilityLevel($level))
129130
);
@@ -208,7 +209,7 @@ function deleteSubjectRequest(string $subjectName): RequestInterface
208209
{
209210
return new Request(
210211
'DELETE',
211-
(new UriTemplate())->expand('subjects/{name}', ['name' => $subjectName]),
212+
sprintf('subjects/%s', $subjectName),
212213
ACCEPT_HEADER
213214
);
214215
}
@@ -222,7 +223,7 @@ function deleteSubjectVersionRequest(string $subjectName, string $versionId): Re
222223
{
223224
return new Request(
224225
'DELETE',
225-
(new UriTemplate())->expand('subjects/{name}/versions/{version}', ['name' => $subjectName, 'version' => $versionId]),
226+
sprintf('subjects/%s/versions/%s', $subjectName, $versionId),
226227
ACCEPT_HEADER
227228
);
228229
}

test/IntegrationTest.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use GuzzleHttp\Client;
1515
use GuzzleHttp\ClientInterface;
1616
use GuzzleHttp\Exception\RequestException;
17-
use GuzzleHttp\UriTemplate;
1817
use PHPUnit\Framework\TestCase;
1918
use Psr\Http\Message\ResponseInterface;
2019
use const FlixTech\SchemaRegistryApi\Constants\COMPATIBILITY_BACKWARD;
@@ -105,12 +104,10 @@ protected function setUp(): void
105104
}
106105

107106
$this->client = new Client([
108-
'base_uri' => (new UriTemplate())->expand(
109-
'http://{host}:{port}',
110-
[
111-
'host' => getenv('TEST_SCHEMA_REGISTRY_HOST'),
112-
'port' => getenv('TEST_SCHEMA_REGISTRY_PORT'),
113-
]
107+
'base_uri' => sprintf(
108+
'http://%s:%s',
109+
getenv('TEST_SCHEMA_REGISTRY_HOST'),
110+
getenv('TEST_SCHEMA_REGISTRY_PORT')
114111
)
115112
]);
116113
}

0 commit comments

Comments
 (0)