33namespace Typesense ;
44
55use Exception ;
6+ use Http \Client \Common \HttpMethodsClient ;
67use Http \Client \Exception as HttpClientException ;
78use Http \Client \Exception \HttpException ;
89use Psr \Http \Client \ClientInterface ;
910use Psr \Http \Message \StreamInterface ;
1011use Psr \Log \LoggerInterface ;
12+ use Http \Discovery \Psr17FactoryDiscovery ;
1113use Typesense \Exceptions \HTTPStatus0Error ;
1214use Typesense \Exceptions \ObjectAlreadyExists ;
1315use Typesense \Exceptions \ObjectNotFound ;
@@ -32,9 +34,9 @@ class ApiCall
3234 private const API_KEY_HEADER_NAME = 'X-TYPESENSE-API-KEY ' ;
3335
3436 /**
35- * @var ClientInterface
37+ * @var ClientInterface | HttpMethodsClient
3638 */
37- private ClientInterface $ client ;
39+ private $ client ;
3840
3941 /**
4042 * @var Configuration
@@ -218,12 +220,35 @@ private function makeRequest(string $method, string $endPoint, bool $asJson, arr
218220 $ reqOp ['query ' ] = http_build_query ($ options ['query ' ]);
219221 }
220222
221- $ response = $ this ->client ->send (
222- \strtoupper ($ method ),
223- $ url . '? ' . ($ reqOp ['query ' ] ?? '' ),
224- $ reqOp ['headers ' ] ?? [],
225- $ reqOp ['body ' ] ?? null
226- );
223+ $ response = null ;
224+
225+ if ($ this ->client instanceof HttpMethodsClient) {
226+ $ response = $ this ->client ->send (
227+ \strtoupper ($ method ),
228+ $ url . '? ' . ($ reqOp ['query ' ] ?? '' ),
229+ $ reqOp ['headers ' ] ?? [],
230+ $ reqOp ['body ' ] ?? null
231+ );
232+ } else {
233+ $ requestFactory = Psr17FactoryDiscovery::findRequestFactory ();
234+ $ streamFactory = Psr17FactoryDiscovery::findStreamFactory ();
235+
236+ $ request = $ requestFactory ->createRequest (
237+ strtoupper ($ method ),
238+ $ url . '? ' . ($ reqOp ['query ' ] ?? '' )
239+ );
240+
241+ foreach ($ reqOp ['headers ' ] ?? [] as $ name => $ value ) {
242+ $ request = $ request ->withHeader ($ name , $ value );
243+ }
244+
245+ if (isset ($ reqOp ['body ' ])) {
246+ $ body = $ streamFactory ->createStream ($ reqOp ['body ' ]);
247+ $ request = $ request ->withBody ($ body );
248+ }
249+
250+ $ response = $ this ->client ->sendRequest ($ request );
251+ }
227252
228253 $ statusCode = $ response ->getStatusCode ();
229254 if (0 < $ statusCode && $ statusCode < 500 ) {
0 commit comments