Skip to content

Commit c49eb72

Browse files
author
duwen
committed
1 parent f8a2caa commit c49eb72

File tree

8 files changed

+349
-335
lines changed

8 files changed

+349
-335
lines changed

.idea/libraries/Dart_Packages.xml

Lines changed: 48 additions & 48 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 227 additions & 258 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ dio.cookieJar=new PersistCookieJar("./cookies");
507507
`PersistCookieJar` is a cookie manager which implements the standard cookie policy declared in RFC. `PersistCookieJar` persists the cookies in files, so if the application exit, the cookies always exist unless call `delete` explicitly.
508508

509509

510-
More details about [cookie_jar](https://github.com/flutterchina/) see : https://github.com/flutterchina/cookie_jar .
510+
More details about [cookie_jar](https://github.com/flutterchina/cookie_jar) see : https://github.com/flutterchina/cookie_jar .
511511

512512
## Copyright & License
513513

issue_template.md

Whitespace-only changes.

lib/src/Dio.dart

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import 'package:dio/src/TransFormer.dart';
1818
/// such as response data is compressed with gzip.
1919
typedef OnDownloadProgress(int received, int total);
2020

21-
typedef OnHttpClientCreate(HttpClient client);
21+
typedef dynamic OnHttpClientCreate(HttpClient client);
2222

2323
/// A powerful Http client for Dart, which supports Interceptors,
2424
/// Global configuration, FormData, File downloading etc. and Dio is
@@ -175,7 +175,7 @@ class Dio {
175175
OnDownloadProgress onProgress,
176176
CancelToken cancelToken,
177177
data,
178-
bool flush: false,
178+
@deprecated bool flush: false,
179179
Options options,
180180
}) async {
181181
// We set the `responseType` to [ResponseType.STREAM] to retrieve the
@@ -216,11 +216,11 @@ class Dio {
216216
new Duration(
217217
milliseconds: options.receiveTimeout),
218218
onTimeout: (EventSink sink) {
219-
return new Future<Response>
220-
.error(new DioError(
219+
sink.addError(new DioError(
221220
message: "Receiving data timeout[${options.receiveTimeout}ms]",
222221
type: DioErrorType.RECEIVE_TIMEOUT
223222
));
223+
sink.close();
224224
},
225225
);
226226
}
@@ -282,7 +282,8 @@ class Dio {
282282
_configHttpClient(HttpClient httpClient, [bool isDefault = false]) {
283283
httpClient.idleTimeout = new Duration(seconds: isDefault ? 3 : 0);
284284
if (onHttpClientCreate != null) {
285-
onHttpClientCreate(httpClient);
285+
//user can return a new HttpClient instance
286+
httpClient= onHttpClientCreate(httpClient)??httpClient;
286287
}
287288
}
288289

@@ -407,9 +408,7 @@ class Dio {
407408

408409
Future<Response<T>> future = _checkIfNeedEnqueue<T>(interceptor.response, () {
409410
_checkCancelled(cancelToken);
410-
if ((response.statusCode >= HttpStatus.OK &&
411-
response.statusCode < HttpStatus.MULTIPLE_CHOICES) ||
412-
response.statusCode == HttpStatus.NOT_MODIFIED) {
411+
if (options.validateStatus(response.statusCode)) {
413412
return _listenCancelForAsyncTask<Response<T>>(
414413
cancelToken, _onSuccess<T>(ret));
415414
} else {
@@ -492,6 +491,8 @@ class Dio {
492491
_setHeaders(options, request);
493492

494493
request.write(data);
494+
}else{
495+
_setHeaders(options, request);
495496
}
496497
}
497498

@@ -533,17 +534,18 @@ class Dio {
533534
return _transFutureStatusIfNecessary<T>(err);
534535
}
535536

536-
_mergeOptions(Options opt) {
537+
Options _mergeOptions(Options opt) {
537538
opt.method ??= options.method ?? "GET";
538539
opt.method = opt.method.toUpperCase();
539-
opt.headers.addAll(options.headers);
540+
opt.headers=(new Map.from(options.headers))..addAll(opt.headers);
540541
opt.baseUrl ??= options.baseUrl ?? "";
541542
opt.connectTimeout ??= options.connectTimeout ?? 0;
542543
opt.receiveTimeout ??= options.receiveTimeout ?? 0;
543544
opt.responseType ??= options.responseType ?? ResponseType.JSON;
544545
opt.data ??= options.data;
545-
opt.extra.addAll(options.extra);
546+
opt.extra=(new Map.from(options.extra))..addAll(opt.extra);
546547
opt.contentType ??= options.contentType ?? ContentType.JSON;
548+
opt.validateStatus??=options.validateStatus;
547549
}
548550

549551
Options _checkOptions(method, options) {

lib/src/Options.dart

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,60 @@ enum ResponseType {
1414
PLAIN
1515
}
1616

17+
typedef bool ValidateStatus(int status);
18+
1719
/**
1820
* The Options class describes the http request information and configuration.
1921
*/
20-
class Options{
21-
Options({this.method,
22+
class Options {
23+
Options({
24+
this.method,
2225
this.baseUrl,
2326
this.connectTimeout,
2427
this.receiveTimeout,
28+
this.path,
2529
this.data,
2630
this.extra,
2731
this.headers,
2832
this.responseType,
29-
this.contentType}) {
33+
this.contentType,
34+
this.validateStatus
35+
}) {
3036
// set the default user-agent with Dio version
3137
this.headers = headers ?? {};
3238
this.contentType;
3339
this.extra = extra ?? {};
40+
this.validateStatus ??=
41+
(int status) => status >= 200 && status < 300 || status == 304;
42+
}
43+
44+
/// Create a new Option from current instance with merging attributes.
45+
Options merge({
46+
String method,
47+
String baseUrl,
48+
String path,
49+
int connectTimeout,
50+
int receiveTimeout,
51+
var data,
52+
Map<String, dynamic> extra,
53+
Map<String, dynamic> headers,
54+
ResponseType responseType,
55+
ContentType contentType,
56+
ValidateStatus validateStatus
57+
}) {
58+
return new Options(
59+
method: method??this.method,
60+
baseUrl: baseUrl??this.baseUrl,
61+
path: path??this.path,
62+
connectTimeout: connectTimeout??this.connectTimeout,
63+
receiveTimeout: receiveTimeout??this.receiveTimeout,
64+
data: data??this.data,
65+
extra: extra??this.extra??{},
66+
headers: headers??this.headers??{},
67+
responseType: responseType??this.responseType,
68+
contentType: contentType??this.contentType,
69+
validateStatus: validateStatus??this.validateStatus
70+
);
3471
}
3572

3673
/// Http method.
@@ -76,6 +113,12 @@ class Options{
76113
/// If you want to receive the response data with String, use `PLAIN`.
77114
ResponseType responseType;
78115

116+
/// `validateStatus` defines whether the request is successful for a given
117+
/// HTTP response status code. If `validateStatus` returns `true` ,
118+
/// the request will be perceived as successful; otherwise, considered as failed.
119+
ValidateStatus validateStatus;
120+
79121
/// Custom field that you can retrieve it later in [Interceptor][TransFormer] and the [Response] object.
80122
Map<String, dynamic> extra;
123+
81124
}

lib/src/TransFormer.dart

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ abstract class TransFormer {
6666
class DefaultTransformer extends TransFormer {
6767

6868
Future transformRequest(Options options) async {
69-
var data = options.data;
69+
var data = options.data??"";
7070
if (data is! String) {
7171
if (options.contentType.mimeType == ContentType.JSON.mimeType) {
72-
return JSON.encode(options.data);
72+
return json.encode(options.data);
7373
} else if (data is Map) {
7474
return TransFormer.urlEncodeMap(data);
7575
}
@@ -89,18 +89,18 @@ class DefaultTransformer extends TransFormer {
8989
stream = stream.timeout(
9090
new Duration(milliseconds: options.receiveTimeout),
9191
onTimeout: (EventSink sink) {
92-
return new Future<Response>.error(
93-
new DioError(
94-
message: "Receiving data timeout[${options
95-
.receiveTimeout}ms]",
96-
type: DioErrorType.RECEIVE_TIMEOUT,
97-
));
92+
sink.addError( new DioError(
93+
message: "Receiving data timeout[${options
94+
.receiveTimeout}ms]",
95+
type: DioErrorType.RECEIVE_TIMEOUT,
96+
));
97+
sink.close();
9898
});
9999
}
100-
String responseBody = await stream.transform(UTF8.decoder).join();
100+
String responseBody = await stream.transform(utf8.decoder).join();
101101
if (options.responseType == ResponseType.JSON &&
102-
response.headers.contentType.mimeType == ContentType.JSON.mimeType) {
103-
return JSON.decode(responseBody);
102+
response.headers.contentType?.mimeType == ContentType.JSON.mimeType) {
103+
return json.decode(responseBody);
104104
}
105105
return responseBody;
106106
}

pubspec.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ version: 0.0.9
44
homepage: https://github.com/flutterchina/dio
55
author: wendux <[email protected]>
66

7-
environment:
8-
sdk: '>=1.20.1 <2.0.0'
7+
#environment:
8+
# sdk: '>=1.9.0'
99

1010
dependencies:
11-
cookie_jar: ^0.0.3
11+
cookie_jar: ^0.0.4
1212

1313
dev_dependencies:
1414
test: "^0.12.0"

0 commit comments

Comments
 (0)