onRequest method

  1. @override
Future<void> onRequest(
  1. RequestOptions options,
  2. RequestInterceptorHandler handler
)

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);

  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;
  handler.next(options);
}