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

Remove DioMixin #2081

Draft
wants to merge 4 commits into
base: 6.0.0
Choose a base branch
from
Draft
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
4 changes: 3 additions & 1 deletion dio/lib/browser.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export 'src/adapters/browser_adapter.dart' show BrowserHttpClientAdapter;
export 'src/dio/dio_for_browser.dart' show DioForBrowser;
export 'src/adapters/download/browser_download_adapter.dart'
show BrowserDownloadAdapter;
export 'src/dio.dart' show Dio;
6 changes: 3 additions & 3 deletions dio/lib/dio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
/// {@category Plugins}
library dio;

export 'src/adapter.dart';
export 'src/adapters/adapter.dart';
export 'src/adapters/download/download_adapter.dart';
export 'src/cancel_token.dart';
export 'src/dio.dart';
export 'src/dio.dart' hide InterceptorState, InterceptorResultType;
export 'src/dio_exception.dart';
export 'src/dio_mixin.dart' hide InterceptorState, InterceptorResultType;
export 'src/form_data.dart';
export 'src/headers.dart';
export 'src/interceptors/log.dart';
Expand Down
3 changes: 2 additions & 1 deletion dio/lib/io.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export 'src/adapters/io_adapter.dart' hide createAdapter;
export 'src/dio/dio_for_native.dart' show DioForNative;
export 'src/adapters/download/io_download_adapter.dart' hide createAdapter;
export 'src/dio.dart' show Dio;
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import 'dart:convert';
import 'dart:typed_data';

import 'options.dart';
import 'redirect_record.dart';
import '../options.dart';
import '../redirect_record.dart';

import 'adapters/io_adapter.dart'
if (dart.library.html) 'adapters/browser_adapter.dart' as adapter;
import 'io_adapter.dart' if (dart.library.html) 'browser_adapter.dart'
as adapter;

/// [HttpAdapter] is a bridge between [Dio] and [HttpClient].
///
Expand Down
2 changes: 1 addition & 1 deletion dio/lib/src/adapters/browser_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'dart:typed_data';

import 'package:meta/meta.dart';

import '../adapter.dart';
import 'adapter.dart';
import '../dio_exception.dart';
import '../headers.dart';
import '../options.dart';
Expand Down
29 changes: 29 additions & 0 deletions dio/lib/src/adapters/download/browser_download_adapter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import '../../cancel_token.dart';
import '../../headers.dart';
import '../../options.dart';
import '../../response.dart';
import '../../transformer.dart';
import 'download_adapter.dart';

DownloadAdapter createAdapter() => BrowserDownloadAdapter();

class BrowserDownloadAdapter implements DownloadAdapter {
@override
Future<Response> download(
String urlPath,
dynamic savePath, {
required RequestCallback request,
required Transformer transformer,
ProgressCallback? onReceiveProgress,
Map<String, dynamic>? queryParameters,
CancelToken? cancelToken,
bool deleteOnError = true,
String lengthHeader = Headers.contentLengthHeader,
Object? data,
Options? options,
}) {
throw UnsupportedError(
'The download method is not available in the Web environment.',
);
}
}
36 changes: 36 additions & 0 deletions dio/lib/src/adapters/download/download_adapter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import '../../cancel_token.dart';
import '../../headers.dart';
import '../../options.dart';
import '../../response.dart';
import '../../transformer.dart';

import 'io_download_adapter.dart'
if (dart.library.html) 'browser_download_adapter.dart' as adapter;

typedef RequestCallback = Future<Response<T>> Function<T>(
String url, {
Object? data,
Map<String, dynamic>? queryParameters,
CancelToken? cancelToken,
Options? options,
ProgressCallback? onSendProgress,
ProgressCallback? onReceiveProgress,
});

abstract class DownloadAdapter {
factory DownloadAdapter() => adapter.createAdapter();

Future<Response> download(
String urlPath,
dynamic savePath, {
required RequestCallback request,
required Transformer transformer,
ProgressCallback? onReceiveProgress,
Map<String, dynamic>? queryParameters,
CancelToken? cancelToken,
bool deleteOnError = true,
String lengthHeader = Headers.contentLengthHeader,
Object? data,
Options? options,
});
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
import 'dart:async';
import 'dart:io';

import '../../dio.dart';
import '../adapter.dart';
import '../cancel_token.dart';
import '../dio_exception.dart';
import '../dio_mixin.dart';
import '../response.dart';
import '../dio.dart';
import '../headers.dart';
import '../options.dart';
import '../adapters/io_adapter.dart';
import '../../cancel_token.dart';
import '../../dio_exception.dart';
import '../../headers.dart';
import '../../options.dart';
import '../../response.dart';
import '../../transformer.dart';
import 'download_adapter.dart';

/// Create the [Dio] instance for native platforms.
Dio createDio([BaseOptions? baseOptions]) => DioForNative(baseOptions);
DownloadAdapter createAdapter() => IODownloadAdapter();

/// Implements features for [Dio] on native platforms.
class DioForNative extends DioMixin implements Dio {
/// Create Dio instance with default [BaseOptions].
/// It is recommended that an application use only the same DIO singleton.
DioForNative([BaseOptions? baseOptions]) {
options = baseOptions ?? BaseOptions();
httpClientAdapter = IOHttpClientAdapter();
}

/// {@macro dio.Dio.download}
class IODownloadAdapter implements DownloadAdapter {
@override
Future<Response> download(
String urlPath,
dynamic savePath, {
required RequestCallback request,
required Transformer transformer,
ProgressCallback? onReceiveProgress,
Map<String, dynamic>? queryParameters,
CancelToken? cancelToken,
Expand All @@ -36,14 +28,14 @@ class DioForNative extends DioMixin implements Dio {
Object? data,
Options? options,
}) async {
options ??= DioMixin.checkOptions('GET', options);
options ??= Dio.checkOptions('GET', options);
// Manually set the `responseType` to [ResponseType.stream]
// to retrieve the response stream.
// Do not modify previous options.
options = options.copyWith(responseType: ResponseType.stream);
final Response<ResponseBody> response;
try {
response = await request<ResponseBody>(
response = await request(
urlPath,
data: data,
options: options,
Expand Down Expand Up @@ -153,7 +145,7 @@ class DioForNative extends DioMixin implements Dio {
}
} finally {
completer.completeError(
DioMixin.assureDioException(e, response.requestOptions),
Dio.assureDioException(e, response.requestOptions),
);
}
});
Expand All @@ -166,7 +158,7 @@ class DioForNative extends DioMixin implements Dio {
completer.complete(response);
} catch (e) {
completer.completeError(
DioMixin.assureDioException(e, response.requestOptions),
Dio.assureDioException(e, response.requestOptions),
);
}
},
Expand All @@ -175,7 +167,7 @@ class DioForNative extends DioMixin implements Dio {
await closeAndDelete();
} finally {
completer.completeError(
DioMixin.assureDioException(e, response.requestOptions),
Dio.assureDioException(e, response.requestOptions),
);
}
},
Expand All @@ -185,6 +177,6 @@ class DioForNative extends DioMixin implements Dio {
await subscription.cancel();
await closeAndDelete();
});
return DioMixin.listenCancelForAsyncTask(cancelToken, completer.future);
return Dio.listenCancelForAsyncTask(cancelToken, completer.future);
}
}
2 changes: 1 addition & 1 deletion dio/lib/src/adapters/io_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'dart:typed_data';

import 'package:async/async.dart';

import '../adapter.dart';
import 'adapter.dart';
import '../dio_exception.dart';
import '../options.dart';
import '../redirect_record.dart';
Expand Down
Loading