diff --git a/lib/services/open_exchange_service.dart b/lib/services/open_exchange_service.dart index ae85b6d..281f535 100644 --- a/lib/services/open_exchange_service.dart +++ b/lib/services/open_exchange_service.dart @@ -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 _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', { 'api_key': apiKey, 'base': 'USD', diff --git a/lib/state/rent_controller.dart b/lib/state/rent_controller.dart index bc26927..c44432f 100644 --- a/lib/state/rent_controller.dart +++ b/lib/state/rent_controller.dart @@ -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 =