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

Commit

Permalink
Allow asynchronized method with savePath (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 authored Dec 16, 2022
1 parent acbf115 commit d3363cc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
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);
});
}

0 comments on commit d3363cc

Please sign in to comment.