onRequest method

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

Handles dio request and creates alice http call based on it

Implementation

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

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

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

  final request = AliceHttpRequest();

  final dynamic data = options.data;
  if (data == null) {
    request
      ..size = 0
      ..body = '';
  } else {
    if (data is FormData) {
      // ignore: avoid_dynamic_calls
      request.body += 'Form data';

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

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

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

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

  aliceCore.addCall(call);
  handler.next(options);
}