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(
|
Future<http.Response> _getWithRateLimitRetry(
|
||||||
Uri uri, {
|
Uri uri, {
|
||||||
required String operation,
|
required String operation,
|
||||||
@@ -78,7 +100,8 @@ class ForexRateApiService {
|
|||||||
throw StateError('ForexRateAPI key is not configured.');
|
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>{
|
final uri = Uri.https('api.forexrateapi.com', '/v1/$date', <String, String>{
|
||||||
'api_key': apiKey,
|
'api_key': apiKey,
|
||||||
'base': 'USD',
|
'base': 'USD',
|
||||||
|
|||||||
@@ -386,6 +386,7 @@ class RentController extends ChangeNotifier {
|
|||||||
} catch (err) {
|
} catch (err) {
|
||||||
final errorText = err.toString();
|
final errorText = err.toString();
|
||||||
if (errorText.contains('429') ||
|
if (errorText.contains('429') ||
|
||||||
|
errorText.contains('104') ||
|
||||||
errorText.toLowerCase().contains('rate_limit_reached')) {
|
errorText.toLowerCase().contains('rate_limit_reached')) {
|
||||||
pausedByRateLimit = true;
|
pausedByRateLimit = true;
|
||||||
_errorMessage =
|
_errorMessage =
|
||||||
|
|||||||
Reference in New Issue
Block a user