1
0
mirror of https://github.com/krawieck/lemmur/ synced 2024-12-18 19:33:35 +01:00
lemmur-app-android/lib/client/http_helper.dart
2020-08-13 20:25:48 +02:00

40 lines
882 B
Dart

import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart' as http;
extension OkResponse on http.Response {
bool get ok => statusCode >= 200 && statusCode < 300;
}
mixin HttpHelper {
String host;
String extraPath;
Future<Map<String, dynamic>> get(
String path, Map<String, String> query) async {
var res = await http.get(Uri.https(host, '$extraPath$path', query));
if (!res.ok) {
// failed request, handle here
}
return jsonDecode(res.body);
}
Future<Map<String, dynamic>> post(
String path, Map<String, dynamic> body) async {
var res = await http.post(
Uri.https(host, '$extraPath$path'),
body: jsonEncode(body),
headers: {HttpHeaders.contentTypeHeader: ContentType.json.mimeType},
);
if (!res.ok) {
// failed request, handle here
}
return jsonDecode(res.body);
}
}