onRequest method

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

Called when the request is about to be sent.

Implementation

@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
  final requestId = generateRequestId();
  options.extra[requestIdKey] = requestId;
  var httpInfo = HttpInfo1(options.uri, options.method, requestId);
  httpInfo.request = HttpRequest(
    header: options.headers,
    queryParameters: options.queryParameters,
    body: options.data,
  );
  final kit = ApmKitManager.instance.getKit(ApmKitName.kitHttp);
  kit?.save(httpInfo);
  var mockResponse = MockLocalTool.parseMockData(options);
  if (mockResponse != null) {
    handler.resolve(mockResponse);
    _filterByRequestId(requestId, (httpInfo) {
      httpInfo?.response = HttpResponse(
        statusCode: mockResponse.statusCode,
        data: mockResponse.data,
        header: mockResponse.headers.map,
      );
      httpInfo?.isMock = true;
    });
    return;
  } else {
    handler.next(options);
  }
}