diff --git a/modules/fx-rates/src/Domain/FxRatesService.php b/modules/fx-rates/src/Domain/FxRatesService.php index 8ee8d15..d718c32 100644 --- a/modules/fx-rates/src/Domain/FxRatesService.php +++ b/modules/fx-rates/src/Domain/FxRatesService.php @@ -414,12 +414,25 @@ final class FxRatesService private function fetchCurrenciesPayload(): array { - $request = $this->buildCurrenciesRequest(); - if ($request === null) { + $requests = $this->buildCurrenciesRequests(); + if ($requests === []) { throw new \RuntimeException('FX-API-Key fehlt.'); } - $payload = $this->requestJson($request['url'], $request['headers'] ?? [], 'Waehrungskatalog konnte nicht geladen werden.'); + $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; + } + } + + if (!is_array($payload)) { + throw $lastError ?? new \RuntimeException('Waehrungskatalog konnte nicht geladen werden.'); + } if ($this->isCurrencyApiCom()) { return $this->normalizeCurrencyApiComCurrenciesPayload($payload); @@ -482,30 +495,40 @@ final class FxRatesService ]; } - private function buildCurrenciesRequest(): ?array + private function buildCurrenciesRequests(): array { $apiKey = $this->apiKey(); if ($apiKey === '') { - return null; + return []; } if ($this->isCurrencyApiCom()) { - return [ + return [[ 'url' => $this->currenciesApiUrl() . '/v3/currencies', 'headers' => [ 'Accept: application/json', 'apikey: ' . $apiKey, ], - ]; + ]]; } return [ - 'url' => sprintf( - '%s/api/v2/currencies?output=json&key=%s', - $this->currenciesApiUrl(), - rawurlencode($apiKey) - ), - 'headers' => ['Accept: application/json'], + [ + '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'], + ], ]; }