wip: api client for bpn-connect
This commit is contained in:
1 parent
34c2209316
commit
0b4172bb6f
12 files changed
+262
-5
No files matched your search
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\BpnConnect;
|
||||
|
||||
use App\BpnConnect\Exception\BpnConnectException;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\ExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
abstract class AbstractApiClient
|
||||
{
|
||||
public function __construct(
|
||||
protected readonly HttpClientInterface $httpClient,
|
||||
protected readonly LoggerInterface $logger,
|
||||
protected readonly ?string $baseUrl = null,
|
||||
protected readonly ?string $apiKey = null,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $query
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
protected function request(string $path, array $query = []): array
|
||||
{
|
||||
$this->assertConfigured();
|
||||
|
||||
try {
|
||||
$response = $this->httpClient->request('GET', $this->baseUrl.$path, [
|
||||
'headers' => [
|
||||
'X-API-KEY' => $this->apiKey,
|
||||
],
|
||||
'query' => $query,
|
||||
]);
|
||||
|
||||
return $response->toArray();
|
||||
} catch (ExceptionInterface $e) {
|
||||
$this->logger->error('BpnConnect request failed', [
|
||||
'path' => $path,
|
||||
'query' => $query,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
|
||||
throw new BpnConnectException('BpnConnect request failed: '.$e->getMessage(), 0, $e);
|
||||
}
|
||||
}
|
||||
|
||||
private function assertConfigured(): void
|
||||
{
|
||||
if (null === $this->baseUrl || null === $this->apiKey) {
|
||||
throw new BpnConnectException('BpnConnect client is not configured (missing BPN_CONNECT_BASE_URL or BPN_CONNECT_API_KEY).');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user