onRequest method
The callback will be executed before the request is initiated.
If you want to continue the request, call handler.next
.
If you want to complete the request with some custom data,
you can resolve a Response
object with handler.resolve
.
If you want to complete the request with an error message,
you can reject a DioError
object with handler.reject
.
Implementation
@override
void onRequest(
RequestOptions options, RequestInterceptorHandler handler) async {
DioCacheOptions? dioCacheOptions =
MapHelper.getDioCacheOptionsFromExtras(options) ??
globalDioCacheOptions;
if (dioCacheOptions?.dioCacheMethod == DioCacheMethod.noCache) {
return super.onRequest(options, handler);
} else if (dioCacheOptions?.dioCacheMethod == DioCacheMethod.cacheOnly) {
String? data =
await Fnc().baseDb.getResponseData(key: _getStorageUrl(options));
if (data == null) {
return super.onRequest(options, handler);
}
return handler.resolve(
Response(requestOptions: options, data: json.decode(data)), true);
} else if (dioCacheOptions?.dioCacheMethod ==
DioCacheMethod.emitLastResponse) {
String? data =
await Fnc().baseDb.getResponseData(key: _getStorageUrl(options));
if (data == null) {
return super.onRequest(options, handler);
}
try {
loadAsyncRequest(options, _dio);
return handler.resolve(
Response(requestOptions: options, data: json.decode(data)), true);
} catch (e) {
FncLogger.logFnc(e.toString());
}
} else {
return super.onRequest(options, handler);
}
}