Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dio.clone #2095

Merged
merged 8 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dio/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ See the [Migration Guide][] for the complete breaking changes list.**
- Support `FileAccessMode` in `Dio.download` and `Dio.downloadUri` to change download file opening mode
- Fix `ListParam` equality by using the `DeepCollectionEquality`.
- Enables configuring the logging details of `DioException` globally and locally.
- Enables using `Dio.clone` to reuse base options, client adapter, interceptors, and transformer,
in a new `Dio` instance.

## 5.7.0

Expand Down
31 changes: 22 additions & 9 deletions dio/README-ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ dio 是一个强大的 HTTP 网络请求库,支持全局配置、Restful API
* [相关插件](#相关插件)
* [相关的项目](#相关的项目)
* [示例](#示例)
* [发起一个 `GET` 请求 :](#发起一个-get-请求-)
* [发起一个 `POST` 请求:](#发起一个-post-请求)
* [发起多个并发请求](#发起多个并发请求)
* [下载文件](#下载文件)
* [以流的方式接收响应数据](#以流的方式接收响应数据)
* [以二进制数组的方式接收响应数据](#以二进制数组的方式接收响应数据)
* [发送 `FormData`](#发送-formdata)
* [通过 `FormData` 上传多个文件](#通过-formdata-上传多个文件)
* [监听发送(上传)数据进度](#监听发送上传数据进度)
* [以流的形式提交二进制数据](#以流的形式提交二进制数据)
* [Dio APIs](#dio-apis)
* [创建一个Dio实例,并配置它](#创建一个dio实例并配置它)
* [请求配置](#请求配置)
Expand All @@ -40,7 +50,7 @@ dio 是一个强大的 HTTP 网络请求库,支持全局配置、Restful API
* [DioException](#dioexception)
* [DioExceptionType](#dioexceptiontype)
* [使用 application/x-www-form-urlencoded 编码](#使用-applicationx-www-form-urlencoded-编码)
* [发送 FormData](#发送-formdata)
* [发送 FormData](#发送-formdata-1)
* [多文件上传](#多文件上传)
* [复用 `FormData` 和 `MultipartFile`](#复用-formdata-和-multipartfile)
* [转换器](#转换器)
Expand Down Expand Up @@ -136,13 +146,13 @@ void request() async {
response = await dio.post('/test', data: {'id': 12, 'name': 'dio'});
```

### 发起多个并发请求:
### 发起多个并发请求

```dart
response = await Future.wait([dio.post('/info'), dio.get('/token')]);
```

### 下载文件:
### 下载文件

```dart
response = await dio.download(
Expand All @@ -151,7 +161,7 @@ response = await dio.download(
);
```

### 以流的方式接收响应数据
### 以流的方式接收响应数据

```dart
final rs = await dio.get(
Expand All @@ -161,7 +171,7 @@ final rs = await dio.get(
print(rs.data.stream); // 响应流
```

### 以二进制数组的方式接收响应数据
### 以二进制数组的方式接收响应数据

```dart
final rs = await dio.get(
Expand All @@ -171,7 +181,7 @@ final rs = await dio.get(
print(rs.data); // 类型: List<int>
```

### 发送 `FormData`:
### 发送 `FormData`

```dart
final formData = FormData.fromMap({
Expand All @@ -181,7 +191,7 @@ final formData = FormData.fromMap({
final response = await dio.post('/info', data: formData);
```

### 通过 `FormData` 上传多个文件:
### 通过 `FormData` 上传多个文件

```dart
final formData = FormData.fromMap({
Expand All @@ -196,7 +206,7 @@ final formData = FormData.fromMap({
final response = await dio.post('/info', data: formData);
```

### 监听发送(上传)数据进度:
### 监听发送(上传)数据进度

```dart
final response = await dio.post(
Expand All @@ -208,7 +218,7 @@ final response = await dio.post(
);
```

### 以流的形式提交二进制数据
### 以流的形式提交二进制数据

```dart
// Binary data
Expand Down Expand Up @@ -255,6 +265,9 @@ void configureDio() {
receiveTimeout: Duration(seconds: 3),
);
final anotherDio = Dio(options);

// Or clone the existing `Dio` instance with all fields.
final clonedDio = dio.clone();
}
```

Expand Down
37 changes: 25 additions & 12 deletions dio/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,18 @@ Timeout, Custom adapters, Transformers, etc.
* [Awesome dio](#awesome-dio)
* [Plugins](#plugins)
* [Examples](#examples)
* [Performing a `GET` request](#performing-a-get-request)
* [Performing a `POST` request](#performing-a-post-request)
* [Performing multiple concurrent requests](#performing-multiple-concurrent-requests)
* [Downloading a file](#downloading-a-file)
* [Get response stream](#get-response-stream)
* [Get response with bytes](#get-response-with-bytes)
* [Sending a `FormData`](#sending-a-formdata)
* [Uploading multiple files to server by FormData](#uploading-multiple-files-to-server-by-formdata)
* [Listening the uploading progress](#listening-the-uploading-progress)
* [Post binary data with Stream](#post-binary-data-with-stream)
* [Dio APIs](#dio-apis)
* [Creating an instance and set default configs.](#creating-an-instance-and-set-default-configs)
* [Creating an instance and set default configs](#creating-an-instance-and-set-default-configs)
* [Request Options](#request-options)
* [Response](#response)
* [Interceptors](#interceptors)
Expand Down Expand Up @@ -94,7 +104,7 @@ in [here](https://github.com/cfug/dio/issues/347).

## Examples

### Performing a `GET` request:
### Performing a `GET` request

```dart
import 'package:dio/dio.dart';
Expand All @@ -114,19 +124,19 @@ void request() async {
}
```

### Performing a `POST` request:
### Performing a `POST` request

```dart
response = await dio.post('/test', data: {'id': 12, 'name': 'dio'});
```

### Performing multiple concurrent requests:
### Performing multiple concurrent requests

```dart
response = await Future.wait([dio.post('/info'), dio.get('/token')]);
```

### Downloading a file:
### Downloading a file

```dart
response = await dio.download(
Expand All @@ -135,7 +145,7 @@ response = await dio.download(
);
```

### Get response stream:
### Get response stream

```dart
final rs = await dio.get(
Expand All @@ -145,7 +155,7 @@ final rs = await dio.get(
print(rs.data.stream); // Response stream.
```

### Get response with bytes:
### Get response with bytes

```dart
final rs = await Dio().get<List<int>>(
Expand All @@ -155,7 +165,7 @@ final rs = await Dio().get<List<int>>(
print(rs.data); // Type: List<int>.
```

### Sending a `FormData`:
### Sending a `FormData`

```dart
final formData = FormData.fromMap({
Expand All @@ -165,7 +175,7 @@ final formData = FormData.fromMap({
final response = await dio.post('/info', data: formData);
```

### Uploading multiple files to server by FormData:
### Uploading multiple files to server by FormData

```dart
final formData = FormData.fromMap({
Expand All @@ -180,7 +190,7 @@ final formData = FormData.fromMap({
final response = await dio.post('/info', data: formData);
```

### Listening the uploading progress:
### Listening the uploading progress

```dart
final response = await dio.post(
Expand All @@ -192,7 +202,7 @@ final response = await dio.post(
);
```

### Post binary data with Stream:
### Post binary data with Stream

```dart
// Binary data
Expand All @@ -214,7 +224,7 @@ See all examples code [here](example).

## Dio APIs

### Creating an instance and set default configs.
### Creating an instance and set default configs

> It is recommended to use a singleton of `Dio` in projects, which can manage configurations like headers, base urls,
> and timeouts consistently.
Expand All @@ -238,6 +248,9 @@ void configureDio() {
receiveTimeout: Duration(seconds: 3),
);
final anotherDio = Dio(options);

// Or clone the existing `Dio` instance with all fields.
final clonedDio = dio.clone();
}
```

Expand Down
8 changes: 8 additions & 0 deletions dio/lib/src/dio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,12 @@ abstract class Dio {
/// The eventual method to submit requests. All callers for requests should
/// eventually go through this method.
Future<Response<T>> fetch<T>(RequestOptions requestOptions);

/// Clones a new [Dio] instance with override fields or reuses current fields.
Dio clone({
BaseOptions? options,
Interceptors? interceptors,
HttpClientAdapter? httpClientAdapter,
Transformer? transformer,
});
}
15 changes: 15 additions & 0 deletions dio/lib/src/dio_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,21 @@ abstract class DioMixin implements Dio {
}
return response;
}

@override
Dio clone({
BaseOptions? options,
Interceptors? interceptors,
HttpClientAdapter? httpClientAdapter,
Transformer? transformer,
}) {
final dio = Dio(options ?? this.options);
dio.interceptors.removeImplyContentTypeInterceptor();
dio.interceptors.addAll(interceptors ?? this.interceptors);
dio.httpClientAdapter = httpClientAdapter ?? this.httpClientAdapter;
dio.transformer = transformer ?? this.transformer;
return dio;
}
}

/// A null-check function for function parameters in Null Safety enabled code.
Expand Down
36 changes: 36 additions & 0 deletions dio/test/dio_mixin_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:typed_data';

import 'package:dio/dio.dart';
import 'package:test/test.dart';

Expand Down Expand Up @@ -28,8 +30,42 @@ void main() {
throwsA(const TypeMatcher<UnimplementedError>()),
);
});

test('cloned', () {
final dio = Dio();
final cloned = dio.clone();
expect(dio == cloned, false);
expect(dio.options, equals(cloned.options));
expect(dio.interceptors, equals(cloned.interceptors));
expect(dio.httpClientAdapter, equals(cloned.httpClientAdapter));
expect(dio.transformer, equals(cloned.transformer));
final clonedWithFields = dio.clone(
options: BaseOptions(baseUrl: 'http://localhost'),
interceptors: Interceptors()..add(InterceptorsWrapper()),
httpClientAdapter: _TestAdapter(),
transformer: SyncTransformer(),
);
expect(clonedWithFields.options.baseUrl, equals('http://localhost'));
expect(clonedWithFields.interceptors.length, equals(2));
expect(clonedWithFields.httpClientAdapter, isA<_TestAdapter>());
expect(clonedWithFields.transformer, isA<SyncTransformer>());
});
}

class _TestDioMixin with DioMixin implements Dio {}

class _TestDioMixinExtends extends DioMixin implements Dio {}

class _TestAdapter implements HttpClientAdapter {
@override
Future<ResponseBody> fetch(
RequestOptions options,
Stream<Uint8List>? requestStream,
Future<void>? cancelFuture,
) {
throw UnimplementedError();
}

@override
void close({bool force = false}) {}
}
Loading