From 2feef4ea8df0794ca91941fc41e3cad5b8188efd Mon Sep 17 00:00:00 2001 From: Alex Li Date: Sat, 2 Dec 2023 15:57:04 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8F=97=EF=B8=8F=20Make=20`native=5Fdio=5F?= =?UTF-8?q?adapter`=20depends=20on=20`http=5Fcompatibility=5Flayer`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lib/src/conversion_layer_adapter.dart | 92 ------------------- .../lib/src/cronet_adapter.dart | 2 +- .../lib/src/cupertino_adapter.dart | 2 +- plugins/native_dio_adapter/pubspec.yaml | 3 +- .../native_dio_adapter/test/client_mock.dart | 32 ------- .../test/conversion_layer_adapter_test.dart | 63 ------------- 6 files changed, 3 insertions(+), 191 deletions(-) delete mode 100644 plugins/native_dio_adapter/lib/src/conversion_layer_adapter.dart delete mode 100644 plugins/native_dio_adapter/test/client_mock.dart delete mode 100644 plugins/native_dio_adapter/test/conversion_layer_adapter_test.dart diff --git a/plugins/native_dio_adapter/lib/src/conversion_layer_adapter.dart b/plugins/native_dio_adapter/lib/src/conversion_layer_adapter.dart deleted file mode 100644 index c153ae8fd..000000000 --- a/plugins/native_dio_adapter/lib/src/conversion_layer_adapter.dart +++ /dev/null @@ -1,92 +0,0 @@ -import 'dart:async'; -import 'dart:convert'; -import 'dart:typed_data'; - -import 'package:dio/dio.dart'; -import 'package:http/http.dart'; - -/// A conversion layer which translates Dio HTTP requests to -/// [http](https://pub.dev/packages/http) compatible requests. -/// This way there's no need to implement custom [HttpClientAdapter] -/// for each platform. Therefore, the required effort to add tests is kept -/// to a minimum. Since `CupertinoClient` and `CronetClient` depend anyway on -/// `http` this also doesn't add any additional dependency. -class ConversionLayerAdapter implements HttpClientAdapter { - final Client client; - - ConversionLayerAdapter(this.client); - - @override - Future fetch( - RequestOptions options, - Stream? requestStream, - Future? cancelFuture, - ) async { - final request = await _fromOptionsAndStream(options, requestStream); - final response = await client.send(request); - return response.toDioResponseBody(options); - } - - @override - void close({bool force = false}) => client.close(); - - Future _fromOptionsAndStream( - RequestOptions options, - Stream? requestStream, - ) async { - final request = Request( - options.method, - options.uri, - ); - - request.headers.addAll( - Map.fromEntries( - options.headers.entries.map( - (e) => MapEntry( - options.preserveHeaderCase ? e.key : e.key.toLowerCase(), - e.value.toString(), - ), - ), - ), - ); - - request.followRedirects = options.followRedirects; - request.maxRedirects = options.maxRedirects; - - if (requestStream != null) { - final completer = Completer(); - final sink = ByteConversionSink.withCallback( - (bytes) => completer.complete( - bytes is Uint8List ? bytes : Uint8List.fromList(bytes), - ), - ); - requestStream.listen( - sink.add, - onError: completer.completeError, - onDone: sink.close, - cancelOnError: true, - ); - final bytes = await completer.future; - request.bodyBytes = bytes; - } - return request; - } -} - -extension on StreamedResponse { - ResponseBody toDioResponseBody(RequestOptions options) { - final dioHeaders = headers.entries.map( - (e) => MapEntry( - options.preserveHeaderCase ? e.key : e.key.toLowerCase(), - [e.value], - ), - ); - return ResponseBody( - stream.cast(), - statusCode, - headers: Map.fromEntries(dioHeaders), - isRedirect: isRedirect, - statusMessage: reasonPhrase, - ); - } -} diff --git a/plugins/native_dio_adapter/lib/src/cronet_adapter.dart b/plugins/native_dio_adapter/lib/src/cronet_adapter.dart index 1b50bf075..500a9d126 100644 --- a/plugins/native_dio_adapter/lib/src/cronet_adapter.dart +++ b/plugins/native_dio_adapter/lib/src/cronet_adapter.dart @@ -2,7 +2,7 @@ import 'dart:typed_data'; import 'package:cronet_http/cronet_http.dart'; import 'package:dio/dio.dart'; -import 'conversion_layer_adapter.dart'; +import 'package:dio_http_compatibility_layer/dio_http_compatibility_layer.dart'; /// A [HttpClientAdapter] for Dio which delegates HTTP requests /// to the native platform by making use of diff --git a/plugins/native_dio_adapter/lib/src/cupertino_adapter.dart b/plugins/native_dio_adapter/lib/src/cupertino_adapter.dart index 800f2493e..dc6b68e42 100644 --- a/plugins/native_dio_adapter/lib/src/cupertino_adapter.dart +++ b/plugins/native_dio_adapter/lib/src/cupertino_adapter.dart @@ -2,7 +2,7 @@ import 'dart:typed_data'; import 'package:cupertino_http/cupertino_http.dart'; import 'package:dio/dio.dart'; -import 'conversion_layer_adapter.dart'; +import 'package:dio_http_compatibility_layer/dio_http_compatibility_layer.dart'; /// A [HttpClientAdapter] for Dio which delegates HTTP requests /// to the native platform by making use of diff --git a/plugins/native_dio_adapter/pubspec.yaml b/plugins/native_dio_adapter/pubspec.yaml index ebff7cd17..ac4040c8f 100644 --- a/plugins/native_dio_adapter/pubspec.yaml +++ b/plugins/native_dio_adapter/pubspec.yaml @@ -24,11 +24,10 @@ dependencies: cupertino_http: ^1.0.0 cronet_http: ^0.4.0 http: ^1.0.0 + dio_http_compatibility_layer: ^0.0.1 dev_dependencies: lints: ^2.0.0 - flutter_test: - sdk: flutter platforms: android: diff --git a/plugins/native_dio_adapter/test/client_mock.dart b/plugins/native_dio_adapter/test/client_mock.dart deleted file mode 100644 index e06b54563..000000000 --- a/plugins/native_dio_adapter/test/client_mock.dart +++ /dev/null @@ -1,32 +0,0 @@ -import 'package:http/http.dart'; - -class CloseClientMock implements Client { - bool closeWasCalled = false; - - @override - void close() { - closeWasCalled = true; - } - - @override - void noSuchMethod(Invocation invocation) { - throw UnimplementedError(); - } -} - -class ClientMock implements Client { - StreamedResponse? response; - - BaseRequest? request; - - @override - Future send(BaseRequest request) async { - this.request = request; - return response!; - } - - @override - void noSuchMethod(Invocation invocation) { - throw UnimplementedError(); - } -} diff --git a/plugins/native_dio_adapter/test/conversion_layer_adapter_test.dart b/plugins/native_dio_adapter/test/conversion_layer_adapter_test.dart deleted file mode 100644 index 5de82452f..000000000 --- a/plugins/native_dio_adapter/test/conversion_layer_adapter_test.dart +++ /dev/null @@ -1,63 +0,0 @@ -import 'dart:typed_data'; - -import 'package:dio/dio.dart'; -import 'package:http/http.dart'; -import 'package:native_dio_adapter/src/conversion_layer_adapter.dart'; -import 'package:flutter_test/flutter_test.dart'; - -import 'client_mock.dart'; - -void main() { - test('close', () { - final mock = CloseClientMock(); - final cla = ConversionLayerAdapter(mock); - - cla.close(); - - expect(mock.closeWasCalled, true); - }); - - test('close with force', () { - final mock = CloseClientMock(); - final cla = ConversionLayerAdapter(mock); - - cla.close(force: true); - - expect(mock.closeWasCalled, true); - }); - - test('headers', () async { - final mock = ClientMock()..response = StreamedResponse(Stream.empty(), 200); - final cla = ConversionLayerAdapter(mock); - - await cla.fetch( - RequestOptions(path: '', headers: {'foo': 'bar'}), - Stream.empty(), - null, - ); - - expect(mock.request?.headers, {'foo': 'bar'}); - }); - - test('download stream', () async { - final mock = ClientMock() - ..response = StreamedResponse( - Stream.fromIterable([ - Uint8List.fromList([10, 1]), - Uint8List.fromList([1, 4]), - Uint8List.fromList([5, 1]), - Uint8List.fromList([1, 1]), - Uint8List.fromList([2, 4]), - ]), - 200); - final cla = ConversionLayerAdapter(mock); - - final resp = await cla.fetch( - RequestOptions(path: ''), - null, - null, - ); - - expect(await resp.stream.length, 5); - }); -}