From d977a912c29fa640d2fba5b5774dbf31dd34e417 Mon Sep 17 00:00:00 2001 From: Kevin Worth Date: Tue, 26 May 2020 07:33:07 -0400 Subject: [PATCH] flempires: Create new class WebService.dart This will allow us to avoid reinventing the wheel each time there's a new web api endpoint to fetch from. --- flempires/lib/WebService.dart | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 flempires/lib/WebService.dart diff --git a/flempires/lib/WebService.dart b/flempires/lib/WebService.dart new file mode 100644 index 0000000..d189a4e --- /dev/null +++ b/flempires/lib/WebService.dart @@ -0,0 +1,23 @@ +import 'package:http/http.dart' as http; +import 'package:http/http.dart'; + +class Request { + final String url; + T Function(Response response) parse; + final String type; + + Request({this.url, this.parse, this.type}); +} + +class WebService { + Future fetch(Request 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); + } + } +} -- 2.43.0