request method

Future<Result> request(
  1. String path, {
  2. dynamic data,
  3. Map<String, dynamic>? queryParameters,
  4. CancelToken? cancelToken,
  5. Options? options,
  6. Duration? connectTimeout,
  7. ProgressCallback? onSendProgress,
  8. ProgressCallback? onReceiveProgress,
})

Implementation

Future<Result> request(
  String path, {
  data,
  Map<String, dynamic>? queryParameters,
  CancelToken? cancelToken,
  Options? options,
  Duration? connectTimeout,
  ProgressCallback? onSendProgress,
  ProgressCallback? onReceiveProgress,
}) async {
  final _options = BaseOptions(
    baseUrl: config.baseUrl,
    connectTimeout:
        connectTimeout != null ? connectTimeout : config.connectTimeout,
    receiveTimeout: config.receiveTimeout,
  );
  final Dio _dio = Dio(
    _options,
  )..interceptors.add(
      InterceptorsWrapper(
        onRequest: (options, handler) async {
          if (options.data is FormData) {
            return handler.next(options);
          }
          return handler
              .next(onRequest != null ? await onRequest!(options) : options);
        },
      ),
    );
  if (Config.logEnable) {
    _dio.interceptors.add(
      LogInterceptor(
        requestBody: true,
        responseBody: true,
      ),
    );
  }
  const bool kIsWeb = bool.fromEnvironment('dart.library.js_util');
  try {
    if (kIsWeb) {
      // _dio.httpClientAdapter = BrowserHttpClientAdapter();
    } else {
      _dio.httpClientAdapter = IOHttpClientAdapter(
        createHttpClient: config.createHttpClient,
        validateCertificate: config.badCertificateCallback,
      );
    }
  } catch (e) {
    _printCatchLog(e);
  }

  Response? response;
  Result result = Result(
      response: response,
      body: {},
      data: {},
      list: [],
      message: config.errorUnknown ?? '',
      error: null);
  try {
    response = await _dio.request(path,
        data: data,
        queryParameters: queryParameters,
        cancelToken: cancelToken,
        options: options,
        onSendProgress: onSendProgress,
        onReceiveProgress: onReceiveProgress);
  } on DioException catch (error) {
    var message = "${error.message ?? error.error}";

    if (config.errorUnknown != null) {
      message = config.errorUnknown!;
    }
    switch (error.type) {
      case DioExceptionType.connectionError:
      case DioExceptionType.badCertificate:
        if (config.errorConnection != null) {
          message = config.errorConnection!;
        }
        break;
      case DioExceptionType.badResponse:
        if (config.errorBadResponse != null) {
          message = config.errorBadResponse!;
        }
        break;
      case DioExceptionType.cancel:
        if (config.errorCancel != null) {
          message = config.errorCancel!;
        }
        break;
      case DioExceptionType.connectionTimeout:
      case DioExceptionType.sendTimeout:
      case DioExceptionType.receiveTimeout:
        if (config.errorTimeout != null) {
          message = config.errorTimeout!;
        }
        break;
      case DioExceptionType.unknown:
        break;
    }
    result = Result(
      response: error.response,
      body: (error.response?.data is Map) ? error.response?.data : {},
      code: '',
      data: {},
      list: [],
      message: message,
      error: error.type,
    );
  } catch (e) {
    _printCatchLog(e);
  }
  if (result.error == null) {
    dynamic body = {};
    if (response?.data is Map) {
      body = response?.data ?? {};
    } else if (response?.data is String) {
      try {
        body = json.decode(response?.data);
      } catch (e) {
        _printCatchLog(e);
      }
    }
    if (body is Map) {
      var code = '';
      var data = {};
      var list = [];
      var message = '';
      try {
        code = _getMap(body, config.code).toString();
      } catch (e) {
        _printCatchLog(e);
      }
      try {
        data = _getMap(body, config.data) ?? {};
      } catch (e) {
        _printCatchLog(e);
      }
      try {
        list = _getMap(body, config.list) ?? [];
      } catch (e) {
        _printCatchLog(e);
      }
      try {
        message = _getMap(body, config.message) ?? '';
      } catch (e) {
        _printCatchLog(e);
      }
      result = Result(
          response: response,
          body: body,
          code: code,
          data: data,
          list: list,
          message: message,
          valid: code == config.validCode);
    } else if (response?.data is Result) {
      result = response?.data;
    }
  }

  if (onResult != null) {
    return onResult!(result);
  }
  return result;
}