Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use package:http_image_provider in all Client implementation examples #1089

Merged
merged 18 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkgs/cronet_http/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.0.1-wip

* Use `package:http_image_provider` in the example application.

## 1.0.0

* No functional changes.
Expand Down
4 changes: 2 additions & 2 deletions pkgs/cronet_http/example/lib/book.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class Book {
String title;
String description;
String imageUrl;
Uri imageUrl;

Book(this.title, this.description, this.imageUrl);

Expand All @@ -21,7 +21,7 @@ class Book {
'description': final String description,
'imageLinks': {'smallThumbnail': final String thumbnail}
}) {
books.add(Book(title, description, thumbnail));
books.add(Book(title, description, Uri.parse(thumbnail)));
}
}
}
Expand Down
35 changes: 23 additions & 12 deletions pkgs/cronet_http/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,31 @@
import 'dart:convert';
import 'dart:io';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:cronet_http/cronet_http.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:http/io_client.dart';
import 'package:http_image_provider/http_image_provider.dart';
import 'package:provider/provider.dart';

import 'book.dart';

void main() {
var clientFactory = Client.new; // Constructs the default client.
late Client httpClient;
brianquinlan marked this conversation as resolved.
Show resolved Hide resolved
if (Platform.isAndroid) {
final engine = CronetEngine.build(
cacheMode: CacheMode.memory, userAgent: 'Book Agent');
clientFactory = () => CronetClient.fromCronetEngine(engine);
cacheMode: CacheMode.memory,
cacheMaxSize: 2 * 1024 * 1024,
userAgent: 'Book Agent');
httpClient = CronetClient.fromCronetEngine(engine);
} else {
httpClient = IOClient(HttpClient()..userAgent = 'Book Agent');
}
runWithClient(() => runApp(const BookSearchApp()), clientFactory);

runApp(Provider<Client>(
create: (_) => httpClient,
child: const BookSearchApp(),
dispose: (_, client) => client.close()));
}

