Refactor ForexRateApiService to remove API key dependency and update README and launch configurations
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
@@ -5,18 +5,17 @@ import 'package:flutter/foundation.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
class ForexConversionResult {
|
||||
ForexConversionResult({required this.quote, required this.result});
|
||||
class ConversionResult {
|
||||
ConversionResult({required this.quote, required this.result});
|
||||
|
||||
final double quote;
|
||||
final double result;
|
||||
}
|
||||
|
||||
class ForexRateApiService {
|
||||
ForexRateApiService({required this.httpClient, required this.apiKey});
|
||||
class FrankfurterApiService {
|
||||
FrankfurterApiService({required this.httpClient});
|
||||
|
||||
final http.Client httpClient;
|
||||
final String apiKey;
|
||||
|
||||
void _log(String message) {
|
||||
if (kDebugMode) {
|
||||
@@ -24,13 +23,7 @@ class ForexRateApiService {
|
||||
}
|
||||
}
|
||||
|
||||
String _redactedUri(Uri uri) {
|
||||
final redactedQuery = <String, String>{...uri.queryParameters};
|
||||
if (redactedQuery.containsKey('api_key')) {
|
||||
redactedQuery['api_key'] = '***REDACTED***';
|
||||
}
|
||||
return uri.replace(queryParameters: redactedQuery).toString();
|
||||
}
|
||||
String _redactedUri(Uri uri) => uri.toString();
|
||||
|
||||
bool _isRateLimitedResponse(http.Response response) {
|
||||
if (response.statusCode == 429) {
|
||||
@@ -40,8 +33,8 @@ class ForexRateApiService {
|
||||
try {
|
||||
final parsed = jsonDecode(response.body) as Map<String, dynamic>;
|
||||
final error = parsed['error'] as Map<String, dynamic>?;
|
||||
final code = error?['code']?.toString().toLowerCase();
|
||||
return code == '104' || code == 'rate_limit_reached';
|
||||
final statusCode = error?['statusCode']?.toString();
|
||||
return statusCode == '429';
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
@@ -51,18 +44,17 @@ class ForexRateApiService {
|
||||
return DateTime.utc(value.year, value.month, value.day);
|
||||
}
|
||||
|
||||
DateTime _latestHistoricalDateUtc() {
|
||||
DateTime _todayUtcDate() {
|
||||
final nowUtc = DateTime.now().toUtc();
|
||||
final todayUtcDate = DateTime.utc(nowUtc.year, nowUtc.month, nowUtc.day);
|
||||
return todayUtcDate.subtract(const Duration(days: 1));
|
||||
return DateTime.utc(nowUtc.year, nowUtc.month, nowUtc.day);
|
||||
}
|
||||
|
||||
DateTime _normalizeHistoricalDate(DateTime requestedDate) {
|
||||
final requestedUtcDate = _asUtcDate(requestedDate);
|
||||
final latestAllowed = _latestHistoricalDateUtc();
|
||||
final latestAllowed = _todayUtcDate();
|
||||
if (requestedUtcDate.isAfter(latestAllowed)) {
|
||||
_log(
|
||||
'Historical date ${DateFormat('yyyy-MM-dd').format(requestedUtcDate)} is not available yet; clamping to ${DateFormat('yyyy-MM-dd').format(latestAllowed)}.',
|
||||
'Requested date ${DateFormat('yyyy-MM-dd').format(requestedUtcDate)} is in the future; clamping to ${DateFormat('yyyy-MM-dd').format(latestAllowed)}.',
|
||||
);
|
||||
return latestAllowed;
|
||||
}
|
||||
@@ -96,37 +88,25 @@ class ForexRateApiService {
|
||||
}
|
||||
|
||||
Future<double> fetchUsdToSekRate({required DateTime estDate}) async {
|
||||
if (apiKey == 'REPLACE_WITH_YOUR_FOREXRATE_API_KEY') {
|
||||
throw StateError('ForexRateAPI key is not configured.');
|
||||
}
|
||||
|
||||
final effectiveDate = _normalizeHistoricalDate(estDate);
|
||||
final date = DateFormat('yyyy-MM-dd').format(effectiveDate);
|
||||
final uri = Uri.https('api.forexrateapi.com', '/v1/$date', <String, String>{
|
||||
'api_key': apiKey,
|
||||
final uri = Uri.https('api.frankfurter.dev', '/v1/$date', <String, String>{
|
||||
'base': 'USD',
|
||||
'currencies': 'SEK',
|
||||
'symbols': 'SEK',
|
||||
});
|
||||
|
||||
_log('Daily rate request: ${_redactedUri(uri)}');
|
||||
|
||||
final response = await _getWithRateLimitRetry(uri, operation: 'Daily rate');
|
||||
if (response.statusCode != 200) {
|
||||
throw StateError('ForexRateAPI request failed (${response.statusCode}).');
|
||||
throw StateError('Frankfurter request 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'];
|
||||
_log('Daily rate success=false. error=$error');
|
||||
throw StateError('ForexRateAPI returned success=false. error=$error');
|
||||
}
|
||||
|
||||
final rates = json['rates'] as Map<String, dynamic>?;
|
||||
final sek = rates?['SEK'];
|
||||
if (sek == null) {
|
||||
throw StateError('SEK rate was missing in API response.');
|
||||
throw StateError('SEK rate was missing in Frankfurter response.');
|
||||
}
|
||||
|
||||
_log('Daily rate parsed SEK quote: $sek');
|
||||
@@ -134,14 +114,10 @@ class ForexRateApiService {
|
||||
return (sek as num).toDouble();
|
||||
}
|
||||
|
||||
Future<ForexConversionResult> convertUsdToSek({
|
||||
Future<ConversionResult> convertUsdToSek({
|
||||
required DateTime estDate,
|
||||
required double amount,
|
||||
}) async {
|
||||
if (apiKey == 'REPLACE_WITH_YOUR_FOREXRATE_API_KEY') {
|
||||
throw StateError('ForexRateAPI key is not configured.');
|
||||
}
|
||||
|
||||
final quote = await fetchUsdToSekRate(estDate: estDate);
|
||||
final result = quote * amount;
|
||||
|
||||
@@ -149,52 +125,35 @@ class ForexRateApiService {
|
||||
'Computed conversion from daily rate quote=$quote result=$result amount=$amount date=${DateFormat('yyyy-MM-dd').format(estDate)}',
|
||||
);
|
||||
|
||||
return ForexConversionResult(quote: quote, result: result);
|
||||
return ConversionResult(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.
|
||||
/// Fetches USD->SEK cross-rates for [year] using Frankfurter's date-range
|
||||
/// endpoint. Returns a map of 'yyyy-MM-dd' -> rate.
|
||||
Future<Map<String, double>?> tryFetchYearRates(int year) async {
|
||||
if (apiKey == 'REPLACE_WITH_YOUR_FOREXRATE_API_KEY') {
|
||||
throw StateError('ForexRateAPI key is not configured.');
|
||||
}
|
||||
|
||||
final start = DateTime(year, 1, 1);
|
||||
final end = DateTime(year, 12, 31);
|
||||
final uri =
|
||||
Uri.https('api.forexrateapi.com', '/v1/timeframe', <String, String>{
|
||||
'api_key': apiKey,
|
||||
'base': 'USD',
|
||||
'currencies': 'SEK',
|
||||
'start_date': DateFormat('yyyy-MM-dd').format(start),
|
||||
'end_date': DateFormat('yyyy-MM-dd').format(end),
|
||||
});
|
||||
final rangePath =
|
||||
'/v1/${DateFormat('yyyy-MM-dd').format(start)}..${DateFormat('yyyy-MM-dd').format(end)}';
|
||||
final uri = Uri.https('api.frankfurter.dev', rangePath, <String, String>{
|
||||
'base': 'USD',
|
||||
'symbols': 'SEK',
|
||||
});
|
||||
|
||||
_log('Timeframe request: ${_redactedUri(uri)}');
|
||||
_log('Range request: ${_redactedUri(uri)}');
|
||||
|
||||
final response = await _getWithRateLimitRetry(uri, operation: 'Timeframe');
|
||||
final response = await _getWithRateLimitRetry(uri, operation: 'Range');
|
||||
if (response.statusCode != 200) {
|
||||
_log(
|
||||
'Timeframe endpoint unavailable for this account or request; falling back to per-day requests. status=${response.statusCode}',
|
||||
'Range endpoint unavailable; falling back to per-day requests. status=${response.statusCode}',
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
final json = jsonDecode(response.body) as Map<String, dynamic>;
|
||||
final success = json['success'] as bool? ?? false;
|
||||
if (!success) {
|
||||
_log(
|
||||
'Timeframe success=false; falling back to per-day requests. error=${json['error']}',
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
final rates = json['rates'] as Map<String, dynamic>?;
|
||||
if (rates == null) {
|
||||
_log(
|
||||
'Timeframe response missing rates; falling back to per-day requests.',
|
||||
);
|
||||
_log('Range response missing rates; falling back to per-day requests.');
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -208,13 +167,11 @@ class ForexRateApiService {
|
||||
}
|
||||
|
||||
if (results.isEmpty) {
|
||||
_log(
|
||||
'Timeframe parsed no SEK entries; falling back to per-day requests.',
|
||||
);
|
||||
_log('Range parsed no SEK entries; falling back to per-day requests.');
|
||||
return null;
|
||||
}
|
||||
|
||||
_log('Timeframe parsed ${results.length} daily SEK quotes.');
|
||||
_log('Range parsed ${results.length} daily SEK quotes.');
|
||||
return results;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user