Add date normalization methods to ForexRateApiService and handle additional error code in RentController

Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
2026-03-20 10:05:52 +01:00
parent 4c10d617a6
commit 5e961d10cb
2 changed files with 25 additions and 1 deletions
+24 -1
View File
@@ -47,6 +47,28 @@ class ForexRateApiService {
}
}
DateTime _asUtcDate(DateTime value) {
return DateTime.utc(value.year, value.month, value.day);
}
DateTime _latestHistoricalDateUtc() {
final nowUtc = DateTime.now().toUtc();
final todayUtcDate = DateTime.utc(nowUtc.year, nowUtc.month, nowUtc.day);
return todayUtcDate.subtract(const Duration(days: 1));
}
DateTime _normalizeHistoricalDate(DateTime requestedDate) {
final requestedUtcDate = _asUtcDate(requestedDate);
final latestAllowed = _latestHistoricalDateUtc();
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)}.',
);
return latestAllowed;
}
return requestedUtcDate;
}
Future<http.Response> _getWithRateLimitRetry(
Uri uri, {
required String operation,
@@ -78,7 +100,8 @@ class ForexRateApiService {
throw StateError('ForexRateAPI key is not configured.');
}
final date = DateFormat('yyyy-MM-dd').format(estDate);
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,
'base': 'USD',
+1
View File
@@ -386,6 +386,7 @@ class RentController extends ChangeNotifier {
} catch (err) {
final errorText = err.toString();
if (errorText.contains('429') ||
errorText.contains('104') ||
errorText.toLowerCase().contains('rate_limit_reached')) {
pausedByRateLimit = true;
_errorMessage =