start method
void
start()
Implementation
void 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) {
this.data = MCRequestData(
requestObject: this,
response: Response(
data: mock, requestOptions: RequestOptions(path: "mock")));
this.requestCompleteFilter();
if (success != null) {
success!(this.data);
}
if (this.delegate != null) {
this.delegate!.requestFinished(this);
}
return;
}
try {
if (this.requestMethod() == MCRequestMethod.Get) {
res = await dio.get(path!,
queryParameters: this.requestArgument(),
cancelToken: this._token,
onReceiveProgress: this.onReceiveProgress);
} else if (this.requestMethod() == MCRequestMethod.Post) {
res = await dio.post(path!,
data: this.requestArgument(),
cancelToken: this._token,
onSendProgress: this.onSendProgress,
onReceiveProgress: this.onReceiveProgress);
} else if (this.requestMethod() == MCRequestMethod.Head) {
res = await dio.head(path!,
queryParameters: this.requestArgument(), cancelToken: this._token);
} else if (this.requestMethod() == MCRequestMethod.Put) {
res = await dio.put(path!,
queryParameters: this.requestArgument(),
cancelToken: this._token,
onSendProgress: this.onSendProgress,
onReceiveProgress: this.onReceiveProgress);
} else if (this.requestMethod() == MCRequestMethod.Delete) {
res = await dio.delete(path!,
queryParameters: this.requestArgument(), cancelToken: this._token);
} else if (this.requestMethod() == MCRequestMethod.Patch) {
res = await dio.patch(path!,
queryParameters: this.requestArgument(),
cancelToken: this._token,
onSendProgress: this.onSendProgress,
onReceiveProgress: this.onReceiveProgress);
} else if (this.requestMethod() == MCRequestMethod.Download) {
res = await dio.download(path!, this.savePath(),
queryParameters: this.requestArgument(),
cancelToken: this._token,
onReceiveProgress: this.onReceiveProgress);
}
} on DioError catch (e) {
this.data = MCRequestData(requestObject: this, error: e);
this.requestCompleteFilter();
if (this.failure != null) {
this.failure!(this.data);
}
if (this.delegate != null) {
this.delegate!.requestFailed(this);
}
return;
}
if (res == null) {
this.data = MCRequestData(
requestObject: this,
error: DioError(requestOptions: RequestOptions(path: "res_null")));
this.requestCompleteFilter();
if (this.failure != null) {
this.failure!(this.data);
}
if (this.delegate != null) {
this.delegate!.requestFailed(this);
}
return;
} else {
this.data = MCRequestData(requestObject: this, response: res);
this.requestCompleteFilter();
if (success != null) {
success!(this.data);
}
if (this.delegate != null) {
this.delegate!.requestFinished(this);
}
return;
}
}