-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hello Conversion Layer Adapter (#1976)
Signed-off-by: Alex Li <[email protected]> Co-authored-by: Alex Li <[email protected]>
- Loading branch information
Showing
18 changed files
with
359 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# https://dart.dev/guides/libraries/private-files | ||
# Created by `dart pub` | ||
.dart_tool/ | ||
|
||
# Avoid committing pubspec.lock for library packages; see | ||
# https://dart.dev/guides/libraries/private-files#pubspeclock. | ||
pubspec.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# CHANGELOG | ||
|
||
## Unreleased | ||
|
||
*None.* | ||
|
||
## 0.1.0 | ||
|
||
- Initial version. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 The CFUG Team | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# dio_compatibility_layer | ||
|
||
[![pub package](https://img.shields.io/pub/v/dio_compatibility_layer.svg)](https://pub.dev/packages/dio_compatibility_layer) | ||
[![likes](https://img.shields.io/pub/likes/dio_compatibility_layer)](https://pub.dev/packages/dio_compatibility_layer/score) | ||
[![popularity](https://img.shields.io/pub/popularity/dio_compatibility_layer)](https://pub.dev/packages/dio_compatibility_layer/score) | ||
[![pub points](https://img.shields.io/pub/points/dio_compatibility_layer)](https://pub.dev/packages/dio_compatibility_layer/score) | ||
|
||
If you encounter bugs, consider fixing it by opening a PR or at least contribute a failing test case. | ||
|
||
This package contains adapters for [Dio](https://pub.dev/packages/dio) | ||
which enables you to make use of other HTTP clients as the underlying implementation. | ||
|
||
Currently, it supports compatibility with | ||
- [`http`](https://pub.dev/packages/http) | ||
|
||
## Get started | ||
|
||
### Install | ||
|
||
Add the `dio_compatibility_layer` package to your | ||
[pubspec dependencies](https://pub.dev/packages/dio_compatibility_layer/install). | ||
|
||
### Example | ||
|
||
To use the `http` compatibility: | ||
|
||
```dart | ||
import 'package:dio/dio.dart'; | ||
import 'package:dio_compatibility_layer/dio_compatibility_layer.dart'; | ||
import 'package:http/http.dart'; | ||
void main() async { | ||
// Start in the `http` world. You can use `http`, `cronet_http`, | ||
// `cupertino_http` and other `http` compatible packages. | ||
final httpClient = Client(); | ||
// Make the `httpClient` compatible via the `ConversionLayerAdapter` class. | ||
final dioAdapter = ConversionLayerAdapter(httpClient); | ||
// Make dio use the `httpClient` via the conversion layer. | ||
final dio = Dio()..httpClientAdapter = dioAdapter; | ||
// Make a request | ||
final response = await dio.get('https://dart.dev'); | ||
print(response); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
include: ../../analysis_options.yaml | ||
|
||
analyzer: | ||
language: | ||
strict-raw-types: true | ||
strict-casts: true | ||
strict-inference: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
presets: | ||
# empty placeholder required in CI scripts | ||
all: | ||
|
||
override_platforms: | ||
chrome: | ||
settings: | ||
headless: true | ||
firefox: | ||
settings: | ||
# headless argument has to be set explicitly for non-chrome browsers | ||
arguments: --headless |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="WEB_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" /> | ||
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" /> | ||
<excludeFolder url="file://$MODULE_DIR$/.dart_tool" /> | ||
<excludeFolder url="file://$MODULE_DIR$/.pub" /> | ||
<excludeFolder url="file://$MODULE_DIR$/build" /> | ||
</content> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
<orderEntry type="library" name="Dart SDK" level="project" /> | ||
<orderEntry type="library" name="Dart Packages" level="project" /> | ||
</component> | ||
</module> |
19 changes: 19 additions & 0 deletions
19
plugins/compatibility_layer/example/conversion_layer_example.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import 'package:dio/dio.dart'; | ||
import 'package:dio_compatibility_layer/dio_compatibility_layer.dart'; | ||
import 'package:http/http.dart'; | ||
|
||
void main() async { | ||
// Start in the `http` world. You can use `http`, `cronet_http`, | ||
// `cupertino_http` and other `http` compatible packages. | ||
final httpClient = Client(); | ||
|
||
// Make the `httpClient` compatible via the `ConversionLayerAdapter` class. | ||
final dioAdapter = ConversionLayerAdapter(httpClient); | ||
|
||
// Make dio use the `httpClient` via the conversion layer. | ||
final dio = Dio()..httpClientAdapter = dioAdapter; | ||
|
||
// Make a request. | ||
final response = await dio.get<dynamic>('https://dart.dev'); | ||
print(response); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
library dio_compatibility_layer; | ||
|
||
export 'src/conversion_layer_adapter.dart'; |
97 changes: 97 additions & 0 deletions
97
plugins/compatibility_layer/lib/src/conversion_layer_adapter.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import 'dart:async'; | ||
import 'dart:convert'; | ||
import 'dart:typed_data'; | ||
|
||
import 'package:dio/dio.dart'; | ||
import 'package:http/http.dart' as http; | ||
|
||
const _kIsWeb = bool.hasEnvironment('dart.library.js_util') | ||
? bool.fromEnvironment('dart.library.js_util') | ||
: identical(0, 0.0); | ||
|
||
/// A conversion layer which translates [Dio] requests to | ||
/// [`http`](https://pub.dev/packages/http) compatible requests. | ||
/// This enables you to use | ||
/// [`cronet_http`](https://pub.dev/packages/cronet_http), | ||
/// [`cupertino_http`](https://pub.dev/packages/cupertino_http), | ||
/// and other `http` compatible packages with [Dio]. | ||
class ConversionLayerAdapter implements HttpClientAdapter { | ||
ConversionLayerAdapter(this.client); | ||
|
||
/// The client instance from the `http` package. | ||
final http.Client client; | ||
|
||
@override | ||
Future<ResponseBody> fetch( | ||
RequestOptions options, | ||
Stream<Uint8List>? requestStream, | ||
Future<dynamic>? cancelFuture, | ||
) async { | ||
final request = await _fromOptionsAndStream(options, requestStream); | ||
final response = await client.send(request); | ||
return ResponseBody( | ||
response.stream.cast<Uint8List>(), | ||
response.statusCode, | ||
statusMessage: response.reasonPhrase, | ||
isRedirect: response.isRedirect, | ||
headers: Map.fromEntries( | ||
response.headers.entries.map((e) => MapEntry(e.key, [e.value])), | ||
), | ||
); | ||
} | ||
|
||
@override | ||
void close({bool force = false}) => client.close(); | ||
|
||
Future<http.BaseRequest> _fromOptionsAndStream( | ||
RequestOptions options, | ||
Stream<Uint8List>? requestStream, | ||
) async { | ||
final http.BaseRequest request; | ||
if (_kIsWeb && requestStream != null) { | ||
final normalRequest = request = http.Request( | ||
options.method, | ||
options.uri, | ||
); | ||
final completer = Completer<Uint8List>(); | ||
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; | ||
normalRequest.bodyBytes = bytes; | ||
} else if (requestStream != null) { | ||
final streamedRequest = request = http.StreamedRequest( | ||
options.method, | ||
options.uri, | ||
); | ||
requestStream.listen( | ||
streamedRequest.sink.add, | ||
onError: streamedRequest.sink.addError, | ||
onDone: streamedRequest.sink.close, | ||
cancelOnError: true, | ||
); | ||
} else { | ||
request = http.Request(options.method, options.uri); | ||
} | ||
request.headers.addAll( | ||
Map.fromEntries( | ||
options.headers.entries.map( | ||
(e) => MapEntry(e.key, e.value.toString().trim()), | ||
), | ||
), | ||
); | ||
request | ||
..followRedirects = options.followRedirects | ||
..maxRedirects = options.maxRedirects | ||
..persistentConnection = options.persistentConnection; | ||
return request; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
name: dio_compatibility_layer | ||
version: 0.1.0 | ||
|
||
description: Enables dio to make use of http packages. | ||
topics: | ||
- dio | ||
- http | ||
- network | ||
- native | ||
- cronet | ||
homepage: https://github.com/cfug/dio | ||
repository: https://github.com/cfug/dio/blob/main/plugins/compatibility_layer | ||
issue_tracker: https://github.com/cfug/dio/issues | ||
|
||
environment: | ||
sdk: ^3.0.0 | ||
|
||
dependencies: | ||
dio: ^5.2.0 | ||
http: ^1.0.0 | ||
|
||
dev_dependencies: | ||
lints: any | ||
test: any |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import 'package:http/http.dart'; | ||
|
||
class CloseClientMock implements Client { | ||
bool closeWasCalled = false; | ||
|
||
@override | ||
void close() { | ||
closeWasCalled = true; | ||
} | ||
|
||
@override | ||
dynamic noSuchMethod(Invocation i) => super.noSuchMethod(i); | ||
} | ||
|
||
class ClientMock implements Client { | ||
StreamedResponse? response; | ||
BaseRequest? request; | ||
|
||
@override | ||
Future<StreamedResponse> send(BaseRequest request) async { | ||
this.request = request; | ||
return response!; | ||
} | ||
|
||
@override | ||
dynamic noSuchMethod(Invocation i) => super.noSuchMethod(i); | ||
} |
Oops, something went wrong.