start method

Future? start()

Implementation

Future? start() async {
  dio.options.connectTimeout = this.connectTimeout();
  dio.options.receiveTimeout = this.receiveTimeout();
  dio.options.sendTimeout = this.sendTimeout();
  dio.options.contentType = this.contentType();
  dio.options.responseType = this.responseType();
  dio.options.headers = this.setHeader();
  dio.interceptors.clear();
  this.customInterceptorAdd();
  if (_requestAccessories.length > 0) {
    dio.interceptors
        .add(MCRequestAccessoryInterceptors(accessory: _requestAccessories));
  }
  if (this.isLog() == true) {
    dio.interceptors.add(LogInterceptor(
      responseBody: true,
      requestBody: true,
    )); //开启请求日志
  }
  String? path = "";
  if (this.requestUrl()!.startsWith("http") ||
      this.requestUrl()!.startsWith("ftp")) {
    path = this.requestUrl();
  } else {
    path = "${this.baseUrl()}${this.requestUrl()}";
  }
  // Map param = this.requestArgument();
  // Response? res;

  dynamic mock = await this.mock();
  if (mock != null) {
    MCRequestData data = MCRequestData(
        requestObject: this,
        response: Response(
            data: mock, requestOptions: RequestOptions(path: "mock")));
    this.requestCompleteFilter();
    if (success != null) {
      success!(data);
    }
    if (this.delegate != null) {
      this.delegate!.requestFinished(this);
    }
    return;
  }

  try {
    if (this.requestMethod() == MCRequestMethod.Get) {
      response = await dio.get(path!,
          queryParameters: this.requestArgument(),
          cancelToken: this._token,
          onReceiveProgress: this.onReceiveProgress);
    } else if (this.requestMethod() == MCRequestMethod.Post) {
      response = await dio.post(path!,
          data: this.requestArgument(),
          cancelToken: this._token,
          onSendProgress: this.onSendProgress,
          onReceiveProgress: this.onReceiveProgress);
    } else if (this.requestMethod() == MCRequestMethod.Head) {
      response = await dio.head(path!,
          queryParameters: this.requestArgument(), cancelToken: this._token);
    } else if (this.requestMethod() == MCRequestMethod.Put) {
      response = await dio.put(path!,
          queryParameters: this.requestArgument(),
          cancelToken: this._token,
          onSendProgress: this.onSendProgress,
          onReceiveProgress: this.onReceiveProgress);
    } else if (this.requestMethod() == MCRequestMethod.Delete) {
      response = await dio.delete(path!,
          queryParameters: this.requestArgument(), cancelToken: this._token);
    } else if (this.requestMethod() == MCRequestMethod.Patch) {
      response = await dio.patch(path!,
          queryParameters: this.requestArgument(),
          cancelToken: this._token,
          onSendProgress: this.onSendProgress,
          onReceiveProgress: this.onReceiveProgress);
    } else if (this.requestMethod() == MCRequestMethod.Download) {
      response = await dio.download(path!, this.savePath(),
          queryParameters: this.requestArgument(),
          cancelToken: this._token,
          onReceiveProgress: this.onReceiveProgress);
    }
  } on DioError catch (e) {
    dioError = e;
    MCRequestData data = MCRequestData(requestObject: this, error: e);
    this.requestCompleteFilter();
    if (this.failure != null) {
      this.failure!(data);
      clearCompletionBlock();
    }
    if (this.delegate != null) {
      this.delegate!.requestFailed(this);
    }
    return;
  }
  if (response == null) {
    DioError e = DioError(requestOptions: RequestOptions(path: "res_null"));
    dioError = e;
    MCRequestData data = MCRequestData(requestObject: this, error: e);
    this.requestCompleteFilter();
    if (this.failure != null) {
      this.failure!(data);
      clearCompletionBlock();
    }
    if (this.delegate != null) {
      this.delegate!.requestFailed(this);
    }
    return;
  } else {
    MCRequestData data =
        MCRequestData(requestObject: this, response: response);
    this.requestCompleteFilter();
    if (success != null) {
      success!(data);
      clearCompletionBlock();
    }
    if (this.delegate != null) {
      this.delegate!.requestFinished(this);
    }
    return;
  }
}