diff --git a/src/ApiCall.php b/src/ApiCall.php index a114b592..afab89e8 100644 --- a/src/ApiCall.php +++ b/src/ApiCall.php @@ -3,11 +3,13 @@ namespace Typesense; use Exception; +use Http\Client\Common\HttpMethodsClient; use Http\Client\Exception as HttpClientException; use Http\Client\Exception\HttpException; use Psr\Http\Client\ClientInterface; use Psr\Http\Message\StreamInterface; use Psr\Log\LoggerInterface; +use Http\Discovery\Psr17FactoryDiscovery; use Typesense\Exceptions\HTTPStatus0Error; use Typesense\Exceptions\ObjectAlreadyExists; use Typesense\Exceptions\ObjectNotFound; @@ -32,9 +34,9 @@ class ApiCall private const API_KEY_HEADER_NAME = 'X-TYPESENSE-API-KEY'; /** - * @var ClientInterface + * @var ClientInterface | HttpMethodsClient */ - private ClientInterface $client; + private $client; /** * @var Configuration @@ -218,12 +220,35 @@ private function makeRequest(string $method, string $endPoint, bool $asJson, arr $reqOp['query'] = http_build_query($options['query']); } - $response = $this->client->send( - \strtoupper($method), - $url . '?' . ($reqOp['query'] ?? ''), - $reqOp['headers'] ?? [], - $reqOp['body'] ?? null - ); + $response = null; + + if ($this->client instanceof HttpMethodsClient) { + $response = $this->client->send( + \strtoupper($method), + $url . '?' . ($reqOp['query'] ?? ''), + $reqOp['headers'] ?? [], + $reqOp['body'] ?? null + ); + } else { + $requestFactory = Psr17FactoryDiscovery::findRequestFactory(); + $streamFactory = Psr17FactoryDiscovery::findStreamFactory(); + + $request = $requestFactory->createRequest( + strtoupper($method), + $url . '?' . ($reqOp['query'] ?? '') + ); + + foreach ($reqOp['headers'] ?? [] as $name => $value) { + $request = $request->withHeader($name, $value); + } + + if (isset($reqOp['body'])) { + $body = $streamFactory->createStream($reqOp['body']); + $request = $request->withBody($body); + } + + $response = $this->client->sendRequest($request); + } $statusCode = $response->getStatusCode(); if (0 < $statusCode && $statusCode < 500) { diff --git a/src/Lib/Configuration.php b/src/Lib/Configuration.php index 7f60ec07..7ad8c80b 100644 --- a/src/Lib/Configuration.php +++ b/src/Lib/Configuration.php @@ -223,9 +223,9 @@ public function getLogger(): LoggerInterface } /** - * @return ClientInterface + * @return ClientInterface | HttpMethodsClient */ - public function getClient(): ClientInterface + public function getClient(): ClientInterface | HttpMethodsClient { if ($this->client === null) { $discoveredClient = Psr18ClientDiscovery::find();