Skip to content

Commit f5c7474

Browse files
committed
fix(client): handle both http client types in api calls
- update `ApiCall` class to support both `HttpMethodsClient` and `ClientInterface` - fix type declaration in `Configuration::getClient()` return type - add request factory handling for `ClientInterface` requests
1 parent c799dd3 commit f5c7474

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

src/ApiCall.php

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
namespace Typesense;
44

55
use Exception;
6+
use Http\Client\Common\HttpMethodsClient;
67
use Http\Client\Exception as HttpClientException;
78
use Http\Client\Exception\HttpException;
89
use Psr\Http\Client\ClientInterface;
910
use Psr\Http\Message\StreamInterface;
1011
use Psr\Log\LoggerInterface;
12+
use Http\Discovery\Psr17FactoryDiscovery;
1113
use Typesense\Exceptions\HTTPStatus0Error;
1214
use Typesense\Exceptions\ObjectAlreadyExists;
1315
use 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) {

src/Lib/Configuration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,9 @@ public function getLogger(): LoggerInterface
223223
}
224224

225225
/**
226-
* @return ClientInterface
226+
* @return ClientInterface | HttpMethodsClient
227227
*/
228-
public function getClient(): ClientInterface
228+
public function getClient(): ClientInterface | HttpMethodsClient
229229
{
230230
if ($this->client === null) {
231231
$discoveredClient = Psr18ClientDiscovery::find();

0 commit comments

Comments
 (0)