Implement local HTTP Api usage
This commit is contained in:
39
lib/api/apiRequest.dart
Normal file
39
lib/api/apiRequest.dart
Normal 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()}");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user