Update API references from OpenExchange to ForexRateAPI in code and documentation

Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
2026-03-19 20:46:58 +01:00
parent fc3cbb0b5d
commit a1f67e71e2
4 changed files with 95 additions and 27 deletions
+66 -10
View File
@@ -3,30 +3,42 @@ import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:intl/intl.dart';
class OpenExchangeService {
OpenExchangeService({required this.httpClient, required this.apiKey});
class ForexConversionResult {
ForexConversionResult({required this.quote, required this.result});
final double quote;
final double result;
}
class ForexRateApiService {
ForexRateApiService({required this.httpClient, required this.apiKey});
final http.Client httpClient;
final String apiKey;
Future<double> fetchUsdToSekRate({required DateTime estDate}) async {
if (apiKey == 'REPLACE_WITH_YOUR_OPENEXCHANGE_API_KEY') {
throw StateError('OpenExchange API 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(
'openexchangerates.org',
'/api/historical/$date.json',
<String, String>{'app_id': apiKey, 'symbols': 'SEK'},
);
final uri = Uri.https('api.forexrateapi.com', '/v1/$date', <String, String>{
'api_key': apiKey,
'base': 'USD',
'currencies': 'SEK',
});
final response = await httpClient.get(uri);
if (response.statusCode != 200) {
throw StateError('OpenExchange request failed (${response.statusCode}).');
throw StateError('ForexRateAPI 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 rates = json['rates'] as Map<String, dynamic>?;
final sek = rates?['SEK'];
if (sek == null) {
@@ -35,4 +47,48 @@ class OpenExchangeService {
return (sek as num).toDouble();
}
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.');
}
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 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(),
);
}
}