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
+2 -2
View File
@@ -23,10 +23,10 @@ Simple Flutter app to track US rental income and convert to SEK for Swedish tax
## Setup ## Setup
1. Set your Open Exchange Rates app key in `lib/state/rent_controller.dart`: 1. Set your FX API app key in `lib/state/rent_controller.dart`:
```dart ```dart
const String openExchangeApiKey = 'REPLACE_WITH_YOUR_OPENEXCHANGE_API_KEY'; const String forexRateApiKey = 'REPLACE_WITH_YOUR_FOREXRATE_API_KEY';
``` ```
1. Install dependencies: 1. Install dependencies:
+1 -1
View File
@@ -112,7 +112,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
), ),
const SizedBox(height: 16), const SizedBox(height: 16),
const Text( const Text(
'OpenExchange API key is configured in code constant openExchangeApiKey.', 'ForexRateAPI key is configured in code constant forexRateApiKey.',
), ),
], ],
), ),
+66 -10
View File
@@ -3,30 +3,42 @@ import 'dart:convert';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
class OpenExchangeService { class ForexConversionResult {
OpenExchangeService({required this.httpClient, required this.apiKey}); 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 http.Client httpClient;
final String apiKey; final String apiKey;
Future<double> fetchUsdToSekRate({required DateTime estDate}) async { Future<double> fetchUsdToSekRate({required DateTime estDate}) async {
if (apiKey == 'REPLACE_WITH_YOUR_OPENEXCHANGE_API_KEY') { if (apiKey == 'REPLACE_WITH_YOUR_FOREXRATE_API_KEY') {
throw StateError('OpenExchange API key is not configured.'); throw StateError('ForexRateAPI key is not configured.');
} }
final date = DateFormat('yyyy-MM-dd').format(estDate); final date = DateFormat('yyyy-MM-dd').format(estDate);
final uri = Uri.https( final uri = Uri.https('api.forexrateapi.com', '/v1/$date', <String, String>{
'openexchangerates.org', 'api_key': apiKey,
'/api/historical/$date.json', 'base': 'USD',
<String, String>{'app_id': apiKey, 'symbols': 'SEK'}, 'currencies': 'SEK',
); });
final response = await httpClient.get(uri); final response = await httpClient.get(uri);
if (response.statusCode != 200) { 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 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 rates = json['rates'] as Map<String, dynamic>?;
final sek = rates?['SEK']; final sek = rates?['SEK'];
if (sek == null) { if (sek == null) {
@@ -35,4 +47,48 @@ class OpenExchangeService {
return (sek as num).toDouble(); 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(),
);
}
} }
+26 -14
View File
@@ -13,7 +13,7 @@ import 'package:rental_income_tracker/services/storage_service.dart';
import 'package:rental_income_tracker/services/time_service.dart'; import 'package:rental_income_tracker/services/time_service.dart';
import 'package:timezone/timezone.dart' as tz; import 'package:timezone/timezone.dart' as tz;
const String openExchangeApiKey = 'REPLACE_WITH_YOUR_OPENEXCHANGE_API_KEY'; const String forexRateApiKey = 'REPLACE_WITH_YOUR_FOREXRATE_API_KEY';
class RentTableRow { class RentTableRow {
RentTableRow({ RentTableRow({
@@ -40,19 +40,19 @@ class RentTableRow {
class RentController extends ChangeNotifier { class RentController extends ChangeNotifier {
RentController({ RentController({
StorageService? storageService, StorageService? storageService,
OpenExchangeService? exchangeService, ForexRateApiService? exchangeService,
}) : _storageService = storageService ?? StorageService(), }) : _storageService = storageService ?? StorageService(),
_exchangeService = _exchangeService =
exchangeService ?? exchangeService ??
OpenExchangeService( ForexRateApiService(
httpClient: http.Client(), httpClient: http.Client(),
apiKey: openExchangeApiKey, apiKey: forexRateApiKey,
) { ) {
_notificationService = NotificationService(_onNotificationAction); _notificationService = NotificationService(_onNotificationAction);
} }
final StorageService _storageService; final StorageService _storageService;
final OpenExchangeService _exchangeService; final ForexRateApiService _exchangeService;
late NotificationService _notificationService; late NotificationService _notificationService;
AppSettings _settings = AppSettings.defaults(); AppSettings _settings = AppSettings.defaults();
@@ -146,12 +146,21 @@ class RentController extends ChangeNotifier {
throw StateError('Set rent amount in Settings before marking paid.'); throw StateError('Set rent amount in Settings before marking paid.');
} }
final key = TimeService.monthKey(year, month);
if (_records.containsKey(key)) {
await _notificationService.cancelForMonth(year, month);
await _syncNotificationScheduleForCurrentMonth();
return;
}
final paidAt = DateTime.now().toUtc(); final paidAt = DateTime.now().toUtc();
final usd = _settings.rentUsd;
final estPaidAt = tz.TZDateTime.from(paidAt, TimeService.est); final estPaidAt = tz.TZDateTime.from(paidAt, TimeService.est);
final estDate = DateTime(estPaidAt.year, estPaidAt.month, estPaidAt.day); final estDate = DateTime(estPaidAt.year, estPaidAt.month, estPaidAt.day);
final rate = await _exchangeService.fetchUsdToSekRate(estDate: estDate); final conversion = await _exchangeService.convertUsdToSek(
final usd = _settings.rentUsd; estDate: estDate,
final sek = usd * rate; amount: usd,
);
final onTime = final onTime =
assumeOnTime || TimeService.isOnTimeByDeadline(paidAt, year, month); assumeOnTime || TimeService.isOnTimeByDeadline(paidAt, year, month);
@@ -159,8 +168,8 @@ class RentController extends ChangeNotifier {
year: year, year: year,
month: month, month: month,
usdAmount: usd, usdAmount: usd,
sekAmount: sek, sekAmount: conversion.result,
usdToSekRate: rate, usdToSekRate: conversion.quote,
paidAtUtc: paidAt, paidAtUtc: paidAt,
onTime: onTime, onTime: onTime,
source: source, source: source,
@@ -194,9 +203,12 @@ class RentController extends ChangeNotifier {
continue; continue;
} }
final estDate = DateTime(year, month, 1);
final rate = await _exchangeService.fetchUsdToSekRate(estDate: estDate);
final usd = _settings.rentUsd; final usd = _settings.rentUsd;
final estDate = DateTime(year, month, 1);
final conversion = await _exchangeService.convertUsdToSek(
estDate: estDate,
amount: usd,
);
final paidAtUtc = tz.TZDateTime( final paidAtUtc = tz.TZDateTime(
TimeService.est, TimeService.est,
@@ -210,8 +222,8 @@ class RentController extends ChangeNotifier {
year: year, year: year,
month: month, month: month,
usdAmount: usd, usdAmount: usd,
sekAmount: usd * rate, sekAmount: conversion.result,
usdToSekRate: rate, usdToSekRate: conversion.quote,
paidAtUtc: paidAtUtc, paidAtUtc: paidAtUtc,
onTime: true, onTime: true,
source: 'backfill', source: 'backfill',