Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
duwen committed Aug 20, 2018
1 parent 40709c0 commit 83809e5
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 289 deletions.
499 changes: 220 additions & 279 deletions .idea/workspace.xml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion README-ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ print(statusCode);
};
dio.interceptor.response.onError = (DioError e){
// 当请求失败时做一些预处理
return DioError;//continue
return e;//continue
}
```

Expand Down
29 changes: 29 additions & 0 deletions example/interceptorLock.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,35 @@ main() async {
return options;
}
};

dio.interceptor.response.onError = (DioError error) {
// Assume 401 stands for token expired
if(error.response?.statusCode==401){
Options options=error.response.request;
// If the token has been updated, repeat directly.
if(csrfToken!=options.headers["csrfToken"]){
options.headers["csrfToken"]=csrfToken;
//repeat
return dio.request(options.path,options: options);
}
// update token and repeat
// Lock to block the incoming request until the token updated
dio.interceptor.request.lock();
dio.interceptor.response.lock();
return tokenDio.get("/token").then((d) {
//update csrfToken
options.headers["csrfToken"] = csrfToken = d.data['data']['token'];
}).whenComplete((){
dio.interceptor.request.unlock();
dio.interceptor.response.unlock();
}).then((e){
//repeat
return dio.request(options.path,options: options);
});
}
return error;
};

_onResult(d){
print("request ok!");
}
Expand Down
10 changes: 5 additions & 5 deletions lib/src/Dio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,11 @@ class Dio {
CancelToken cancelToken,
Options options,
}) async {
var httpClient = _httpClient;
var httpClient =_httpClient ;
if (cancelToken != null) {
httpClient = new HttpClient();
_configHttpClient(httpClient);
httpClient=_configHttpClient(new HttpClient());
} else if (!_httpClientInited) {
_configHttpClient(httpClient, true);
_httpClient=httpClient=_configHttpClient(_httpClient, true);
_httpClientInited = true;
}
return _request<T>(path,
Expand All @@ -283,12 +282,13 @@ class Dio {
);
}

_configHttpClient(HttpClient httpClient, [bool isDefault = false]) {
HttpClient _configHttpClient(HttpClient httpClient, [bool isDefault = false]) {
httpClient.idleTimeout = new Duration(seconds: isDefault ? 3 : 0);
if (onHttpClientCreate != null) {
//user can return a new HttpClient instance
httpClient = onHttpClientCreate(httpClient) ?? httpClient;
}
return httpClient;
}

Future<Response<T>> _request<T>(String path,
Expand Down
4 changes: 2 additions & 2 deletions lib/src/Options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ class Options {
connectTimeout: connectTimeout??this.connectTimeout,
receiveTimeout: receiveTimeout??this.receiveTimeout,
data: data??this.data,
extra: extra??this.extra??{},
headers: headers??this.headers??{},
extra: extra??new Map.from(this.extra??{}),
headers: headers??new Map.from(this.headers??{}),
responseType: responseType??this.responseType,
contentType: contentType??this.contentType,
validateStatus: validateStatus??this.validateStatus
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
name: dio
description: A powerful Http client for Dart, which supports Interceptors, FormData, Request Cancellation, File Downloading, Timeout etc.
version: 0.1.8
version: 1.0.1
homepage: https://github.com/flutterchina/dio
author: wendux <[email protected]>

environment:
sdk: '>=1.9.0 <3.0.0'

dependencies:
cookie_jar: ^0.0.4
cookie_jar: ^0.0.6

dev_dependencies:
test: "^0.12.0"
Expand Down

0 comments on commit 83809e5

Please sign in to comment.