Skip to content

Commit

Permalink
Use Uri.https where it makes sense
Browse files Browse the repository at this point in the history
  • Loading branch information
rrousselGit committed Aug 5, 2023
1 parent cd0e1eb commit 9d4edfd
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 59 deletions.
55 changes: 14 additions & 41 deletions examples/pub/lib/pub_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,17 @@ class PubRepository {
_configureDio();
}

static const _scheme = 'https';
static const _host = 'pub.dartlang.org';
final dio = Dio();

Future<List<Package>> getPackages({
required int page,
CancelToken? cancelToken,
}) async {
final uri = Uri(
scheme: _scheme,
host: _host,
path: 'api/packages',
queryParameters: <String, String>{'page': '$page'},
final uri = Uri.https(
_host,
'api/packages',
<String, String>{'page': '$page'},
);

final response = await dio.getUri<Map<String, Object?>>(
Expand All @@ -40,11 +38,10 @@ class PubRepository {
required String search,
CancelToken? cancelToken,
}) async {
final uri = Uri(
scheme: _scheme,
host: _host,
path: 'api/search',
queryParameters: <String, String>{'page': '$page', 'q': search},
final uri = Uri.https(
_host,
'api/search',
<String, String>{'page': '$page', 'q': search},
);
// Returns {packages: [{ package: string }]}
final response = await dio.getUri<Map<String, Object?>>(
Expand All @@ -60,11 +57,7 @@ class PubRepository {
required String packageName,
CancelToken? cancelToken,
}) async {
final uri = Uri(
scheme: _scheme,
host: _host,
path: 'api/packages/$packageName',
);
final uri = Uri.https(_host, 'api/packages/$packageName');

final response = await dio.getUri<Map<String, Object?>>(
uri,
Expand All @@ -79,22 +72,14 @@ class PubRepository {
required String packageName,
CancelToken? cancelToken,
}) async {
final uri = Uri(
scheme: _scheme,
host: _host,
path: 'api/packages/$packageName/metrics',
);
final uri = Uri.https(_host, 'api/packages/$packageName/metrics');

final responseFuture = dio.getUri<Map<String, Object?>>(
uri,
cancelToken: cancelToken,
);

final likesUri = Uri(
scheme: _scheme,
host: _host,
path: 'api/packages/$packageName/likes',
);
final likesUri = Uri.https(_host, 'api/packages/$packageName/likes');

/// Although the metrics request does include the likes count, it seems that
/// the server caches the response for a long period of time.
Expand All @@ -116,11 +101,7 @@ class PubRepository {
required String packageName,
CancelToken? cancelToken,
}) async {
final uri = Uri(
scheme: _scheme,
host: _host,
path: 'api/account/likes/$packageName',
);
final uri = Uri.https(_host, 'api/account/likes/$packageName');

await dio.putUri<void>(
uri,
Expand All @@ -135,11 +116,7 @@ class PubRepository {
required String packageName,
CancelToken? cancelToken,
}) async {
final uri = Uri(
scheme: _scheme,
host: _host,
path: 'api/account/likes/$packageName',
);
final uri = Uri.https(_host, 'api/account/likes/$packageName');

await dio.deleteUri<void>(
uri,
Expand All @@ -149,11 +126,7 @@ class PubRepository {
}

Future<List<String>> getLikedPackages({CancelToken? cancelToken}) async {
final uri = Uri(
scheme: _scheme,
host: _host,
path: 'api/account/likes',
);
final uri = Uri.https(_host, 'api/account/likes');

final response = await dio.getUri<Map<String, Object?>>(
uri,
Expand Down
4 changes: 1 addition & 3 deletions website/docs/essentials/first_request/codegen/provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ part 'provider.g.dart';
@riverpod
Future<Activity> activity(ActivityRef ref) async {
// Using package:http, we fetch a random activity from the Bored API.
final response = await http.get(
Uri(scheme: 'https', host: 'boredapi.com', path: '/api/activity'),
);
final response = await http.get(Uri.https('boredapi.com', '/api/activity'));
// Using dart:convert, we then decode the JSON payload into a Map data structure.
final json = jsonDecode(response.body) as Map<String, dynamic>;
// Finally, we convert the Map into an Activity instance.
Expand Down
4 changes: 1 addition & 3 deletions website/docs/essentials/first_request/raw/provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import 'activity.dart';

final activityProvider = FutureProvider.autoDispose((ref) async {
// Using package:http, we fetch a random activity from the Bored API.
final response = await http.get(
Uri(scheme: 'https', host: 'boredapi.com', path: '/api/activity'),
);
final response = await http.get(Uri.https('boredapi.com', '/api/activity'));
// Using dart:convert, we then decode the JSON payload into a Map data structure.
final json = jsonDecode(response.body) as Map<String, dynamic>;
// Finally, we convert the Map into an Activity instance.
Expand Down
4 changes: 1 addition & 3 deletions website/docs/essentials/passing_args/codegen/provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ part 'provider.g.dart';
/* SNIPPET START */
@riverpod
Future<Activity> activity(ActivityRef ref) async {
final response = await http.get(
Uri(scheme: 'https', host: 'boredapi.com', path: '/api/activity'),
);
final response = await http.get(Uri.https('boredapi.com', '/api/activity'));
final json = jsonDecode(response.body) as Map<String, dynamic>;
return Activity.fromJson(json);
}
4 changes: 1 addition & 3 deletions website/docs/essentials/passing_args/raw/provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import '../../first_request/raw/activity.dart';
/* SNIPPET START */

final activityProvider = FutureProvider.autoDispose((ref) async {
final response = await http.get(
Uri(scheme: 'https', host: 'boredapi.com', path: '/api/activity'),
);
final response = await http.get(Uri.https('boredapi.com', '/api/activity'));
final json = jsonDecode(response.body) as Map<String, dynamic>;
return Activity.fromJson(json);
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class TodoList extends _$TodoList {

Future<void> addTodo(Todo todo) async {
await http.post(
Uri(scheme: 'https', host: 'your_api.com', path: '/todos'),
Uri.https('your_api.com', '/todos'),
// We serialize our Todo object and POST it to the server.
headers: {'Content-Type': 'application/json'},
body: jsonEncode(todo.toJson()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class TodoList extends AutoDisposeAsyncNotifier<List<Todo>> {
Future<void> addTodo(Todo todo) async {
// We don't care about the API response
await http.post(
Uri(scheme: 'https', host: 'your_api.com', path: '/todos'),
Uri.https('your_api.com', '/todos'),
headers: {'Content-Type': 'application/json'},
body: jsonEncode(todo.toJson()),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class TodoList extends AutoDisposeAsyncNotifier<List<Todo>> {
Future<void> addTodo(Todo todo) async {
// We don't care about the API response
await http.post(
Uri(scheme: 'https', host: 'your_api.com', path: '/todos'),
Uri.https('your_api.com', '/todos'),
headers: {'Content-Type': 'application/json'},
body: jsonEncode(todo.toJson()),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class TodoList extends AutoDisposeAsyncNotifier<List<Todo>> {
Future<void> addTodo(Todo todo) async {
// We don't care about the API response
await http.post(
Uri(scheme: 'https', host: 'your_api.com', path: '/todos'),
Uri.https('your_api.com', '/todos'),
headers: {'Content-Type': 'application/json'},
body: jsonEncode(todo.toJson()),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class TodoList extends AutoDisposeAsyncNotifier<List<Todo>> {
Future<void> addTodo(Todo todo) async {
// The POST request will return a List<Todo> matching the new application state
final response = await http.post(
Uri(scheme: 'https', host: 'your_api.com', path: '/todos'),
Uri.https('your_api.com', '/todos'),
headers: {'Content-Type': 'application/json'},
body: jsonEncode(todo.toJson()),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class TodoList extends AutoDisposeAsyncNotifier<List<Todo>> {

Future<void> addTodo(Todo todo) async {
await http.post(
Uri(scheme: 'https', host: 'your_api.com', path: '/todos'),
Uri.https('your_api.com', '/todos'),
// We serialize our Todo object and POST it to the server.
headers: {'Content-Type': 'application/json'},
body: jsonEncode(todo.toJson()),
Expand Down

0 comments on commit 9d4edfd

Please sign in to comment.