onRequest method
Called when the request is about to be sent.
Implementation
@override
void onRequest(
RequestOptions options,
RequestInterceptorHandler handler,
) async {
// Add time when the request has been sent
// for further expiry calculation.
options.extra[CacheResponse.requestSentDate] = DateTime.now();
final cacheOptions = _getCacheOptions(options);
if (_shouldSkip(options, options: cacheOptions)) {
handler.next(options);
return;
}
// Early ends if policy does not require cache lookup.
final policy = cacheOptions.policy;
if (policy != CachePolicy.request && policy != CachePolicy.forceCache) {
handler.next(options);
return;
}
final strategy = await CacheStrategyFactory(
request: options,
cacheResponse: await _loadCacheResponse(options),
cacheOptions: cacheOptions,
).compute();
var cacheResponse = strategy.cacheResponse;
if (cacheResponse != null) {
// Cache hit
// Update cached response if needed
cacheResponse = await _updateCacheResponse(cacheResponse, cacheOptions);
handler.resolve(
cacheResponse.toResponse(options, fromNetwork: false),
true,
);
return;
}
// Requests with conditional request if available
// or requests with given options
handler.next(strategy.request ?? options);
}