Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
brianquinlan committed Dec 21, 2023
1 parent e6ba2f1 commit 5afe399
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 44 deletions.
57 changes: 16 additions & 41 deletions pkgs/cupertino_http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,49 +24,25 @@ This approach allows the same HTTP code to be used on all platforms, while
still allowing platform-specific setup.

```dart
late Client client;
if (Platform.isIOS) {
final config = URLSessionConfiguration.ephemeralSessionConfiguration()
..allowsCellularAccess = false
..allowsConstrainedNetworkAccess = false
..allowsExpensiveNetworkAccess = false;
client = CupertinoClient.fromSessionConfiguration(config);
} else {
client = IOClient(); // Uses an HTTP client based on dart:io
}
final response = await client.get(Uri.https(
'www.googleapis.com',
'/books/v1/volumes',
{'q': 'HTTP', 'maxResults': '40', 'printType': 'books'}));
```

[package:http runWithClient][] can be used to configure the
[package:http Client][] for the entire application.

```dart
void main() {
late Client client;
if (Platform.isIOS) {
client = CupertinoClient.defaultSessionConfiguration();
import 'package:cupertino_http/cupertino_http.dart';
import 'package:http/http.dart';
import 'package:http/io_client.dart';
void main() async {
late Client httpClient;
if (Platform.isIOS || Platform.isMacOS) {
final config = URLSessionConfiguration.ephemeralSessionConfiguration()
..cache = URLCache.withCapacity(memoryCapacity: 2 * 1024 * 1024)
..httpAdditionalHeaders = {'User-Agent': 'Book Agent'};
httpClient = CupertinoClient.fromSessionConfiguration(config);
} else {
client = IOClient();
httpClient = IOClient(HttpClient()..userAgent = 'Book Agent');
}
runWithClient(() => runApp(const MyApp()), () => client);
}
...
class MainPageState extends State<MainPage> {
void someMethod() {
// Will use the Client configured in main.
final response = await get(Uri.https(
'www.googleapis.com',
'/books/v1/volumes',
{'q': 'HTTP', 'maxResults': '40', 'printType': 'books'}));
}
...
final response = await client.get(Uri.https(
'www.googleapis.com',
'/books/v1/volumes',
{'q': 'HTTP', 'maxResults': '40', 'printType': 'books'}));
}
```

Expand All @@ -88,6 +64,5 @@ task.resume();
```

[package:http Client]: https://pub.dev/documentation/http/latest/http/Client-class.html
[package:http runWithClient]: https://pub.dev/documentation/http/latest/http/runWithClient.html
[Foundation URL Loading System]: https://developer.apple.com/documentation/foundation/url_loading_system
[dart:io HttpClient]: https://api.dart.dev/stable/dart-io/HttpClient-class.html
3 changes: 2 additions & 1 deletion pkgs/flutter_http_example/lib/http_client_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ Client httpClient() {
}
if (Platform.isIOS || Platform.isMacOS) {
final config = URLSessionConfiguration.ephemeralSessionConfiguration()
..cache = URLCache.withCapacity(memoryCapacity: maxCacheSize);
..cache = URLCache.withCapacity(memoryCapacity: maxCacheSize)
..httpAdditionalHeaders = {'User-Agent': 'Book Agent'};
return CupertinoClient.fromSessionConfiguration(config);
}
return IOClient(HttpClient()..userAgent = 'Book Agent');
Expand Down
4 changes: 2 additions & 2 deletions pkgs/flutter_http_example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ environment:
flutter: '>=3.10.0'

dependencies:
cronet_http: ^0.4.1
cupertino_http: ^1.1.0
cronet_http: ^1.0.0
cupertino_http: ^1.2.0
cupertino_icons: ^1.0.2
fetch_client: ^1.0.2
flutter:
Expand Down

0 comments on commit 5afe399

Please sign in to comment.