Basic request backend
This commit is contained in:
parent
fd1c104e6a
commit
6aab88bbfd
1 changed files with 64 additions and 0 deletions
64
lib/backend/request.dart
Normal file
64
lib/backend/request.dart
Normal file
|
@ -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<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 LoginDetails credentials}) async {
|
||||||
|
Map<String, String> 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<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);
|
||||||
|
}
|
Loading…
Reference in a new issue