actions-test/lib/backend/request.dart
Jakob Meier eba0790f7c
More bug fixes (see description)
- Fixed bug where opening room would show null-error,
  for a couple of seconds.
  This was caused by the FAB not checking if widget.info!=null

- Added try&catch for item category,
  to prevent index-out-of-bounds when the item uses a category
  that no longer exists

- Renamed add/remove Articles to fit the new name: ShoppingListItem
2023-04-05 09:23:41 +02:00

64 lines
1.8 KiB
Dart

import 'package:http/http.dart' as http;
import 'dart:convert';
import './resolve_url.dart';
import './user.dart';
enum Result { ok, err }
class Response {
final Map<String, dynamic> body;
final Result res;
const Response({required this.body, required this.res});
}
Future<Response> usePostApi(
{required OutbagServer target,
String path = '',
required Map<String, String> headers,
required Map<String, dynamic> body}) async {
final resp = await http.post(Uri.parse('${target.base}api/$path'),
headers: headers, body: jsonEncode({'data': body}));
final json = jsonDecode(resp.body);
return Response(
body: json, res: resp.statusCode == 200 ? Result.ok : Result.err);
}
Future<Response> postWithCreadentials(
{required OutbagServer target,
String path = '',
required Map<String, dynamic> body,
required User credentials}) async {
Map<String, String> headers = {
"Content-Type": "application/json",
'Connection': 'keep-alive',
'Authorization':
'Digest name=${credentials.username} server=${target.tag} accountKey=${credentials.password}'
};
return await usePostApi(
target: target, path: path, headers: headers, body: body);
}
Future<Response> postWithToken(
{required OutbagServer target,
String path = '',
required Map<String, dynamic> body,
required String token}) async {
Map<String, String> headers = {
"Content-Type": "application/json",
'Authorization': 'Bearer $token'
};
return await usePostApi(
target: target, path: path, headers: headers, body: body);
}
Future<Response> postUnauthorized(
{required OutbagServer target,
String path = '',
required Map<String, dynamic> body}) async {
Map<String, String> headers = {
"Content-Type": "application/json",
};
return await usePostApi(
target: target, path: path, headers: headers, body: body);
}