remove address book operation code, as it duplicates the the functionality of web console (#7451)

Signed-off-by: 21pages <pages21@163.com>
This commit is contained in:
21pages
2024-03-21 14:01:18 +08:00
committed by GitHub
parent f421a14659
commit 67bc26ed57
46 changed files with 48 additions and 1218 deletions

View File

@@ -263,71 +263,6 @@ class AbModel {
return false;
}
Future<String> addSharedAb(String name, String note) async {
try {
if (addressbooks.containsKey(name)) {
return '$name already exists';
}
final api = "${await bind.mainGetApiServer()}/api/ab/shared/add";
var headers = getHttpHeaders();
headers['Content-Type'] = "application/json";
var v = {
'name': name,
};
if (note.isNotEmpty) {
v['note'] = note;
}
final body = jsonEncode(v);
final resp =
await http.post(Uri.parse(api), headers: headers, body: body);
final errMsg = _jsonDecodeActionResp(resp);
return errMsg;
} catch (e) {
return e.toString();
}
}
Future<String> updateSharedAb(String guid, String name, String note) async {
try {
final api =
"${await bind.mainGetApiServer()}/api/ab/shared/update/profile";
var headers = getHttpHeaders();
headers['Content-Type'] = "application/json";
var v = {
'guid': guid,
'name': name,
};
if (note.isNotEmpty) {
v['note'] = note;
}
final body = jsonEncode(v);
final resp = await http.put(Uri.parse(api), headers: headers, body: body);
final errMsg = _jsonDecodeActionResp(resp);
return errMsg;
} catch (e) {
return e.toString();
}
}
Future<String> deleteSharedAb(String name) async {
try {
final guid = abProfiles.firstWhereOrNull((e) => e.name == name)?.guid;
if (guid == null) {
return '$name not found';
}
final api = "${await bind.mainGetApiServer()}/api/ab/shared";
var headers = getHttpHeaders();
headers['Content-Type'] = "application/json";
final body = jsonEncode([guid]);
final resp =
await http.delete(Uri.parse(api), headers: headers, body: body);
final errMsg = _jsonDecodeActionResp(resp);
return errMsg;
} catch (e) {
return e.toString();
}
}
// #endregion
// #region rule
@@ -341,173 +276,6 @@ class AbModel {
return list;
}
Future<List<AbRulePayload>> getAllRules() async {
try {
List<AbRulePayload> res = [];
final abGuid = current.sharedProfile()?.guid;
if (abGuid == null) {
return res;
}
final api = "${await bind.mainGetApiServer()}/api/ab/rules";
var uri0 = Uri.parse(api);
var headers = getHttpHeaders();
headers['Content-Type'] = "application/json";
final pageSize = 100;
var total = 0;
int currentPage = 0;
do {
currentPage += 1;
var uri = Uri(
scheme: uri0.scheme,
host: uri0.host,
path: uri0.path,
port: uri0.port,
queryParameters: {
'current': currentPage.toString(),
'pageSize': pageSize.toString(),
'ab': abGuid,
});
final resp = await http.post(uri, headers: headers);
Map<String, dynamic> json =
_jsonDecodeRespMap(utf8.decode(resp.bodyBytes), resp.statusCode);
if (resp.statusCode == 404) {
debugPrint(
"HTTP 404, api server doesn't support shared address book");
return res;
}
if (json.containsKey('error')) {
throw json['error'];
}
if (resp.statusCode != 200) {
throw 'HTTP ${resp.statusCode}';
}
if (json.containsKey('total')) {
if (total == 0) total = json['total'];
if (json.containsKey('data')) {
final data = json['data'];
if (data is List) {
for (final d in data) {
final t = AbRulePayload.fromJson(d);
res.add(t);
}
}
}
}
} while (currentPage * pageSize < total);
return res;
} catch (err) {
debugPrint('get all rules err: ${err.toString()}');
}
return [];
}
Future<String?> addRule(String? user, String? group, int rule) async {
try {
final abGuid = current.sharedProfile()?.guid;
if (abGuid == null) {
return "shared profile not found";
}
final api = "${await bind.mainGetApiServer()}/api/ab/rule";
var headers = getHttpHeaders();
headers['Content-Type'] = "application/json";
final body = jsonEncode({
'guid': abGuid,
'user': user,
'group': group,
'rule': rule,
});
final resp =
await http.post(Uri.parse(api), headers: headers, body: body);
final errMsg = _jsonDecodeActionResp(resp);
if (errMsg.isNotEmpty) {
return errMsg;
}
return null;
} catch (err) {
return err.toString();
}
}
Future<String?> updateRule(String ruleGuid, int rule) async {
try {
final abGuid = current.sharedProfile()?.guid;
if (abGuid == null) {
return "shared profile not found";
}
final api = "${await bind.mainGetApiServer()}/api/ab/rule";
var headers = getHttpHeaders();
headers['Content-Type'] = "application/json";
final body = jsonEncode({
'guid': ruleGuid,
'rule': rule,
});
final resp =
await http.patch(Uri.parse(api), headers: headers, body: body);
final errMsg = _jsonDecodeActionResp(resp);
if (errMsg.isNotEmpty) {
return errMsg;
}
return null;
} catch (err) {
return err.toString();
}
}
Future<String?> deleteRules(List<String> ruleGuids) async {
try {
final abGuid = current.sharedProfile()?.guid;
if (abGuid == null) {
return "shared profile not found";
}
final api = "${await bind.mainGetApiServer()}/api/ab/rules";
var headers = getHttpHeaders();
headers['Content-Type'] = "application/json";
final body = jsonEncode(ruleGuids);
final resp =
await http.delete(Uri.parse(api), headers: headers, body: body);
final errMsg = _jsonDecodeActionResp(resp);
if (errMsg.isNotEmpty) {
return errMsg;
}
return null;
} catch (err) {
return err.toString();
}
}
Future<Map<String, List<String>>> getNamesTree() async {
Map<String, List<String>> res = Map.fromEntries([]);
try {
final abGuid = current.sharedProfile()?.guid;
if (abGuid == null) {
return res;
}
final api = "${await bind.mainGetApiServer()}/api/ab/rule/tree/$abGuid";
var headers = getHttpHeaders();
headers['Content-Type'] = "application/json";
final resp = await http.post(Uri.parse(api), headers: headers);
if (resp.statusCode == 404) {
debugPrint("HTTP 404, api server doesn't support shared address book");
return res;
}
Map<String, dynamic> json =
_jsonDecodeRespMap(utf8.decode(resp.bodyBytes), resp.statusCode);
if (resp.statusCode != 200) {
throw 'HTTP ${resp.statusCode}';
}
json.forEach((key, value) {
if (value is List) {
res[key] = value.map((e) => e.toString()).toList();
}
});
return res;
} catch (err) {
debugPrint('get name tree err: ${err.toString()}');
}
return res;
}
// #endregion
// #region peer