77use AvroSchema ;
88use Closure ;
99use FlixTech \SchemaRegistryApi \AsynchronousRegistry ;
10+ use FlixTech \SchemaRegistryApi \Constants ;
1011use FlixTech \SchemaRegistryApi \Exception \ExceptionMap ;
1112use FlixTech \SchemaRegistryApi \Schema \AvroReference ;
1213use FlixTech \SchemaRegistryApi \Exception \RuntimeException ;
1314use FlixTech \SchemaRegistryApi \Exception \SchemaRegistryException ;
15+ use FlixTech \SchemaRegistryApi \Json ;
16+ use FlixTech \SchemaRegistryApi \Requests ;
1417use GuzzleHttp \ClientInterface ;
1518use GuzzleHttp \Exception \RequestException ;
1619use GuzzleHttp \Promise \PromiseInterface ;
1720use Psr \Http \Message \RequestInterface ;
1821use Psr \Http \Message \ResponseInterface ;
19- use const FlixTech \SchemaRegistryApi \Constants \VERSION_LATEST ;
20- use function FlixTech \SchemaRegistryApi \Requests \checkIfSubjectHasSchemaRegisteredRequest ;
21- use function FlixTech \SchemaRegistryApi \Requests \decodeResponse ;
22- use function FlixTech \SchemaRegistryApi \Requests \registerNewSchemaVersionWithSubjectRequest ;
23- use function FlixTech \SchemaRegistryApi \Requests \schemaRequest ;
24- use function FlixTech \SchemaRegistryApi \Requests \singleSubjectVersionRequest ;
25- use function FlixTech \SchemaRegistryApi \Requests \validateSchemaId ;
26- use function FlixTech \SchemaRegistryApi \Requests \validateVersionId ;
2722
2823class GuzzlePromiseAsyncRegistry implements AsynchronousRegistry
2924{
@@ -71,10 +66,10 @@ public function __construct(ClientInterface $client)
7166 */
7267 public function register (string $ subject , AvroSchema $ schema , AvroReference ...$ references ): PromiseInterface
7368 {
74- $ request = registerNewSchemaVersionWithSubjectRequest ((string ) $ schema , $ subject , ...$ references );
69+ $ request = Requests:: registerNewSchemaVersionWithSubjectRequest ((string ) $ schema , $ subject , ...$ references );
7570
7671 $ onFulfilled = function (ResponseInterface $ response ) {
77- return $ this -> getJsonFromResponseBody ($ response )['id ' ];
72+ return Json:: decodeResponse ($ response )['id ' ];
7873 };
7974
8075 return $ this ->makeRequest ($ request , $ onFulfilled );
@@ -87,10 +82,10 @@ public function register(string $subject, AvroSchema $schema, AvroReference ...$
8782 */
8883 public function schemaId (string $ subject , AvroSchema $ schema ): PromiseInterface
8984 {
90- $ request = checkIfSubjectHasSchemaRegisteredRequest ($ subject , (string ) $ schema );
85+ $ request = Requests:: checkIfSubjectHasSchemaRegisteredRequest ($ subject , (string )$ schema );
9186
9287 $ onFulfilled = function (ResponseInterface $ response ) {
93- return $ this -> getJsonFromResponseBody ($ response )['id ' ];
88+ return Json:: decodeResponse ($ response )['id ' ];
9489 };
9590
9691 return $ this ->makeRequest ($ request , $ onFulfilled );
@@ -103,11 +98,11 @@ public function schemaId(string $subject, AvroSchema $schema): PromiseInterface
10398 */
10499 public function schemaForId (int $ schemaId ): PromiseInterface
105100 {
106- $ request = schemaRequest (validateSchemaId ($ schemaId ));
101+ $ request = Requests:: schemaRequest (Requests:: validateSchemaId ($ schemaId ));
107102
108103 $ onFulfilled = function (ResponseInterface $ response ) {
109104 return AvroSchema::parse (
110- $ this -> getJsonFromResponseBody ($ response )['schema ' ]
105+ Json:: decodeResponse ($ response )['schema ' ]
111106 );
112107 };
113108
@@ -121,11 +116,11 @@ public function schemaForId(int $schemaId): PromiseInterface
121116 */
122117 public function schemaForSubjectAndVersion (string $ subject , int $ version ): PromiseInterface
123118 {
124- $ request = singleSubjectVersionRequest ($ subject , validateVersionId ($ version ));
119+ $ request = Requests:: singleSubjectVersionRequest ($ subject , Requests:: validateVersionId ($ version ));
125120
126121 $ onFulfilled = function (ResponseInterface $ response ) {
127122 return AvroSchema::parse (
128- $ this -> getJsonFromResponseBody ($ response )['schema ' ]
123+ Json:: decodeResponse ($ response )['schema ' ]
129124 );
130125 };
131126
@@ -139,10 +134,10 @@ public function schemaForSubjectAndVersion(string $subject, int $version): Promi
139134 */
140135 public function schemaVersion (string $ subject , AvroSchema $ schema ): PromiseInterface
141136 {
142- $ request = checkIfSubjectHasSchemaRegisteredRequest ($ subject , (string ) $ schema );
137+ $ request = Requests:: checkIfSubjectHasSchemaRegisteredRequest ($ subject , (string )$ schema );
143138
144139 $ onFulfilled = function (ResponseInterface $ response ) {
145- return $ this -> getJsonFromResponseBody ($ response )['version ' ];
140+ return Json:: decodeResponse ($ response )['version ' ];
146141 };
147142
148143 return $ this ->makeRequest ($ request , $ onFulfilled );
@@ -155,11 +150,11 @@ public function schemaVersion(string $subject, AvroSchema $schema): PromiseInter
155150 */
156151 public function latestVersion (string $ subject ): PromiseInterface
157152 {
158- $ request = singleSubjectVersionRequest ($ subject , VERSION_LATEST );
153+ $ request = Requests:: singleSubjectVersionRequest ($ subject , Constants:: VERSION_LATEST );
159154
160155 $ onFulfilled = function (ResponseInterface $ response ) {
161156 return AvroSchema::parse (
162- $ this -> getJsonFromResponseBody ($ response )['schema ' ]
157+ Json:: decodeResponse ($ response )['schema ' ]
163158 );
164159 };
165160
@@ -178,31 +173,4 @@ private function makeRequest(RequestInterface $request, callable $onFulfilled):
178173 ->sendAsync ($ request )
179174 ->then ($ onFulfilled , $ this ->rejectedCallback );
180175 }
181-
182- /**
183- * @param ResponseInterface $response
184- * @return array<mixed, mixed>
185- */
186- private function getJsonFromResponseBody (ResponseInterface $ response ): array
187- {
188- $ body = (string ) $ response ->getBody ();
189-
190- try {
191- $ decoded = \GuzzleHttp \json_decode ($ body , true );
192-
193- if (!is_array ($ decoded )) {
194- throw new InvalidArgumentException (
195- sprintf ('response content "%s" is not a valid json object ' , $ body )
196- );
197- }
198-
199- return $ decoded ;
200- } catch (InvalidArgumentException $ e ) {
201- throw new InvalidArgumentException (
202- sprintf ('%s - with content "%s" ' , $ e ->getMessage (), $ body ),
203- $ e ->getCode (),
204- $ e
205- );
206- }
207- }
208176}
0 commit comments