request method

void request(
  1. RequestBuilder builder, {
  2. CancelToken? cancelToken,
})

Implementation

void request(RequestBuilder builder, {CancelToken? cancelToken}) async {
  try {
    if (builder.getUrl() == "") {
      throw "请求url为null";
    }
    Options options = Options();
    options.method = builder.getHttpRequestMethod();
    if (builder.getHeaders() != null) {
      options.headers = builder.getHeaders();
    }

    Object? dataParams;
    Map<String, dynamic>? queryParams;
    if (builder.getHttpRequestMethodTyp() == HttpRequestMethod.get ||
        builder.getHttpRequestMethodTyp() == HttpRequestMethod.download) {
      queryParams =_getFinalRequestParams(builder.getParams(),builder.getIsFilterRequestParamsNullValue());
    } else {
      if(builder.getHttpRequestParam()==HttpRequestParam.body){
        dataParams = _getFinalRequestParams(builder.getParams(),builder.getIsFilterRequestParamsNullValue());
      }else{
        queryParams = _getFinalRequestParams(builder.getParams(),builder.getIsFilterRequestParamsNullValue());
      }
    }

    Response response;

    if (builder.getHttpRequestMethodTyp() == HttpRequestMethod.download) {
      if (builder.getSavePath() == null) {
        throw "savePath为null";
      } else {
        response = await _dio
            .download(builder.getUrl(), builder.getSavePath(),
                onReceiveProgress: (count, total) {
          if (builder.receiveProgress != null) {
            builder.receiveProgress!(count, total);
          }
        },cancelToken: cancelToken);
      }
    }else{
      response = await _dio.request(builder.getUrl(),
          data: dataParams,
          queryParameters: queryParams,
          options: options,
          cancelToken: cancelToken);
    }

    if (response.statusCode == 200) {
      builder.success(response.data);
    } else {
      var exception =
          BaseException(response.statusCode, BaseException.unknownException);
      exception.stackInfo = response.data.toString();
      builder.fail(exception);
    }
  } catch (e) {
    var exception = BaseException.fromJson(e);
    builder.fail(exception);
  }
}