|
|
|
@@ -26,8 +26,8 @@ class ForexRateApiService {
|
|
|
|
|
|
|
|
|
|
String _redactedUri(Uri uri) {
|
|
|
|
|
final redactedQuery = <String, String>{...uri.queryParameters};
|
|
|
|
|
if (redactedQuery.containsKey('access_key')) {
|
|
|
|
|
redactedQuery['access_key'] = '***REDACTED***';
|
|
|
|
|
if (redactedQuery.containsKey('api_key')) {
|
|
|
|
|
redactedQuery['api_key'] = '***REDACTED***';
|
|
|
|
|
}
|
|
|
|
|
return uri.replace(queryParameters: redactedQuery).toString();
|
|
|
|
|
}
|
|
|
|
@@ -74,26 +74,22 @@ class ForexRateApiService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<double> fetchUsdToSekRate({required DateTime estDate}) async {
|
|
|
|
|
if (apiKey == 'REPLACE_WITH_YOUR_EXCHANGE_RATE_API_KEY') {
|
|
|
|
|
throw StateError('ExchangeratesAPI key is not configured.');
|
|
|
|
|
if (apiKey == 'REPLACE_WITH_YOUR_FOREXRATE_API_KEY') {
|
|
|
|
|
throw StateError('ForexRateAPI key is not configured.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final date = DateFormat('yyyy-MM-dd').format(estDate);
|
|
|
|
|
final uri = Uri.https(
|
|
|
|
|
'api.exchangeratesapi.io',
|
|
|
|
|
'/v1/$date',
|
|
|
|
|
// Free-tier plans can restrict custom base currencies.
|
|
|
|
|
// We request EUR-based USD and SEK rates and derive USD->SEK via cross-rate.
|
|
|
|
|
<String, String>{'access_key': apiKey, 'symbols': 'USD,SEK'},
|
|
|
|
|
);
|
|
|
|
|
final uri = Uri.https('api.forexrateapi.com', '/v1/$date', <String, String>{
|
|
|
|
|
'api_key': apiKey,
|
|
|
|
|
'base': 'USD',
|
|
|
|
|
'currencies': 'SEK',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
_log('Daily rate request: ${_redactedUri(uri)}');
|
|
|
|
|
|
|
|
|
|
final response = await _getWithRateLimitRetry(uri, operation: 'Daily rate');
|
|
|
|
|
if (response.statusCode != 200) {
|
|
|
|
|
throw StateError(
|
|
|
|
|
'ExchangeratesAPI request failed (${response.statusCode}).',
|
|
|
|
|
);
|
|
|
|
|
throw StateError('ForexRateAPI request failed (${response.statusCode}).');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final json = jsonDecode(response.body) as Map<String, dynamic>;
|
|
|
|
@@ -101,37 +97,26 @@ class ForexRateApiService {
|
|
|
|
|
if (!success) {
|
|
|
|
|
final error = json['error'];
|
|
|
|
|
_log('Daily rate success=false. error=$error');
|
|
|
|
|
throw StateError('ExchangeratesAPI returned success=false. error=$error');
|
|
|
|
|
throw StateError('ForexRateAPI returned success=false. error=$error');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final rates = json['rates'] as Map<String, dynamic>?;
|
|
|
|
|
final usdPerEur = rates?['USD'];
|
|
|
|
|
final sekPerEur = rates?['SEK'];
|
|
|
|
|
if (usdPerEur == null || sekPerEur == null) {
|
|
|
|
|
throw StateError('USD/SEK rates were missing in API response.');
|
|
|
|
|
final sek = rates?['SEK'];
|
|
|
|
|
if (sek == null) {
|
|
|
|
|
throw StateError('SEK rate was missing in API response.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final usdPerEurValue = (usdPerEur as num).toDouble();
|
|
|
|
|
final sekPerEurValue = (sekPerEur as num).toDouble();
|
|
|
|
|
if (usdPerEurValue == 0) {
|
|
|
|
|
throw StateError('USD rate was zero; cannot derive USD->SEK.');
|
|
|
|
|
}
|
|
|
|
|
_log('Daily rate parsed SEK quote: $sek');
|
|
|
|
|
|
|
|
|
|
final usdToSek = sekPerEurValue / usdPerEurValue;
|
|
|
|
|
|
|
|
|
|
_log(
|
|
|
|
|
'Daily rate parsed usdPerEur=$usdPerEurValue sekPerEur=$sekPerEurValue derivedUsdToSek=$usdToSek',
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return usdToSek;
|
|
|
|
|
return (sek as num).toDouble();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<ForexConversionResult> convertUsdToSek({
|
|
|
|
|
required DateTime estDate,
|
|
|
|
|
required double amount,
|
|
|
|
|
}) async {
|
|
|
|
|
if (apiKey == 'REPLACE_WITH_YOUR_EXCHANGE_RATE_API_KEY') {
|
|
|
|
|
throw StateError('ExchangeratesAPI key is not configured.');
|
|
|
|
|
if (apiKey == 'REPLACE_WITH_YOUR_FOREXRATE_API_KEY') {
|
|
|
|
|
throw StateError('ForexRateAPI key is not configured.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final quote = await fetchUsdToSekRate(estDate: estDate);
|
|
|
|
@@ -148,70 +133,9 @@ class ForexRateApiService {
|
|
|
|
|
/// API call. Returns a map of 'yyyy-MM-dd' -> rate, or null if the timeseries
|
|
|
|
|
/// endpoint is not available on the current subscription plan.
|
|
|
|
|
Future<Map<String, double>?> tryFetchYearRates(int year) async {
|
|
|
|
|
if (apiKey == 'REPLACE_WITH_YOUR_EXCHANGE_RATE_API_KEY') {
|
|
|
|
|
throw StateError('ExchangeratesAPI key is not configured.');
|
|
|
|
|
if (apiKey == 'REPLACE_WITH_YOUR_FOREXRATE_API_KEY') {
|
|
|
|
|
throw StateError('ForexRateAPI key is not configured.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final uri =
|
|
|
|
|
Uri.https('api.exchangeratesapi.io', '/v1/timeseries', <String, String>{
|
|
|
|
|
'access_key': apiKey,
|
|
|
|
|
'start_date': '$year-01-01',
|
|
|
|
|
'end_date': '$year-12-31',
|
|
|
|
|
'symbols': 'USD,SEK',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
_log('Timeseries request: ${_redactedUri(uri)}');
|
|
|
|
|
|
|
|
|
|
final response = await _getWithRateLimitRetry(uri, operation: 'Timeseries');
|
|
|
|
|
|
|
|
|
|
if (response.statusCode == 403) {
|
|
|
|
|
_log(
|
|
|
|
|
'Timeseries endpoint not available on current plan; falling back to daily requests.',
|
|
|
|
|
);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (response.statusCode != 200) {
|
|
|
|
|
throw StateError(
|
|
|
|
|
'ExchangeratesAPI timeseries failed (${response.statusCode}).',
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final json = jsonDecode(response.body) as Map<String, dynamic>;
|
|
|
|
|
final success = json['success'] as bool? ?? false;
|
|
|
|
|
if (!success) {
|
|
|
|
|
final error = json['error'] as Map<String, dynamic>?;
|
|
|
|
|
final code = error?['code']?.toString() ?? '';
|
|
|
|
|
if (code == 'function_access_restricted' ||
|
|
|
|
|
code == 'subscription_plan_not_support_endpoint') {
|
|
|
|
|
_log(
|
|
|
|
|
'Timeseries endpoint not available on current plan; falling back to daily requests.',
|
|
|
|
|
);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
_log('Timeseries success=false. error=$error');
|
|
|
|
|
throw StateError(
|
|
|
|
|
'ExchangeratesAPI timeseries returned success=false. error=$error',
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final rates = json['rates'] as Map<String, dynamic>?;
|
|
|
|
|
if (rates == null) {
|
|
|
|
|
throw StateError('Timeseries response missing rates object.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final result = <String, double>{};
|
|
|
|
|
for (final entry in rates.entries) {
|
|
|
|
|
final date = entry.key;
|
|
|
|
|
final dayRates = entry.value as Map<String, dynamic>?;
|
|
|
|
|
final usdPerEur = (dayRates?['USD'] as num?)?.toDouble();
|
|
|
|
|
final sekPerEur = (dayRates?['SEK'] as num?)?.toDouble();
|
|
|
|
|
if (usdPerEur != null && sekPerEur != null && usdPerEur != 0) {
|
|
|
|
|
result[date] = sekPerEur / usdPerEur;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_log('Timeseries parsed ${result.length} rate entries for year $year');
|
|
|
|
|
return result;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|