req<T> method
Future<Response<T>>
req<
T>( - Client self,
- String method,
- String path, {
- dynamic data,
- dynamic optionsHandler(
- Options
)?,
- ProgressCallback? onSendProgress,
- ProgressCallback? onReceiveProgress,
- CancelToken? cancelToken,
})
Implementation
Future<Response<T>> req<T>(
Client self,
String method,
String path, {
dynamic data,
Function(Options)? optionsHandler,
ProgressCallback? onSendProgress,
ProgressCallback? onReceiveProgress,
CancelToken? cancelToken,
}) async {
// options
Options options = Options(method: method);
if (options.headers == null) {
options.headers = {};
}
// 二次处理options
if (optionsHandler != null) {
optionsHandler(options);
}
// authorization
String? str = self.auth.authorize(method, path);
if (str != null) {
options.headers?['authorization'] = str;
}
var resp = await this.requestUri<T>(
Uri.parse(
'${path.startsWith(RegExp(r'(http|https)://')) ? path : join(self.uri, path)}'),
options: options,
data: data,
onSendProgress: onSendProgress,
onReceiveProgress: onReceiveProgress,
cancelToken: cancelToken,
);
if (resp.statusCode == 401) {
String? w3AHeader = resp.headers.value('www-authenticate');
String? lowerW3AHeader = w3AHeader?.toLowerCase();
// before is noAuth
if (self.auth.type == AuthType.NoAuth) {
// Digest
if (lowerW3AHeader?.contains('digest') == true) {
self.auth = DigestAuth(
user: self.auth.user,
pwd: self.auth.pwd,
dParts: DigestParts(w3AHeader));
}
// Basic
else if (lowerW3AHeader?.contains('basic') == true) {
self.auth = BasicAuth(user: self.auth.user, pwd: self.auth.pwd);
}
// error
else {
throw newResponseError(resp);
}
}
// before is digest and Nonce Lifetime is out
else if (self.auth.type == AuthType.DigestAuth &&
lowerW3AHeader?.contains('stale=true') == true) {
self.auth = DigestAuth(
user: self.auth.user,
pwd: self.auth.pwd,
dParts: DigestParts(w3AHeader));
} else {
throw newResponseError(resp);
}
// retry
return this.req<T>(
self,
method,
path,
data: data,
optionsHandler: optionsHandler,
onSendProgress: onSendProgress,
onReceiveProgress: onReceiveProgress,
cancelToken: cancelToken,
);
}
return resp;
}