class BookSearchApp extends StatelessWidget {
Expand All @@ -44,20 +54,22 @@ class HomePage extends StatefulWidget {
class _HomePageState extends State<HomePage> {
List<Book>? _books;
String? _lastQuery;
late Client _client;

@override
void initState() {
super.initState();
_client = context.read<Client>();
}

// Get the list of books matching `query`.
// The `get` call will automatically use the `client` configurated in `main`.
Future<List<Book>> _findMatchingBooks(String query) async {
final response = await get(
final response = await _client.get(
Uri.https(
'www.googleapis.com',
'/books/v1/volumes',
{'q': query, 'maxResults': '40', 'printType': 'books'},
{'q': query, 'maxResults': '20', 'printType': 'books'},
),
);

Expand Down Expand Up @@ -129,11 +141,10 @@ class _BookListState extends State<BookList> {
itemBuilder: (context, index) => Card(
key: ValueKey(widget.books[index].title),
child: ListTile(
leading: CachedNetworkImage(
placeholder: (context, url) =>
const CircularProgressIndicator(),
imageUrl:
widget.books[index].imageUrl.replaceFirst('http', 'https')),
leading: Image(
image: HttpImage(
widget.books[index].imageUrl.replace(scheme: 'https'),
client: context.read<Client>())),
title: Text(widget.books[index].title),
subtitle: Text(widget.books[index].description),
),
Expand Down
3 changes: 2 additions & 1 deletion pkgs/cronet_http/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ environment:
sdk: ^3.0.0

dependencies:
cached_network_image: ^3.2.3
cronet_http:
path: ../
cupertino_icons: ^1.0.2
flutter:
sdk: flutter
http: ^1.0.0
http_image_provider: ^0.0.2
provider: ^6.1.1

dev_dependencies:
dart_flutter_team_lints: ^2.0.0
Expand Down
2 changes: 1 addition & 1 deletion pkgs/cronet_http/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: cronet_http
version: 1.0.0
version: 1.0.1-wip
description: >-
An Android Flutter plugin that provides access to the Cronet HTTP client.
repository: https://github.com/dart-lang/http/tree/master/pkgs/cronet_http
Expand Down
4 changes: 4 additions & 0 deletions pkgs/cupertino_http/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.2.1-wip

* Use `package:http_image_provider` in the example application.

## 1.2.0

* Add support for setting additional http headers in
Expand Down
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;
brianquinlan marked this conversation as resolved.
Show resolved Hide resolved
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
4 changes: 2 additions & 2 deletions pkgs/cupertino_http/example/lib/book.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class Book {
String title;
String description;
String imageUrl;
Uri imageUrl;

Book(this.title, this.description, this.imageUrl);

Expand All @@ -21,7 +21,7 @@ class Book {
'description': final String description,
'imageLinks': {'smallThumbnail': final String thumbnail}
}) {
books.add(Book(title, description, thumbnail));
books.add(Book(title, description, Uri.parse(thumbnail)));
}
}
}
Expand Down
31 changes: 20 additions & 11 deletions pkgs/cupertino_http/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,30 @@
import 'dart:convert';
import 'dart:io';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:cupertino_http/cupertino_http.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:http/io_client.dart';
import 'package:http_image_provider/http_image_provider.dart';
import 'package:provider/provider.dart';

import 'book.dart';

void main() {
var clientFactory = Client.new; // The default Client.
late Client httpClient;
brianquinlan marked this conversation as resolved.
Show resolved Hide resolved
if (Platform.isIOS || Platform.isMacOS) {
final config = URLSessionConfiguration.ephemeralSessionConfiguration()
..cache = URLCache.withCapacity(memoryCapacity: 2 * 1024 * 1024)
..httpAdditionalHeaders = {'User-Agent': 'Book Agent'};
clientFactory = () => CupertinoClient.fromSessionConfiguration(config);
httpClient = CupertinoClient.fromSessionConfiguration(config);
} else {
httpClient = IOClient(HttpClient()..userAgent = 'Book Agent');
}
runWithClient(() => runApp(const BookSearchApp()), clientFactory);

runApp(Provider<Client>(
create: (_) => httpClient,
child: const BookSearchApp(),
dispose: (_, client) => client.close()));
}

class BookSearchApp extends StatelessWidget {
Expand All @@ -45,20 +53,22 @@ class HomePage extends StatefulWidget {
class _HomePageState extends State<HomePage> {
List<Book>? _books;
String? _lastQuery;
late Client _client;

@override
void initState() {
super.initState();
_client = context.read<Client>();
}

// Get the list of books matching `query`.
// The `get` call will automatically use the `client` configurated in `main`.
Future<List<Book>> _findMatchingBooks(String query) async {
final response = await get(
final response = await _client.get(
Uri.https(
'www.googleapis.com',
'/books/v1/volumes',
{'q': query, 'maxResults': '40', 'printType': 'books'},
{'q': query, 'maxResults': '20', 'printType': 'books'},
),
);

Expand Down Expand Up @@ -130,11 +140,10 @@ class _BookListState extends State<BookList> {
itemBuilder: (context, index) => Card(
key: ValueKey(widget.books[index].title),
child: ListTile(
leading: CachedNetworkImage(
placeholder: (context, url) =>
const CircularProgressIndicator(),
imageUrl:
widget.books[index].imageUrl.replaceFirst('http', 'https')),
leading: Image(
image: HttpImage(
widget.books[index].imageUrl.replace(scheme: 'https'),
client: context.read<Client>())),
title: Text(widget.books[index].title),
subtitle: Text(widget.books[index].description),
),
Expand Down
3 changes: 2 additions & 1 deletion pkgs/cupertino_http/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ environment:
flutter: '>=3.10.0'

dependencies:
cached_network_image: ^3.2.3
cupertino_http:
path: ../
cupertino_icons: ^1.0.2
flutter:
sdk: flutter
http: ^1.0.0
http_image_provider: ^0.0.2
provider: ^6.1.1

dev_dependencies:
convert: ^3.1.1
Expand Down
2 changes: 1 addition & 1 deletion pkgs/cupertino_http/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: cupertino_http
version: 1.2.0
version: 1.2.1-wip
description: >-
A macOS/iOS Flutter plugin that provides access to the Foundation URL
Loading System.
Expand Down
8 changes: 3 additions & 5 deletions pkgs/flutter_http_example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ A Flutter sample app that illustrates how to configure and use
including:

* configuration for multiple platforms.
* using `runWithClient` and `package:provider` to pass `Client`s through
an application.
* using `package:provider` to pass `Client`s through an application.
* writing tests using `MockClient`.

## The important bits
Expand All @@ -34,9 +33,8 @@ This library demonstrates how to:

* import `http_client_factory.dart` or `http_client_factory_web.dart`,
depending on whether we are targeting the web browser or not.
* share a `package:http` `Client` by using `runWithClient` and
`package:provider`.
* call `package:http` functions.
* share a `package:http` `Client` by using `package:provider`.
* call `package:http` `Client` methods.

### `widget_test.dart`

Expand Down
4 changes: 2 additions & 2 deletions pkgs/flutter_http_example/lib/book.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class Book {
String title;
String description;
String imageUrl;
Uri imageUrl;

Book(this.title, this.description, this.imageUrl);

Expand All @@ -21,7 +21,7 @@ class Book {
'description': final String description,
'imageLinks': {'smallThumbnail': final String thumbnail}
}) {
books.add(Book(title, description, thumbnail));
books.add(Book(title, description, Uri.parse(thumbnail)));
}
}
}
Expand Down
16 changes: 13 additions & 3 deletions pkgs/flutter_http_example/lib/http_client_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,23 @@ import 'dart:io';
import 'package:cronet_http/cronet_http.dart';
import 'package:cupertino_http/cupertino_http.dart';
import 'package:http/http.dart';
import 'package:http/io_client.dart';

const maxCacheSize = 2 * 1024 * 1024;
brianquinlan marked this conversation as resolved.
Show resolved Hide resolved

Client httpClient() {
if (Platform.isAndroid) {
return CronetClient.defaultCronetEngine();
final engine = CronetEngine.build(
cacheMode: CacheMode.memory,
cacheMaxSize: maxCacheSize,
userAgent: 'Book Agent');
return CronetClient.fromCronetEngine(engine);
}
if (Platform.isIOS || Platform.isMacOS) {
return CupertinoClient.defaultSessionConfiguration();
final config = URLSessionConfiguration.ephemeralSessionConfiguration()
..cache = URLCache.withCapacity(memoryCapacity: maxCacheSize)
..httpAdditionalHeaders = {'User-Agent': 'Book Agent'};
return CupertinoClient.fromSessionConfiguration(config);
}
return Client(); // Return the default client.
return IOClient(HttpClient()..userAgent = 'Book Agent');
}
Loading
Loading