2021-12-11 16:17:38 +00:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:http/http.dart' as http;
|
2022-08-31 14:22:47 +00:00
|
|
|
import 'token.dart';
|
|
|
|
|
|
|
|
class TokenException implements Exception {
|
|
|
|
String wdExpMsg() => 'Invalid token in storage!';
|
|
|
|
}
|
2021-12-11 16:17:38 +00:00
|
|
|
|
|
|
|
class API {
|
|
|
|
static Future<String> query(
|
|
|
|
String apinode, String action, Object payload) async {
|
|
|
|
final t = await Token.getInstance().getToken();
|
|
|
|
if (t != null) {
|
|
|
|
final resp = await http.post(
|
|
|
|
Uri.parse(t.domain + '/api/$apinode/$action'),
|
|
|
|
headers: <String, String>{
|
|
|
|
'Content-Type': 'application/json; charset=UTF-8',
|
|
|
|
'Token': t.token
|
|
|
|
},
|
|
|
|
body: jsonEncode(payload),
|
|
|
|
);
|
|
|
|
|
2022-08-21 20:44:12 +00:00
|
|
|
return resp.body;
|
2021-12-11 16:17:38 +00:00
|
|
|
} else {
|
2022-08-31 14:22:47 +00:00
|
|
|
throw TokenException();
|
2021-12-11 16:17:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|