Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 16 additions & 21 deletions libs/php-api-client-base/src/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,22 @@ static function (callable $handler) use ($authenticator): callable {
]);
}

public function sendRequest(RequestInterface $request): void
/**
* @param array<string, mixed> $options
*/
public function sendRequest(RequestInterface $request, array $options = []): ResponseInterface
{
$this->doSendRequest($request);
try {
return $this->httpClient->send($request, $options);
} catch (RequestException $e) {
throw $this->processRequestException($e);
} catch (GuzzleException $e) {
throw new $this->exceptionClass($e->getMessage(), 0, $e, null, null);
} catch (Throwable $e) {
// Non-Guzzle failure bubbling out of the handler stack — e.g. an authenticator
// that could not produce credentials (after retries are exhausted).
throw new $this->exceptionClass(trim($e->getMessage()), 0, $e, null, null);
}
}

/**
Expand All @@ -130,7 +143,7 @@ public function sendRequestAndMapResponse(
array $options = [],
bool $isList = false,
) {
$response = $this->doSendRequest($request, $options);
$response = $this->sendRequest($request, $options);
$body = $response->getBody()->getContents();

try {
Expand Down Expand Up @@ -165,24 +178,6 @@ public function sendRequestAndMapResponse(
}
}

/**
* @param array<string, mixed> $options
*/
private function doSendRequest(RequestInterface $request, array $options = []): ResponseInterface
{
try {
return $this->httpClient->send($request, $options);
} catch (RequestException $e) {
throw $this->processRequestException($e);
} catch (GuzzleException $e) {
throw new $this->exceptionClass($e->getMessage(), 0, $e, null, null);
} catch (Throwable $e) {
// Non-Guzzle failure bubbling out of the handler stack — e.g. an authenticator
// that could not produce credentials (after retries are exhausted).
throw new $this->exceptionClass(trim($e->getMessage()), 0, $e, null, null);
}
}

private function processRequestException(RequestException $e): ClientException
{
$response = $e->getResponse();
Expand Down
13 changes: 13 additions & 0 deletions libs/php-api-client-base/tests/ApiClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ public function testAddsAuthHeaderPerRequest(): void
self::assertSame('secret-token', $last->getHeaderLine('X-KBC-ManageApiToken'));
}

public function testSendRequestReturnsResponse(): void
{
$mock = new MockHandler([new Response(201, [], '{"hello":"world"}')]);
$client = new ApiClient('https://example.test', new NoAuthAuthenticator(), new ApiClientOptions(
requestHandler: HandlerStack::create($mock),
));

$response = $client->sendRequest(new Request('GET', 'foo'));

self::assertSame(201, $response->getStatusCode());
self::assertSame('{"hello":"world"}', (string) $response->getBody());
}

public function testMapsResponseToModel(): void
{
$mock = new MockHandler([new Response(200, [], '{"name":"foo"}')]);
Expand Down
Loading