Skip to content

Commit 092bb2d

Browse files
authored
Create a package:cronet_http/cronet_http.dart import (#859)
1 parent a62f5b3 commit 092bb2d

File tree

7 files changed

+68
-5
lines changed

7 files changed

+68
-5
lines changed

pkgs/cronet_http/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.2.0
2+
3+
* Restructure `package:cronet_http` to offer a
4+
`package:cronet_http/cronet_http.dart` import.
5+
16
## 0.1.2
27

38
* Fix a NPE that occurs when an error occurs before a response is received.

pkgs/cronet_http/example/integration_test/client_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:cronet_http/cronet_client.dart';
5+
import 'package:cronet_http/cronet_http.dart';
66
import 'package:http/http.dart';
77
import 'package:http_client_conformance_tests/http_client_conformance_tests.dart';
88
import 'package:integration_test/integration_test.dart';

pkgs/cronet_http/example/integration_test/cronet_configuration_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
import 'dart:io';
88

9-
import 'package:cronet_http/cronet_client.dart';
9+
import 'package:cronet_http/cronet_http.dart';
1010
import 'package:integration_test/integration_test.dart';
1111
import 'package:test/test.dart';
1212

pkgs/cronet_http/example/lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import 'dart:convert';
66
import 'dart:io';
77

88
import 'package:cached_network_image/cached_network_image.dart';
9-
import 'package:cronet_http/cronet_client.dart';
9+
import 'package:cronet_http/cronet_http.dart';
1010
import 'package:flutter/material.dart';
1111
import 'package:http/http.dart';
1212

pkgs/cronet_http/lib/cronet_http.dart

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// An Android Flutter plugin that provides access to the
6+
/// [Cronet](https://developer.android.com/guide/topics/connectivity/cronet/reference/org/chromium/net/package-summary)
7+
/// HTTP client.
8+
///
9+
/// ```
10+
/// import 'package:cronet_http/cronet_http.dart';
11+
///
12+
/// void main() async {
13+
/// var client = CronetClient();
14+
/// final response = await client.get(
15+
/// Uri.https('www.googleapis.com', '/books/v1/volumes', {'q': '{http}'}));
16+
/// if (response.statusCode != 200) {
17+
/// throw HttpException('bad response: ${response.statusCode}');
18+
/// }
19+
///
20+
/// final decodedResponse =
21+
/// jsonDecode(utf8.decode(response.bodyBytes)) as Map;
22+
///
23+
/// final itemCount = decodedResponse['totalItems'];
24+
/// print('Number of books about http: $itemCount.');
25+
/// for (var i = 0; i < min(itemCount, 10); ++i) {
26+
/// print(decodedResponse['items'][i]['volumeInfo']['title']);
27+
/// }
28+
/// }
29+
/// ```
30+
///
31+
/// [CronetClient] is an implementation of the `package:http` [Client],
32+
/// which means that it can easily used conditionally based on the current
33+
/// platform.
34+
///
35+
/// ```
36+
/// void main() {
37+
/// var clientFactory = Client.new; // Constructs the default client.
38+
/// if (Platform.isAndroid) {
39+
/// Future<CronetEngine>? engine;
40+
/// clientFactory = () {
41+
/// engine ??= CronetEngine.build(
42+
/// cacheMode: CacheMode.memory, userAgent: 'MyAgent');
43+
/// return CronetClient.fromCronetEngineFuture(engine!);
44+
/// };
45+
/// }
46+
/// runWithClient(() => runApp(const MyFlutterApp()), clientFactory);
47+
/// }
48+
/// ```
49+
///
50+
/// After the above setup, calling [Client] methods or any of the
51+
/// `package:http` convenient functions (e.g. [get]) will result in
52+
/// [CronetClient] being used on Android.
53+
54+
import 'package:http/http.dart';
55+
56+
import 'src/cronet_client.dart';
57+
58+
export 'src/cronet_client.dart';

pkgs/cronet_http/lib/cronet_client.dart renamed to pkgs/cronet_http/lib/src/cronet_client.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import 'dart:async';
1717
import 'package:flutter/services.dart';
1818
import 'package:http/http.dart';
1919

20-
import 'src/messages.dart' as messages;
20+
import 'messages.dart' as messages;
2121

2222
late final _api = messages.HttpApi();
2323

pkgs/cronet_http/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: cronet_http
22
description: >
33
An Android Flutter plugin that provides access to the Cronet HTTP client.
4-
version: 0.1.2
4+
version: 0.2.0
55
repository: https://github.com/dart-lang/http/tree/master/pkgs/cronet_http
66

77
environment:

0 commit comments

Comments
 (0)