From: Kevin Worth Date: Tue, 26 May 2020 11:33:07 +0000 (-0400) Subject: flempires: Create new class WebService.dart X-Git-Url: https://git.cworth.org/git?p=lmno.games;a=commitdiff_plain;h=d977a912c29fa640d2fba5b5774dbf31dd34e417 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. --- 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); + } + } +}