Implement local HTTP Api usage

This commit is contained in:
2023-02-14 18:06:18 +01:00
parent 83ad7d59d2
commit 7432972b3c
27 changed files with 1300 additions and 59 deletions

39
lib/api/apiRequest.dart Normal file
View File

@ -0,0 +1,39 @@
import 'dart:developer';
import 'package:http/http.dart' as http;
import 'package:marianum_mobile/api/apiError.dart';
class ApiRequest {
Uri endpoint;
ApiRequest(this.endpoint);
Future<http.Response> get(Map<String, String>? headers) async {
return await http.get(endpoint, headers: headers);
}
Future<http.Response> post(String data, Map<String, String>? headers) async {
log("Fetching: ${data}");
try {
http.Response response = await http
.post(endpoint, body: data, headers: headers)
.timeout(
const Duration(seconds: 10),
onTimeout: () {
log("timeout!");
throw ApiError("Network timeout");
}
);
if(response.statusCode != 200) {
log("Got ${response.statusCode}");
throw ApiError("Service response invalid, got status ${response.statusCode}");
}
return response;
} on Exception catch(e) {
throw ApiError("Http: ${e.toString()}");
}
}
}