This will allow us to avoid reinventing the wheel each time there's a
new web api endpoint to fetch from.
--- /dev/null
+import 'package:http/http.dart' as http;
+import 'package:http/http.dart';
+
+class Request<T> {
+ final String url;
+ T Function(Response response) parse;
+ final String type;
+
+ Request({this.url, this.parse, this.type});
+}
+
+class WebService {
+ Future<T> fetch<T>(Request<T> request) async {
+ final response = await http.get(request.url);
+
+ if (response.statusCode == 200) {
+ return request.parse(response);
+ } else {
+ throw Exception(
+ 'Failed to fetch ' + request.type + ' from url ' + request.url);
+ }
+ }
+}