asdsad
This commit is contained in:
@@ -29,6 +29,7 @@ $mm->registerFunction($moduleName, 'table', static function (string $name): stri
|
|||||||
$mm->registerFunction($moduleName, 'settings', static function (): array {
|
$mm->registerFunction($moduleName, 'settings', static function (): array {
|
||||||
$saved = modules()->settings('fx-rates');
|
$saved = modules()->settings('fx-rates');
|
||||||
$provider = trim((string) ($saved['provider'] ?? (getenv('FX_RATES_PROVIDER') ?: getenv('MINING_CHECKER_FX_PROVIDER') ?: 'currencyapi')));
|
$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')), '/');
|
$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') ?: '')));
|
$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)));
|
$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 [
|
return [
|
||||||
'provider' => $provider !== '' ? $provider : 'currencyapi',
|
'provider' => $provider !== '' ? $provider : 'currencyapi',
|
||||||
|
'api_version' => in_array($apiVersion, ['v2', 'v3'], true) ? $apiVersion : 'v2',
|
||||||
'api_url' => $apiUrl,
|
'api_url' => $apiUrl,
|
||||||
'api_key' => $apiKey,
|
'api_key' => $apiKey,
|
||||||
'timeout_sec' => $timeout,
|
'timeout_sec' => $timeout,
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"title": "Waehrungskurse",
|
"title": "Waehrungskurse",
|
||||||
"version": "0.1.3",
|
"version": "0.1.4",
|
||||||
"description": "Zentrales Modul fuer Waehrungskurse, Historie und API-Abrufe.",
|
"description": "Zentrales Modul fuer Waehrungskurse, Historie und API-Abrufe.",
|
||||||
"enabled_by_default": true,
|
"enabled_by_default": true,
|
||||||
"setup": {
|
"setup": {
|
||||||
@@ -14,6 +14,7 @@
|
|||||||
{ "name": "db.user", "label": "DB User", "type": "text", "required": false },
|
{ "name": "db.user", "label": "DB User", "type": "text", "required": false },
|
||||||
{ "name": "db.password", "label": "DB Passwort", "type": "password", "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": "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_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": "api_key", "label": "FX API Key", "type": "password", "required": false },
|
||||||
{ "name": "timeout_sec", "label": "Timeout (Sek.)", "type": "number", "required": false },
|
{ "name": "timeout_sec", "label": "Timeout (Sek.)", "type": "number", "required": false },
|
||||||
|
|||||||
@@ -367,7 +367,7 @@ final class FxRatesService
|
|||||||
|
|
||||||
$payload = $this->requestJson($request['url'], $request['headers'] ?? [], 'FX-Kurse konnten nicht geladen werden.');
|
$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);
|
return $this->normalizeCurrencyApiComLatestPayload($payload, $baseCurrency, $currencies);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -414,27 +414,14 @@ final class FxRatesService
|
|||||||
|
|
||||||
private function fetchCurrenciesPayload(): array
|
private function fetchCurrenciesPayload(): array
|
||||||
{
|
{
|
||||||
$requests = $this->buildCurrenciesRequests();
|
$request = $this->buildCurrenciesRequest();
|
||||||
if ($requests === []) {
|
if ($request === null) {
|
||||||
throw new \RuntimeException('FX-API-Key fehlt.');
|
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.');
|
$payload = $this->requestJson($request['url'], $request['headers'] ?? [], 'Waehrungskatalog konnte nicht geladen werden.');
|
||||||
break;
|
|
||||||
} catch (\RuntimeException $exception) {
|
|
||||||
$lastError = $exception;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!is_array($payload)) {
|
if ($this->usesApiVersion('v3')) {
|
||||||
throw $lastError ?? new \RuntimeException('Waehrungskatalog konnte nicht geladen werden.');
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->isCurrencyApiCom()) {
|
|
||||||
return $this->normalizeCurrencyApiComCurrenciesPayload($payload);
|
return $this->normalizeCurrencyApiComCurrenciesPayload($payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -448,7 +435,7 @@ final class FxRatesService
|
|||||||
private function buildLatestRequest(string $baseCurrency, ?array $currencies = null): ?array
|
private function buildLatestRequest(string $baseCurrency, ?array $currencies = null): ?array
|
||||||
{
|
{
|
||||||
$apiKey = $this->apiKey();
|
$apiKey = $this->apiKey();
|
||||||
if ($this->isCurrencyApiCom()) {
|
if ($this->usesApiVersion('v3')) {
|
||||||
if ($apiKey === '') {
|
if ($apiKey === '') {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -495,40 +482,30 @@ final class FxRatesService
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
private function buildCurrenciesRequests(): array
|
private function buildCurrenciesRequest(): ?array
|
||||||
{
|
{
|
||||||
$apiKey = $this->apiKey();
|
$apiKey = $this->apiKey();
|
||||||
if ($apiKey === '') {
|
if ($apiKey === '') {
|
||||||
return [];
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->isCurrencyApiCom()) {
|
if ($this->usesApiVersion('v3')) {
|
||||||
return [[
|
return [
|
||||||
'url' => $this->currenciesApiUrl() . '/v3/currencies',
|
'url' => $this->currenciesApiUrl() . '/v3/currencies',
|
||||||
'headers' => [
|
'headers' => [
|
||||||
'Accept: application/json',
|
'Accept: application/json',
|
||||||
'apikey: ' . $apiKey,
|
'apikey: ' . $apiKey,
|
||||||
],
|
],
|
||||||
]];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
[
|
|
||||||
'url' => sprintf(
|
|
||||||
'%s/api/v1/currencies?output=json&key=%s',
|
|
||||||
$this->currenciesApiUrl(),
|
|
||||||
rawurlencode($apiKey)
|
|
||||||
),
|
|
||||||
'headers' => ['Accept: application/json'],
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'url' => sprintf(
|
'url' => sprintf(
|
||||||
'%s/api/v2/currencies?output=json&key=%s',
|
'%s/api/v2/currencies?output=json&key=%s',
|
||||||
$this->currenciesApiUrl(),
|
$this->currenciesApiUrl(),
|
||||||
rawurlencode($apiKey)
|
rawurlencode($apiKey)
|
||||||
),
|
),
|
||||||
'headers' => ['Accept: application/json'],
|
'headers' => ['Accept: application/json'],
|
||||||
],
|
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -788,16 +765,15 @@ final class FxRatesService
|
|||||||
return rtrim((string) ($this->settings['currencies_url'] ?? $this->apiUrl()), '/');
|
return rtrim((string) ($this->settings['currencies_url'] ?? $this->apiUrl()), '/');
|
||||||
}
|
}
|
||||||
|
|
||||||
private function isCurrencyApiCom(): bool
|
private function apiVersion(): string
|
||||||
{
|
{
|
||||||
foreach ([$this->apiUrl(), $this->currenciesApiUrl()] as $url) {
|
$version = strtolower(trim((string) ($this->settings['api_version'] ?? 'v2')));
|
||||||
$host = strtolower((string) parse_url($url, PHP_URL_HOST));
|
return in_array($version, ['v2', 'v3'], true) ? $version : 'v2';
|
||||||
if ($host !== '' && str_contains($host, 'currencyapi.com')) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
private function usesApiVersion(string $version): bool
|
||||||
|
{
|
||||||
|
return $this->apiVersion() === strtolower(trim($version));
|
||||||
}
|
}
|
||||||
|
|
||||||
private function apiKey(): string
|
private function apiKey(): string
|
||||||
|
|||||||
@@ -523,6 +523,14 @@ $activeDbGroup = $testGroup !== null && array_key_exists($testGroup, $dbGroups)
|
|||||||
<input type="text" name="provider" value="<?= e((string) ($current['provider'] ?? 'currencyapi')) ?>">
|
<input type="text" name="provider" value="<?= e((string) ($current['provider'] ?? 'currencyapi')) ?>">
|
||||||
<small class="muted">Unterstuetzt legacy currencyapi.net und currencyapi.com v3.</small>
|
<small class="muted">Unterstuetzt legacy currencyapi.net und currencyapi.com v3.</small>
|
||||||
</label>
|
</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">
|
<label class="setup-field muted">
|
||||||
<span>FX API URL</span>
|
<span>FX API URL</span>
|
||||||
<input type="text" name="api_url" value="<?= e((string) ($current['api_url'] ?? 'https://currencyapi.net')) ?>">
|
<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>
|
<span>Timeout (Sek.)</span>
|
||||||
<input type="number" name="timeout_sec" value="<?= e((string) ($current['timeout_sec'] ?? 10)) ?>">
|
<input type="number" name="timeout_sec" value="<?= e((string) ($current['timeout_sec'] ?? 10)) ?>">
|
||||||
</label>
|
</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">
|
<label class="setup-field muted">
|
||||||
<span>Standard-Basiswaehrung</span>
|
<span>Standard-Basiswaehrung</span>
|
||||||
<input type="text" name="default_base_currency" value="<?= e((string) ($current['default_base_currency'] ?? 'EUR')) ?>">
|
<input type="text" name="default_base_currency" value="<?= e((string) ($current['default_base_currency'] ?? 'EUR')) ?>">
|
||||||
|
|||||||
Reference in New Issue
Block a user