Skip to content
This repository has been archived by the owner on Feb 13, 2023. It is now read-only.

🚀 Allow asynchronized method with savePath #36

Merged
merged 3 commits into from
Dec 16, 2022
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
1 change: 1 addition & 0 deletions dio/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 5.0.0-dev.1

- Allow asynchronized method with `savePath`.
- Allow `data` in all request methods.
- A platform independent `HttpClientAdapter` can now be instantiated by doing
`dio.httpClientAdapter = HttpClientAdapter();`.
Expand Down
6 changes: 3 additions & 3 deletions dio/lib/src/dio/dio_for_native.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,19 @@ class DioForNative with DioMixin implements Dio {
rethrow;
}
final File file;
if (savePath is String Function(Headers)) {
if (savePath is FutureOr<String> Function(Headers)) {
// Add real Uri and redirect information to headers.
response.headers
..add('redirects', response.redirects.length.toString())
..add('uri', response.realUri.toString());
file = File(savePath(response.headers));
file = File(await savePath(response.headers));
} else if (savePath is String) {
file = File(savePath);
} else {
throw ArgumentError.value(
savePath.runtimeType,
'savePath',
'The type must be `String` or `String Function(Headers)`.',
'The type must be `String` or `FutureOr<String> Function(Headers)`.',
);
}

Expand Down
16 changes: 16 additions & 0 deletions dio/test/download_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'dart:io';
import 'package:dio/dio.dart';
import 'package:test/test.dart';

import 'mock/adapters.dart';
import 'utils.dart';

void main() {
Expand Down Expand Up @@ -93,4 +94,19 @@ void main() {
);
//print(r);
});

test('Test `savePath` types', () async {
Object? error;
final dio = Dio()
..options.baseUrl = EchoAdapter.mockBase
..httpClientAdapter = EchoAdapter();
try {
await dio.download('/test', 'testPath');
await dio.download('/test', (headers) => 'testPath');
await dio.download('/test', (headers) async => 'testPath');
} catch (e) {
error = e;
}
expect(error, null);
});
}