request method
isFullUrl
- url传入的是否为完整的一个URL,如果为true
,将忽略host
Implementation
@Doc(message: "向服务器发起http请求")
Future<T> request([RequestParams options = const RequestParams()]) async {
final fullUrl = options.fullUrl;
try {
if (options.showDefaultLoading) {
showLoading(loadingText: options.loadingText);
}
final baseOptions = await getOptions(options);
final d = await getDio(baseOptions);
d.interceptors.addAll(
options.interceptorCall?.call(interceptions) ?? interceptions);
final contentTypeStr = options.contentType ??
(httpMethod == HttpMethod.post
? io.ContentType.json.value
: options.contentType);
final bodyParams = formData ?? (options.data ?? params);
final queryParameters = httpMethod == HttpMethod.post
? null
: (options.nullParams == true ? null : options.data ?? params);
final contentTypeString = httpMethod == HttpMethod.probuf
? kProtobufContentType
: contentTypeStr;
final finalUrl = fullUrl ?? (_host + url);
await options.dioStart?.call(d, finalUrl);
var uri = (options.urlParseFormat ?? (v, p) => v)
.call(finalUrl, queryParameters);
final bodyData = httpMethod == HttpMethod.get ? null : bodyParams;
final response = await d.request(uri,
options: dio.Options(
method: httpMethod.method,
contentType: contentTypeString,
headers: options.headers,
responseType: options.responseType,
requestEncoder: options.requestEncoder,
),
queryParameters:
httpMethod == HttpMethod.get ? queryParameters : null,
data: bodyData,
onReceiveProgress: options.onReceiveProgress,
onSendProgress: options.onSendCallback,
cancelToken: options.cancelToken);
options.responseResultCallback?.call(response);
if (options.showDefaultLoading) {
closeLoading();
}
final data = response.data;
var model = DartTypeModel.createFrom(data);
beforeHandleDartTypeModel(model, options, response);
return covertToModel(model, options);
} on dio.DioException catch (e) {
throw BaseApiException.createFromDioException(e);
} finally {
if (options.showDefaultLoading) {
closeLoading();
}
}
}