Skip to content

Commit

Permalink
1. add adapter demo
Browse files Browse the repository at this point in the history
2. add DioHttpHeaders class
  • Loading branch information
duwen committed Mar 1, 2019
1 parent f31f88c commit 15de2ea
Show file tree
Hide file tree
Showing 11 changed files with 601 additions and 70 deletions.
3 changes: 3 additions & 0 deletions README-ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,9 @@ HttpClientAdapter是 Dio 和 HttpClient之间的桥梁。2.0抽象出adapter主

Dio 使用`DefaultHttpClientAdapter`作为其默认HttpClientAdapter,`DefaultHttpClientAdapter`使用`dart:io:HttpClient` 来发起网络请求。

[这里](https://github.com/flutterchina/dio/blob/master/example/adapter.dart) 有一个简单的自定义Adapter的示例,读者可以参考。另外本项目的自动化测试用例全都是通过一个自定义的[MockAdapter](https://github.com/flutterchina/dio/blob/master/package_src/test/mock_adapter.dart)来模拟服务器返回数据的。




### 设置Http代理
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ We can use any HttpClient not just `dart:io:HttpClient` to make the Http request
dio.httpClientAdapter = new DefaultHttpClientAdapter();
```


[Here](https://github.com/flutterchina/dio/blob/master/example/adapter.dart) is a simple example to custom adapter.

### Using proxy

Expand Down
26 changes: 26 additions & 0 deletions example/adapter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:dio/dio.dart';

class MyAdapter extends HttpClientAdapter {
DefaultHttpClientAdapter defaultHttpClientAdapter =
DefaultHttpClientAdapter();

@override
Future<ResponseBody> fetch(RequestOptions options,
Stream<List<int>> requestStream, Future cancelFuture) async {
Uri uri = options.uri;
// hook requests to google.com
if (uri.host == "google.com") {
return ResponseBody.fromString("Too young too simple!", 200, null);
}
return defaultHttpClientAdapter.fetch(options, requestStream, cancelFuture);
}
}

main() async {
var dio = new Dio();
dio.httpClientAdapter = MyAdapter();
Response response = await dio.get("https://google.com");
print(response);
response = await dio.get("https://baidu.com");
print(response);
}
1 change: 1 addition & 0 deletions example/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ main() async {
),
);
dio.interceptors.add(LogInterceptor());

await dio.get(
"/test",
queryParameters: {"kk": "tt"},
Expand Down
1 change: 1 addition & 0 deletions package_src/lib/dio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ export 'src/upload_file_info.dart';
export 'src/interceptors/log.dart';
export 'src/interceptors/cookie_mgr.dart';
export 'src/adapter.dart';
export 'src/dio_http_headers.dart';
12 changes: 4 additions & 8 deletions package_src/lib/src/adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,7 @@ abstract class HttpClientAdapter {
}

class ResponseBody {
ResponseBody(
this.stream,
this.statusCode,
this.headers,
);
ResponseBody(this.stream, this.statusCode, [this.headers]);

/// The response stream
Stream<List<int>> stream;
Expand All @@ -65,11 +61,11 @@ class ResponseBody {
/// Http status code
int statusCode;

ResponseBody.fromString(String text, this.statusCode, this.headers)
ResponseBody.fromString(String text, this.statusCode, [this.headers])
: stream =
Stream.fromIterable(utf8.encode(text).map((e) => [e]).toList());

ResponseBody.fromBytes(List<int> bytes, this.statusCode, this.headers)
ResponseBody.fromBytes(List<int> bytes, this.statusCode, [this.headers])
: stream = Stream.fromIterable(bytes.map((e) => [e]).toList());
}

Expand Down Expand Up @@ -118,7 +114,7 @@ class DefaultHttpClientAdapter extends HttpClientAdapter {
}

HttpClientResponse responseStream = await request.close();
return new ResponseBody(
return ResponseBody(
responseStream,
responseStream.statusCode,
responseStream.headers,
Expand Down
4 changes: 4 additions & 0 deletions package_src/lib/src/dio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'options.dart';
import 'response.dart';
import 'transformer.dart';
import 'adapter.dart';
import 'dio_http_headers.dart';

/// Callback to listen the progress for sending/receiving data.
///
Expand Down Expand Up @@ -719,6 +720,9 @@ class Dio {
stream,
cancelToken?.whenCancel,
);
if(responseBody.headers==null){
responseBody.headers=DioHttpHeaders();
}
Response ret = new Response(
headers: responseBody.headers,
request: options,
Expand Down
Loading

0 comments on commit 15de2ea

Please sign in to comment.