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:
@@ -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',
|
||||
|
||||
Reference in New Issue
Block a user