Skip to content

Commit

Permalink
fix(client): handle both http client types in api calls
Browse files Browse the repository at this point in the history
- update `ApiCall` class to support both `HttpMethodsClient` and `ClientInterface`
- fix type declaration in `Configuration::getClient()` return type
- add request factory handling for `ClientInterface` requests
  • Loading branch information
tharropoulos committed Feb 24, 2025
1 parent c799dd3 commit f5c7474
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
41 changes: 33 additions & 8 deletions src/ApiCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/Lib/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit f5c7474

Please sign in to comment.