diff --git a/modules/fx-rates/bootstrap.php b/modules/fx-rates/bootstrap.php
index 9c17065..e45c7ca 100644
--- a/modules/fx-rates/bootstrap.php
+++ b/modules/fx-rates/bootstrap.php
@@ -29,6 +29,7 @@ $mm->registerFunction($moduleName, 'table', static function (string $name): stri
$mm->registerFunction($moduleName, 'settings', static function (): array {
$saved = modules()->settings('fx-rates');
$provider = trim((string) ($saved['provider'] ?? (getenv('FX_RATES_PROVIDER') ?: getenv('MINING_CHECKER_FX_PROVIDER') ?: 'currencyapi')));
+ $apiVersion = strtolower(trim((string) ($saved['api_version'] ?? 'v2')));
$apiUrl = rtrim((string) ($saved['api_url'] ?? (getenv('FX_RATES_API_URL') ?: getenv('MINING_CHECKER_FX_URL') ?: 'https://currencyapi.net')), '/');
$apiKey = trim((string) ($saved['api_key'] ?? (getenv('FX_RATES_API_KEY') ?: getenv('MINING_CHECKER_FX_API_KEY') ?: '')));
$timeout = max(2, (int) ($saved['timeout_sec'] ?? (getenv('FX_RATES_TIMEOUT') ?: getenv('MINING_CHECKER_FX_TIMEOUT') ?: 10)));
@@ -57,6 +58,7 @@ $mm->registerFunction($moduleName, 'settings', static function (): array {
return [
'provider' => $provider !== '' ? $provider : 'currencyapi',
+ 'api_version' => in_array($apiVersion, ['v2', 'v3'], true) ? $apiVersion : 'v2',
'api_url' => $apiUrl,
'api_key' => $apiKey,
'timeout_sec' => $timeout,
diff --git a/modules/fx-rates/module.json b/modules/fx-rates/module.json
index fd28973..9f85972 100644
--- a/modules/fx-rates/module.json
+++ b/modules/fx-rates/module.json
@@ -1,6 +1,6 @@
{
"title": "Waehrungskurse",
- "version": "0.1.3",
+ "version": "0.1.4",
"description": "Zentrales Modul fuer Waehrungskurse, Historie und API-Abrufe.",
"enabled_by_default": true,
"setup": {
@@ -14,6 +14,7 @@
{ "name": "db.user", "label": "DB User", "type": "text", "required": false },
{ "name": "db.password", "label": "DB Passwort", "type": "password", "required": false },
{ "name": "provider", "label": "FX Provider", "type": "text", "required": false, "help": "Unterstuetzt legacy currencyapi.net und currencyapi.com v3." },
+ { "name": "api_version", "label": "FX API Version", "type": "select", "required": false, "help": "Steuert die Endpoint-Version unabhaengig von der Domain." },
{ "name": "api_url", "label": "FX API URL", "type": "text", "required": false, "help": "Nur die Basis-URL eintragen, z.B. https://api.currencyapi.com oder https://currencyapi.net." },
{ "name": "api_key", "label": "FX API Key", "type": "password", "required": false },
{ "name": "timeout_sec", "label": "Timeout (Sek.)", "type": "number", "required": false },
diff --git a/modules/fx-rates/src/Domain/FxRatesService.php b/modules/fx-rates/src/Domain/FxRatesService.php
index d718c32..0a8f291 100644
--- a/modules/fx-rates/src/Domain/FxRatesService.php
+++ b/modules/fx-rates/src/Domain/FxRatesService.php
@@ -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
diff --git a/partials/landingpages/modules/setup.php b/partials/landingpages/modules/setup.php
index fe8893b..969b8d7 100644
--- a/partials/landingpages/modules/setup.php
+++ b/partials/landingpages/modules/setup.php
@@ -523,6 +523,14 @@ $activeDbGroup = $testGroup !== null && array_key_exists($testGroup, $dbGroups)
Unterstuetzt legacy currencyapi.net und currencyapi.com v3.
+
-