onRequest method

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

Handles dio request and creates nirikshak http call based on it

Implementation

@override
void onRequest(
  RequestOptions options,
  RequestInterceptorHandler handler,
) {
  final call = NirikshakHttpCall(options.hashCode);

  final uri = options.uri;
  call.method = options.method;
  var path = options.uri.path;
  if (path.isEmpty) {
    path = "/";
  }
  call.endpoint = path;
  call.server = uri.host;
  call.client = "Dio";
  call.uri = options.uri.toString();

  if (uri.scheme == "https") {
    call.secure = true;
  }

  final NirikshakHttpRequest request = NirikshakHttpRequest();

  final data = options.data;
  if (data == null) {
    request.size = 0;
    request.body = "";
  } else {
    if (data is FormData) {
      request.body += "Form data";

      if (data.fields.isNotEmpty == true) {
        List<NirikshakFormDataField> fields = [];
        for (final entry in data.fields) {
          fields.add(NirikshakFormDataField(entry.key, entry.value));
        }
        request.formDataFields = fields;
      }
      if (data.files.isNotEmpty == true) {
        List<NirikshakFormDataFile> files = [];
        for (var entry in data.files) {
          files.add(NirikshakFormDataFile(entry.value.filename ?? '',
              entry.value.contentType.toString(), entry.value.length));
        }

        request.formDataFiles = files;
      }
    } else {
      request.size = utf8.encode(data.toString()).length;
      request.body = data;
    }
  }

  request.time = DateTime.now();
  request.headers = options.headers;
  request.contentType = options.contentType.toString();
  request.queryParameters = options.queryParameters;

  call.request = request;
  call.response = NirikshakHttpResponse();

  nirikshakCore.addCall(call);
  return super.onRequest(options, handler);
}