onRequest method
Called when the request is about to be sent.
Implementation
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) async {
void registerIdleObs() {
if (_sseCacheDeliverer.idleObserver == null) {
_updateActiveTimeStamp();
_sseCacheDeliverer.setIdleObserver(() {
int idleInterval = DateTime.now().millisecondsSinceEpoch - _sseActiveTimeStamp;
if (idleInterval > _idleTimeout) {
if (idleInterval > _exceptionTimeout) {
_changeConnectState(ConnectState.connectException);
} else {
_changeConnectState(ConnectState.connectIdle);
}
}
});
}
}
bool checkShouldConnectState(String requestUrl) {
slog.df("check should connect state url $requestUrl", tag: tag);
if (_sseConfig!.unCheckConnectStatePaths != null) {
for (String path in _sseConfig!.unCheckConnectStatePaths!) {
if (requestUrl.contains(path)) {
slog.df("state detection should not be performed $requestUrl", tag: tag);
return false;
}
}
}
return true;
}
if (options.isStream() && _streamTransforming.value) {
slog.d("reject request url ${options.uri}, because stream is transforming", tag: tag);
handler.reject(DioException(
error: _errorTransformingMsg,
requestOptions: options,
message: "stream is transforming"));
} else {
if (options.isStream()) {
slog.df("onRequest invoke url ${options.uri}", tag: tag);
_sseBridge.work();
if (checkShouldConnectState(options.uri.toString())) {
registerIdleObs();
}
_disconnect();
_streamTransforming.value = true;
if (options.isOfflineStream()) {
slog.df("request was offline mode ${options.uri}", tag: tag);
// Don't care the actual content of the ResponseBody
// because the data read in onResponse comes from external settings.(SseOfflineProvider)
handler.resolve(
Response(requestOptions: options, data: ResponseBody.fromString('', 200)), true);
return;
}
}
handler.next(options);
}
}