asdsad
This commit is contained in:
@@ -367,7 +367,7 @@ final class FxRatesService
|
||||
|
||||
$payload = $this->requestJson($request['url'], $request['headers'] ?? [], 'FX-Kurse konnten nicht geladen werden.');
|
||||
|
||||
if ($this->isCurrencyApiCom()) {
|
||||
if ($this->usesApiVersion('v3')) {
|
||||
return $this->normalizeCurrencyApiComLatestPayload($payload, $baseCurrency, $currencies);
|
||||
}
|
||||
|
||||
@@ -414,27 +414,14 @@ final class FxRatesService
|
||||
|
||||
private function fetchCurrenciesPayload(): array
|
||||
{
|
||||
$requests = $this->buildCurrenciesRequests();
|
||||
if ($requests === []) {
|
||||
$request = $this->buildCurrenciesRequest();
|
||||
if ($request === null) {
|
||||
throw new \RuntimeException('FX-API-Key fehlt.');
|
||||
}
|
||||
|
||||
$payload = null;
|
||||
$lastError = null;
|
||||
foreach ($requests as $request) {
|
||||
try {
|
||||
$payload = $this->requestJson($request['url'], $request['headers'] ?? [], 'Waehrungskatalog konnte nicht geladen werden.');
|
||||
break;
|
||||
} catch (\RuntimeException $exception) {
|
||||
$lastError = $exception;
|
||||
}
|
||||
}
|
||||
$payload = $this->requestJson($request['url'], $request['headers'] ?? [], 'Waehrungskatalog konnte nicht geladen werden.');
|
||||
|
||||
if (!is_array($payload)) {
|
||||
throw $lastError ?? new \RuntimeException('Waehrungskatalog konnte nicht geladen werden.');
|
||||
}
|
||||
|
||||
if ($this->isCurrencyApiCom()) {
|
||||
if ($this->usesApiVersion('v3')) {
|
||||
return $this->normalizeCurrencyApiComCurrenciesPayload($payload);
|
||||
}
|
||||
|
||||
@@ -448,7 +435,7 @@ final class FxRatesService
|
||||
private function buildLatestRequest(string $baseCurrency, ?array $currencies = null): ?array
|
||||
{
|
||||
$apiKey = $this->apiKey();
|
||||
if ($this->isCurrencyApiCom()) {
|
||||
if ($this->usesApiVersion('v3')) {
|
||||
if ($apiKey === '') {
|
||||
return null;
|
||||
}
|
||||
@@ -495,40 +482,30 @@ final class FxRatesService
|
||||
];
|
||||
}
|
||||
|
||||
private function buildCurrenciesRequests(): array
|
||||
private function buildCurrenciesRequest(): ?array
|
||||
{
|
||||
$apiKey = $this->apiKey();
|
||||
if ($apiKey === '') {
|
||||
return [];
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->isCurrencyApiCom()) {
|
||||
return [[
|
||||
if ($this->usesApiVersion('v3')) {
|
||||
return [
|
||||
'url' => $this->currenciesApiUrl() . '/v3/currencies',
|
||||
'headers' => [
|
||||
'Accept: application/json',
|
||||
'apikey: ' . $apiKey,
|
||||
],
|
||||
]];
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
[
|
||||
'url' => sprintf(
|
||||
'%s/api/v1/currencies?output=json&key=%s',
|
||||
$this->currenciesApiUrl(),
|
||||
rawurlencode($apiKey)
|
||||
),
|
||||
'headers' => ['Accept: application/json'],
|
||||
],
|
||||
[
|
||||
'url' => sprintf(
|
||||
'%s/api/v2/currencies?output=json&key=%s',
|
||||
$this->currenciesApiUrl(),
|
||||
rawurlencode($apiKey)
|
||||
),
|
||||
'headers' => ['Accept: application/json'],
|
||||
],
|
||||
'url' => sprintf(
|
||||
'%s/api/v2/currencies?output=json&key=%s',
|
||||
$this->currenciesApiUrl(),
|
||||
rawurlencode($apiKey)
|
||||
),
|
||||
'headers' => ['Accept: application/json'],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -788,16 +765,15 @@ final class FxRatesService
|
||||
return rtrim((string) ($this->settings['currencies_url'] ?? $this->apiUrl()), '/');
|
||||
}
|
||||
|
||||
private function isCurrencyApiCom(): bool
|
||||
private function apiVersion(): string
|
||||
{
|
||||
foreach ([$this->apiUrl(), $this->currenciesApiUrl()] as $url) {
|
||||
$host = strtolower((string) parse_url($url, PHP_URL_HOST));
|
||||
if ($host !== '' && str_contains($host, 'currencyapi.com')) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
$version = strtolower(trim((string) ($this->settings['api_version'] ?? 'v2')));
|
||||
return in_array($version, ['v2', 'v3'], true) ? $version : 'v2';
|
||||
}
|
||||
|
||||
return false;
|
||||
private function usesApiVersion(string $version): bool
|
||||
{
|
||||
return $this->apiVersion() === strtolower(trim($version));
|
||||
}
|
||||
|
||||
private function apiKey(): string
|
||||
|
||||
Reference in New Issue
Block a user