Refactor ForexRateAPI integration to load API key from .env file and enhance error handling
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
@@ -16,79 +18,129 @@ class ForexRateApiService {
|
||||
final http.Client httpClient;
|
||||
final String apiKey;
|
||||
|
||||
void _log(String message) {
|
||||
if (kDebugMode) {
|
||||
debugPrint('[ForexRateApiService] $message');
|
||||
}
|
||||
}
|
||||
|
||||
String _redactedUri(Uri uri) {
|
||||
final redactedQuery = <String, String>{...uri.queryParameters};
|
||||
if (redactedQuery.containsKey('access_key')) {
|
||||
redactedQuery['access_key'] = '***REDACTED***';
|
||||
}
|
||||
return uri.replace(queryParameters: redactedQuery).toString();
|
||||
}
|
||||
|
||||
bool _isRateLimitedResponse(http.Response response) {
|
||||
if (response.statusCode == 429) {
|
||||
return true;
|
||||
}
|
||||
|
||||
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 == 'rate_limit_reached';
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<http.Response> _getWithRateLimitRetry(
|
||||
Uri uri, {
|
||||
required String operation,
|
||||
}) async {
|
||||
const maxAttempts = 3;
|
||||
|
||||
for (var attempt = 1; attempt <= maxAttempts; attempt++) {
|
||||
final response = await httpClient.get(uri);
|
||||
_log('$operation HTTP status: ${response.statusCode}');
|
||||
_log('$operation raw response: ${response.body}');
|
||||
|
||||
final shouldRetry = _isRateLimitedResponse(response);
|
||||
if (!shouldRetry || attempt == maxAttempts) {
|
||||
return response;
|
||||
}
|
||||
|
||||
final delayMs = 1200 * attempt;
|
||||
_log(
|
||||
'$operation rate limited; retrying in ${delayMs}ms (attempt ${attempt + 1}/$maxAttempts)',
|
||||
);
|
||||
await Future.delayed(Duration(milliseconds: delayMs));
|
||||
}
|
||||
|
||||
throw StateError('Unexpected rate-limit retry flow for $operation.');
|
||||
}
|
||||
|
||||
Future<double> fetchUsdToSekRate({required DateTime estDate}) async {
|
||||
if (apiKey == 'REPLACE_WITH_YOUR_FOREXRATE_API_KEY') {
|
||||
throw StateError('ForexRateAPI key is not configured.');
|
||||
if (apiKey == 'REPLACE_WITH_YOUR_EXCHANGE_RATE_API_KEY') {
|
||||
throw StateError('ExchangeratesAPI key is not configured.');
|
||||
}
|
||||
|
||||
final date = DateFormat('yyyy-MM-dd').format(estDate);
|
||||
final uri = Uri.https('api.forexrateapi.com', '/v1/$date', <String, String>{
|
||||
'api_key': apiKey,
|
||||
'base': 'USD',
|
||||
'currencies': 'SEK',
|
||||
});
|
||||
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 response = await httpClient.get(uri);
|
||||
_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(
|
||||
'ExchangeratesAPI request failed (${response.statusCode}).',
|
||||
);
|
||||
}
|
||||
|
||||
final json = jsonDecode(response.body) as Map<String, dynamic>;
|
||||
final success = json['success'] as bool? ?? false;
|
||||
if (!success) {
|
||||
throw StateError('ForexRateAPI returned success=false.');
|
||||
final error = json['error'];
|
||||
_log('Daily rate success=false. error=$error');
|
||||
throw StateError('ExchangeratesAPI 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.');
|
||||
final usdPerEur = rates?['USD'];
|
||||
final sekPerEur = rates?['SEK'];
|
||||
if (usdPerEur == null || sekPerEur == null) {
|
||||
throw StateError('USD/SEK rates were missing in API response.');
|
||||
}
|
||||
|
||||
return (sek as num).toDouble();
|
||||
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.');
|
||||
}
|
||||
|
||||
final usdToSek = sekPerEurValue / usdPerEurValue;
|
||||
|
||||
_log(
|
||||
'Daily rate parsed usdPerEur=$usdPerEurValue sekPerEur=$sekPerEurValue derivedUsdToSek=$usdToSek',
|
||||
);
|
||||
|
||||
return usdToSek;
|
||||
}
|
||||
|
||||
Future<ForexConversionResult> convertUsdToSek({
|
||||
required DateTime estDate,
|
||||
required double amount,
|
||||
}) async {
|
||||
if (apiKey == 'REPLACE_WITH_YOUR_FOREXRATE_API_KEY') {
|
||||
throw StateError('ForexRateAPI key is not configured.');
|
||||
if (apiKey == 'REPLACE_WITH_YOUR_EXCHANGE_RATE_API_KEY') {
|
||||
throw StateError('ExchangeratesAPI key is not configured.');
|
||||
}
|
||||
|
||||
final date = DateFormat('yyyy-MM-dd').format(estDate);
|
||||
final uri =
|
||||
Uri.https('api.forexrateapi.com', '/v1/convert', <String, String>{
|
||||
'api_key': apiKey,
|
||||
'from': 'USD',
|
||||
'to': 'SEK',
|
||||
'amount': amount.toStringAsFixed(2),
|
||||
'date': date,
|
||||
});
|
||||
final quote = await fetchUsdToSekRate(estDate: estDate);
|
||||
final result = quote * amount;
|
||||
|
||||
final response = await httpClient.get(uri);
|
||||
if (response.statusCode != 200) {
|
||||
throw StateError('ForexRateAPI convert failed (${response.statusCode}).');
|
||||
}
|
||||
|
||||
final json = jsonDecode(response.body) as Map<String, dynamic>;
|
||||
final success = json['success'] as bool? ?? false;
|
||||
if (!success) {
|
||||
throw StateError('ForexRateAPI convert returned success=false.');
|
||||
}
|
||||
|
||||
final info = json['info'] as Map<String, dynamic>?;
|
||||
final quote = info?['quote'];
|
||||
final result = json['result'];
|
||||
if (quote == null || result == null) {
|
||||
throw StateError(
|
||||
'ForexRateAPI convert response was missing quote/result.',
|
||||
);
|
||||
}
|
||||
|
||||
return ForexConversionResult(
|
||||
quote: (quote as num).toDouble(),
|
||||
result: (result as num).toDouble(),
|
||||
_log(
|
||||
'Computed conversion from daily rate quote=$quote result=$result amount=$amount date=${DateFormat('yyyy-MM-dd').format(estDate)}',
|
||||
);
|
||||
|
||||
return ForexConversionResult(quote: quote, result: result);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user