39 lines
1003 B
Dart
39 lines
1003 B
Dart
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()}");
|
|
}
|
|
|
|
}
|
|
} |