onRequest method
Called when the request is about to be sent.
Implementation
@override
Future<void> onRequest(
RequestOptions options,
RequestInterceptorHandler handler,
) async {
final plan = _engine.planRequest(
options.method,
options.uri,
requestHeaders: _stringHeaders(options.headers),
);
if (plan.passThrough) {
options.extra[_startKey] = _nowMicros();
handler.next(options);
return;
}
if (plan.latency > Duration.zero) {
await Future<void>.delayed(plan.latency);
}
final failure = plan.failure;
if (failure != null) {
handler.reject(_failureException(options, failure, plan.latency), true);
return;
}
// Upload bandwidth delay based on the request body size.
final requestBytes = _bytesOf(options.headers, options.data);
final uploadDelay = _engine.bandwidthDelay(requestBytes, upload: true);
if (uploadDelay > Duration.zero) {
await Future<void>.delayed(uploadDelay);
}
options.extra[_planKey] = plan.latency + uploadDelay;
if (plan.tampering != null) {
options.extra[_tamperKey] = plan.tampering;
}
handler.next(options);
}