From 6aab88bbfde0330821d94e6221f8f41412b74f7b Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Fri, 17 Mar 2023 21:06:41 +0100 Subject: [PATCH] Basic request backend --- lib/backend/request.dart | 64 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 lib/backend/request.dart diff --git a/lib/backend/request.dart b/lib/backend/request.dart new file mode 100644 index 0000000..39df13f --- /dev/null +++ b/lib/backend/request.dart @@ -0,0 +1,64 @@ +import 'package:http/http.dart' as http; +import 'dart:convert'; +import './resolve_url.dart'; +import './storage.dart'; + +enum Result { + ok, + err +} +class Response { + final Map body; + final Result res; + + const Response({required this.body, required this.res}); +} + +Future usePostApi( + {required OutbagServer target, + String path = '', + required Map headers, + required Map 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 postWithCreadentials( + {required OutbagServer target, + String path = '', + required Map body, + required LoginDetails credentials}) async { + Map headers = { + "Content-Type": "application/json", + 'Authorization': + 'Digest name=${credentials.username} server=${target.base} accountKey=${credentials.password}' + }; + return await usePostApi( + target: target, path: path, headers: headers, body: body); +} + +Future postWithToken( + {required OutbagServer target, + String path = '', + required Map body, + required String token}) async { + Map headers = { + "Content-Type": "application/json", + 'Authorization': 'Bearer $token' + }; + return await usePostApi( + target: target, path: path, headers: headers, body: body); +} + +Future postUnauthorized( + {required OutbagServer target, + String path = '', + required Map body}) async { + Map headers = { + "Content-Type": "application/json", + }; + return await usePostApi( + target: target, path: path, headers: headers, body: body); +}