-
Notifications
You must be signed in to change notification settings - Fork 22
/
Functions.php
229 lines (202 loc) · 6.08 KB
/
Functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
namespace FlixTech\SchemaRegistryApi\Requests;
use Assert\Assert;
use FlixTech\SchemaRegistryApi\Schema\AvroReference;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Message\RequestInterface;
use const FlixTech\SchemaRegistryApi\Constants\ACCEPT_HEADER;
use const FlixTech\SchemaRegistryApi\Constants\COMPATIBILITY_BACKWARD;
use const FlixTech\SchemaRegistryApi\Constants\COMPATIBILITY_BACKWARD_TRANSITIVE;
use const FlixTech\SchemaRegistryApi\Constants\COMPATIBILITY_FORWARD;
use const FlixTech\SchemaRegistryApi\Constants\COMPATIBILITY_FORWARD_TRANSITIVE;
use const FlixTech\SchemaRegistryApi\Constants\COMPATIBILITY_FULL;
use const FlixTech\SchemaRegistryApi\Constants\COMPATIBILITY_FULL_TRANSITIVE;
use const FlixTech\SchemaRegistryApi\Constants\COMPATIBILITY_NONE;
use const FlixTech\SchemaRegistryApi\Constants\CONTENT_TYPE_HEADER;
use const FlixTech\SchemaRegistryApi\Constants\VERSION_LATEST;
use function implode;
function allSubjectsRequest(): RequestInterface
{
return new Request(
'GET',
'subjects',
ACCEPT_HEADER
);
}
function allSubjectVersionsRequest(string $subjectName): RequestInterface
{
return new Request(
'GET',
sprintf('subjects/%s/versions', $subjectName),
ACCEPT_HEADER
);
}
function singleSubjectVersionRequest(string $subjectName, string $versionId): RequestInterface
{
return new Request(
'GET',
sprintf(
'subjects/%s/versions/%s',
$subjectName,
$versionId,
),
ACCEPT_HEADER
);
}
function registerNewSchemaVersionWithSubjectRequest(string $schema, string $subjectName, AvroReference ...$references): RequestInterface
{
return new Request(
'POST',
sprintf('subjects/%s/versions', $subjectName),
CONTENT_TYPE_HEADER + ACCEPT_HEADER,
prepareJsonSchemaForTransfer(validateSchemaStringAsJson($schema), ...$references)
);
}
function checkSchemaCompatibilityAgainstVersionRequest(string $schema, string $subjectName, string $versionId): RequestInterface
{
return new Request(
'POST',
sprintf(
'compatibility/subjects/%s/versions/%s',
$subjectName,
$versionId,
),
CONTENT_TYPE_HEADER + ACCEPT_HEADER,
prepareJsonSchemaForTransfer(validateSchemaStringAsJson($schema))
);
}
function checkIfSubjectHasSchemaRegisteredRequest(string $subjectName, string $schema): RequestInterface
{
return new Request(
'POST',
sprintf('subjects/%s', $subjectName),
CONTENT_TYPE_HEADER + ACCEPT_HEADER,
prepareJsonSchemaForTransfer(validateSchemaStringAsJson($schema))
);
}
function schemaRequest(string $id): RequestInterface
{
return new Request(
'GET',
sprintf('schemas/ids/%s', $id),
ACCEPT_HEADER
);
}
function defaultCompatibilityLevelRequest(): RequestInterface
{
return new Request(
'GET',
'config',
ACCEPT_HEADER
);
}
function changeDefaultCompatibilityLevelRequest(string $level): RequestInterface
{
return new Request(
'PUT',
'config',
ACCEPT_HEADER,
prepareCompatibilityLevelForTransport(validateCompatibilityLevel($level))
);
}
function subjectCompatibilityLevelRequest(string $subjectName): RequestInterface
{
return new Request(
'GET',
sprintf('config/%s', $subjectName),
ACCEPT_HEADER
);
}
function changeSubjectCompatibilityLevelRequest(string $subjectName, string $level): RequestInterface
{
return new Request(
'PUT',
sprintf('config/%s', $subjectName),
ACCEPT_HEADER,
prepareCompatibilityLevelForTransport(validateCompatibilityLevel($level))
);
}
/**
* @param int|string $versionId
* @return string
*/
function validateVersionId($versionId): string
{
if (VERSION_LATEST !== $versionId) {
Assert::that($versionId)
->integerish('$versionId must be an integer of type int or string')
->between(1, 2 ** 31 - 1, '$versionId must be between 1 and 2^31 - 1');
}
return (string) $versionId;
}
function validateSchemaStringAsJson(string $schema): string
{
Assert::that($schema)->isJsonString('$schema must be a valid JSON string');
return $schema;
}
function prepareJsonSchemaForTransfer(string $schema, AvroReference ...$references): string
{
$return = [
'schema' => $schema
];
return !$references
? \GuzzleHttp\json_encode($return)
: \GuzzleHttp\json_encode(array_merge($return, ['references' => $references]));
}
function validateCompatibilityLevel(string $compatibilityVersion): string
{
$compatibilities = [
COMPATIBILITY_NONE,
COMPATIBILITY_BACKWARD,
COMPATIBILITY_BACKWARD_TRANSITIVE,
COMPATIBILITY_FORWARD,
COMPATIBILITY_FORWARD_TRANSITIVE,
COMPATIBILITY_FULL,
COMPATIBILITY_FULL_TRANSITIVE,
];
Assert::that($compatibilityVersion)->inArray(
$compatibilities,
'$level must be one of ' . implode(', ', $compatibilities)
);
return $compatibilityVersion;
}
function prepareCompatibilityLevelForTransport(string $compatibilityLevel): string
{
return \GuzzleHttp\json_encode(['compatibility' => $compatibilityLevel]);
}
/**
* @param int|string $schemaId
* @return string
*/
function validateSchemaId($schemaId): string
{
Assert::that($schemaId)
->integerish('$schemaId must be an integer value of type int or string')
->greaterThan(0, '$schemaId must be greater than 0');
return (string) $schemaId;
}
/**
* @param string $subjectName
* @return RequestInterface
*/
function deleteSubjectRequest(string $subjectName): RequestInterface
{
return new Request(
'DELETE',
sprintf('subjects/%s', $subjectName),
ACCEPT_HEADER
);
}
/**
* @param string $subjectName
* @param string $versionId
* @return RequestInterface
*/
function deleteSubjectVersionRequest(string $subjectName, string $versionId): RequestInterface
{
return new Request(
'DELETE',
sprintf('subjects/%s/versions/%s', $subjectName, $versionId),
ACCEPT_HEADER
);
}