Skip to content

Commit

Permalink
Merge pull request #45 from koji-1009/feat/http_client
Browse files Browse the repository at this point in the history
feat: Remove http package
  • Loading branch information
koji-1009 authored Jul 19, 2024
2 parents 9080440 + 34dd3f1 commit b3bf317
Show file tree
Hide file tree
Showing 16 changed files with 281 additions and 68 deletions.
55 changes: 49 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,56 @@ class HomePage extends StatelessWidget {

### Use another http client

If you want to use another http client, like [dio](https://pub.dev/packages/dio), you can create a custom `TaroHttpClient`.
If you want to use another http client, like [http](https://pub.dev/packages/http) or [dio](https://pub.dev/packages/dio), you can create a custom `TaroHttpClient`.

#### http

```dart
class HttpHttp implements TaroHttpClient {
const HttpHttp({
this.timeout = const Duration(
seconds: 180,
),
});
final Duration timeout;
@override
Future<TaroHttpResponse> get({
required Uri uri,
required Map<String, String> headers,
StreamController<ImageChunkEvent>? chunkEvents,
}) async {
final response = await http
.get(
uri,
headers: headers,
)
.timeout(timeout);
return (
statusCode: response.statusCode,
bodyBytes: response.bodyBytes,
reasonPhrase: response.reasonPhrase,
contentLength: response.contentLength,
headers: response.headers,
isRedirect: response.isRedirect,
);
}
}
```

Then, create a `Taro` instance with the custom http client.

```dart
Taro.instance.networkLoader = TaroLoaderNetwork(
client: const HttpHttp(),
);
```

#### dio

```dart
class DioHttp implements TaroHttpClient {
/// Creates a [DioHttp].
const DioHttp({
required this.dio,
});
Expand Down Expand Up @@ -143,11 +188,9 @@ If a native cache directory exists, such as Android or iOS, use [path_provider](

- [clock](https://pub.dev/packages/clock)
- Get current time and mock time
- [crypto](https://pub.dev/packages/crypto)
- Get persistent file name from URL and options
- [image](https://pub.dev/packages/image)
- Resize image
- [http](https://pub.dev/packages/http)
- Fetch data from network
- [path_provider](https://pub.dev/packages/path_provider)
- Get application cache directory
- [crypto](https://pub.dev/packages/crypto)
- Get persistent file name from URL and options
1 change: 1 addition & 0 deletions example/lib/dio_http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:typed_data';
import 'package:dio/dio.dart';
import 'package:taro/taro.dart';

/// [DioHttp] is a class that performs GET requests using the dio package
class DioHttp implements TaroHttpClient {
/// Creates a [DioHttp].
const DioHttp({
Expand Down
35 changes: 35 additions & 0 deletions example/lib/http_http.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'package:http/http.dart' as http;
import 'package:taro/taro.dart';

/// [HttpHttp] is a class that performs GET requests using the http package
class HttpHttp implements TaroHttpClient {
/// Creates a [HttpClient].
const HttpHttp({
this.timeout = const Duration(
seconds: 180,
),
});

final Duration timeout;

@override
Future<TaroHttpResponse> get({
required Uri uri,
required Map<String, String> headers,
}) async {
final response = await http
.get(
uri,
headers: headers,
)
.timeout(timeout);
return (
statusCode: response.statusCode,
bodyBytes: response.bodyBytes,
reasonPhrase: response.reasonPhrase,
contentLength: response.contentLength,
headers: response.headers,
isRedirect: response.isRedirect,
);
}
}
32 changes: 23 additions & 9 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
import 'package:dio/dio.dart';
import 'package:example/app.dart';
import 'package:example/dio_http.dart';
import 'package:example/http_http.dart';
import 'package:flutter/material.dart';
import 'package:taro/taro.dart';

enum HttpMode {
http,
dio,
none,
}

void main() {
const useDio = true;
if (useDio) {
Taro.instance.networkLoader = TaroLoaderNetwork(
client: DioHttp(
dio: Dio()
..options.connectTimeout = const Duration(seconds: 10)
..options.receiveTimeout = const Duration(seconds: 10),
),
);
const mode = HttpMode.none;
switch (mode) {
case HttpMode.http:
Taro.instance.networkLoader = const TaroLoaderNetwork(
client: HttpHttp(),
);
case HttpMode.dio:
Taro.instance.networkLoader = TaroLoaderNetwork(
client: DioHttp(
dio: Dio()
..options.connectTimeout = const Duration(seconds: 10)
..options.receiveTimeout = const Duration(seconds: 10),
),
);
case HttpMode.none:
break;
}

runApp(const App());
Expand Down
42 changes: 21 additions & 21 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,18 @@ packages:
dependency: "direct main"
description:
name: dio
sha256: "11e40df547d418cc0c4900a9318b26304e665da6fa4755399a9ff9efd09034b5"
sha256: e17f6b3097b8c51b72c74c9f071a605c47bcc8893839bd66732457a5ebe73714
url: "https://pub.dev"
source: hosted
version: "5.4.3+1"
version: "5.5.0+1"
dio_web_adapter:
dependency: transitive
description:
name: dio_web_adapter
sha256: "36c5b2d79eb17cdae41e974b7a8284fec631651d2a6f39a8a2ff22327e90aeac"
url: "https://pub.dev"
source: hosted
version: "1.0.1"
fake_async:
dependency: transitive
description:
Expand Down Expand Up @@ -108,13 +116,13 @@ packages:
source: sdk
version: "0.0.0"
http:
dependency: transitive
dependency: "direct main"
description:
name: http
sha256: "761a297c042deedc1ffbb156d6e2af13886bb305c2a343a4d972504cd67dd938"
sha256: b9c29a161230ee03d3ccf545097fccd9b87a5264228c5d348202e0f0c28f9010
url: "https://pub.dev"
source: hosted
version: "1.2.1"
version: "1.2.2"
http_parser:
dependency: transitive
description:
Expand Down Expand Up @@ -207,10 +215,10 @@ packages:
dependency: transitive
description:
name: path_provider_android
sha256: "9c96da072b421e98183f9ea7464898428e764bc0ce5567f27ec8693442e72514"
sha256: "30c5aa827a6ae95ce2853cdc5fe3971daaac00f6f081c419c013f7f57bff2f5e"
url: "https://pub.dev"
source: hosted
version: "2.2.5"
version: "2.2.7"
path_provider_foundation:
dependency: transitive
description:
Expand Down Expand Up @@ -239,10 +247,10 @@ packages:
dependency: transitive
description:
name: path_provider_windows
sha256: "8bc9f22eee8690981c22aa7fc602f5c85b497a6fb2ceb35ee5a5e5ed85ad8170"
sha256: bd6f00dbd873bfb70d0761682da2b3a2c2fccc2b9e84c495821639601d81afe7
url: "https://pub.dev"
source: hosted
version: "2.2.1"
version: "2.3.0"
petitparser:
dependency: transitive
description:
Expand All @@ -255,10 +263,10 @@ packages:
dependency: transitive
description:
name: platform
sha256: "12220bb4b65720483f8fa9450b4332347737cf8213dd2840d8b2c823e47243ec"
sha256: "9b71283fc13df574056616011fb138fd3b793ea47cc509c189a6c3fa5f8a1a65"
url: "https://pub.dev"
source: hosted
version: "3.1.4"
version: "3.1.5"
plugin_platform_interface:
dependency: transitive
description:
Expand Down Expand Up @@ -355,18 +363,10 @@ packages:
dependency: transitive
description:
name: web
sha256: "97da13628db363c635202ad97068d47c5b8aa555808e7a9411963c533b449b27"
url: "https://pub.dev"
source: hosted
version: "0.5.1"
win32:
dependency: transitive
description:
name: win32
sha256: a79dbe579cb51ecd6d30b17e0cae4e0ea15e2c0e66f69ad4198f22a6789e94f4
sha256: d43c1d6b787bf0afad444700ae7f4db8827f701bc61c255ac8d328c6f4d52062
url: "https://pub.dev"
source: hosted
version: "5.5.1"
version: "1.0.0"
xdg_directories:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies:
sdk: flutter

cupertino_icons: ^1.0.0
http: ^1.0.0
dio: ^5.0.0

taro:
Expand Down
22 changes: 12 additions & 10 deletions lib/src/network/http_client.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import 'package:http/http.dart' as http;
import 'dart:async';

import 'package:taro/src/taro_loader_network.dart';

import 'shared.dart' as client;

/// [HttpClient] is a class that manages the sending of GET requests to the provided URL.
class HttpClient implements TaroHttpClient {
/// Creates a [HttpClient].
Expand All @@ -17,14 +20,13 @@ class HttpClient implements TaroHttpClient {
required Uri uri,
required Map<String, String> headers,
}) async {
final response = await http.get(uri, headers: headers).timeout(timeout);
return (
statusCode: response.statusCode,
bodyBytes: response.bodyBytes,
reasonPhrase: response.reasonPhrase,
contentLength: response.contentLength,
headers: response.headers,
isRedirect: response.isRedirect,
);
final response = await client
.get(
uri: uri,
headers: headers,
)
.timeout(timeout);

return response;
}
}
34 changes: 34 additions & 0 deletions lib/src/network/native.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'dart:async';
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:taro/src/taro_loader_network.dart';

final _httpClient = HttpClient()..autoUncompress = false;

/// Load image data as [TaroHttpResponse] from the network.
Future<TaroHttpResponse> get({
required Uri uri,
required Map<String, String> headers,
}) async {
final request = await _httpClient.getUrl(uri);
for (final entry in headers.entries) {
request.headers.add(entry.key, entry.value);
}
final response = await request.close();

final responseBytes = await consolidateHttpClientResponseBytes(response);
final responseHeaders = <String, String>{};
response.headers.forEach((key, values) {
responseHeaders[key] = values.join(',');
});

return (
statusCode: response.statusCode,
bodyBytes: responseBytes,
reasonPhrase: response.reasonPhrase,
contentLength: response.contentLength,
headers: responseHeaders,
isRedirect: response.isRedirect,
);
}
3 changes: 3 additions & 0 deletions lib/src/network/shared.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export 'unsupported.dart'
if (dart.library.ffi) 'native.dart'
if (dart.library.html) 'web.dart';
10 changes: 10 additions & 0 deletions lib/src/network/unsupported.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'dart:async';

import 'package:taro/src/taro_loader_network.dart';

/// Load image data as [TaroHttpResponse] from the network.
Future<TaroHttpResponse> get({
required Uri uri,
required Map<String, String> headers,
}) async =>
throw UnimplementedError();
36 changes: 36 additions & 0 deletions lib/src/network/web.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'dart:async';
import 'dart:js_interop';

import 'package:taro/src/network/web_fetch.dart';
import 'package:taro/src/taro_loader_network.dart';

/// Load image data as [TaroHttpResponse] from the network.
Future<TaroHttpResponse> get({
required Uri uri,
required Map<String, String> headers,
}) async {
final requestHeaders = Headers();
for (final entry in headers.entries) {
requestHeaders.append(entry.key, entry.value);
}
final response = await fetch(
uri.toString(),
requestHeaders,
).toDart;

final responseBuffer = await response.arrayBuffer().toDart;
final responseBytes = responseBuffer.toDart.asUint8List();
final responseHeaders = <String, String>{};
response.headers.forEach((String value, String key, JSObject object) {
responseHeaders[key] = value;
}.toJS);

return (
statusCode: response.status,
bodyBytes: responseBytes,
reasonPhrase: response.statusText,
contentLength: responseBytes.length,
headers: responseHeaders,
isRedirect: response.redirected,
);
}
Loading

0 comments on commit b3bf317

Please sign in to comment.