asdsad
All checks were successful
Deploy / deploy-staging (push) Successful in 6s
Deploy / deploy-production (push) Has been skipped

This commit is contained in:
2026-04-29 02:08:26 +02:00
parent 9e687455fa
commit a0f19ff6b4
4 changed files with 36 additions and 53 deletions

View File

@@ -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,

View File

@@ -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 },

View File

@@ -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;
}
}
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'],
],
];
}
@@ -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

View File

@@ -523,6 +523,14 @@ $activeDbGroup = $testGroup !== null && array_key_exists($testGroup, $dbGroups)
<input type="text" name="provider" value="<?= e((string) ($current['provider'] ?? 'currencyapi')) ?>">
<small class="muted">Unterstuetzt legacy currencyapi.net und currencyapi.com v3.</small>
</label>
<label class="setup-field muted">
<span>FX API Version</span>
<select name="api_version">
<option value="v2" <?= (string) ($current['api_version'] ?? 'v2') === 'v2' ? 'selected' : '' ?>>v2</option>
<option value="v3" <?= (string) ($current['api_version'] ?? 'v2') === 'v3' ? 'selected' : '' ?>>v3</option>
</select>
<small class="muted">Steuert die Endpoint-Version unabhaengig von der Domain.</small>
</label>
<label class="setup-field muted">
<span>FX API URL</span>
<input type="text" name="api_url" value="<?= e((string) ($current['api_url'] ?? 'https://currencyapi.net')) ?>">
@@ -536,10 +544,6 @@ $activeDbGroup = $testGroup !== null && array_key_exists($testGroup, $dbGroups)
<span>Timeout (Sek.)</span>
<input type="number" name="timeout_sec" value="<?= e((string) ($current['timeout_sec'] ?? 10)) ?>">
</label>
<label class="setup-field muted">
<span>Datei-Cache TTL (Sek.)</span>
<input type="number" name="cache_ttl_sec" value="<?= e((string) ($current['cache_ttl_sec'] ?? 21600)) ?>">
</label>
<label class="setup-field muted">
<span>Standard-Basiswaehrung</span>
<input type="text" name="default_base_currency" value="<?= e((string) ($current['default_base_currency'] ?? 'EUR')) ?>">