Can backfill the previous year, although some API rate limiting but it works

Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
2026-03-19 23:55:08 +01:00
parent e6c16967f9
commit d1d1d9fb95
2 changed files with 107 additions and 5 deletions
+72 -1
View File
@@ -1,5 +1,5 @@
import 'dart:convert';
import 'dart:async';
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
@@ -143,4 +143,75 @@ class ForexRateApiService {
return ForexConversionResult(quote: quote, result: result);
}
/// Fetches USD->SEK cross-rates for every day of [year] in a single timeseries
/// 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.');
}
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;
}
}