2021-03-25 23:30:34 +00:00
|
|
|
import 'dart:async';
|
2022-02-01 01:56:05 +00:00
|
|
|
import 'dart:convert';
|
2022-02-08 06:59:35 +00:00
|
|
|
import 'dart:io';
|
2021-01-21 21:01:42 +00:00
|
|
|
|
2021-01-21 07:35:38 +00:00
|
|
|
import 'package:dio/dio.dart';
|
2021-03-25 23:30:34 +00:00
|
|
|
import 'package:selfprivacy/config/get_it_config.dart';
|
2022-09-04 11:29:05 +00:00
|
|
|
import 'package:selfprivacy/logic/api_maps/rest_maps/api_map.dart';
|
2021-08-29 13:54:28 +00:00
|
|
|
import 'package:selfprivacy/logic/common_enum/common_enum.dart';
|
2022-05-18 10:39:11 +00:00
|
|
|
import 'package:selfprivacy/logic/models/hive/backblaze_bucket.dart';
|
2022-06-05 19:36:32 +00:00
|
|
|
import 'package:selfprivacy/logic/models/hive/server_domain.dart';
|
2022-05-18 10:39:11 +00:00
|
|
|
import 'package:selfprivacy/logic/models/hive/user.dart';
|
2022-05-14 02:54:40 +00:00
|
|
|
import 'package:selfprivacy/logic/models/json/api_token.dart';
|
|
|
|
import 'package:selfprivacy/logic/models/json/auto_upgrade_settings.dart';
|
|
|
|
import 'package:selfprivacy/logic/models/json/backup.dart';
|
|
|
|
import 'package:selfprivacy/logic/models/json/device_token.dart';
|
2022-05-18 10:39:11 +00:00
|
|
|
import 'package:selfprivacy/logic/models/json/recovery_token_status.dart';
|
2022-02-08 21:01:08 +00:00
|
|
|
import 'package:selfprivacy/logic/models/timezone_settings.dart';
|
2021-01-21 07:35:38 +00:00
|
|
|
|
2022-03-03 17:38:30 +00:00
|
|
|
class ApiResponse<D> {
|
|
|
|
ApiResponse({
|
|
|
|
required this.statusCode,
|
|
|
|
required this.data,
|
2022-06-05 19:36:32 +00:00
|
|
|
this.errorMessage,
|
2022-03-03 17:38:30 +00:00
|
|
|
});
|
2022-06-05 19:36:32 +00:00
|
|
|
final int statusCode;
|
|
|
|
final String? errorMessage;
|
|
|
|
final D data;
|
|
|
|
|
|
|
|
bool get isSuccess => statusCode >= 200 && statusCode < 300;
|
2022-03-03 17:38:30 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 23:30:34 +00:00
|
|
|
class ServerApi extends ApiMap {
|
2022-06-05 22:40:34 +00:00
|
|
|
ServerApi({
|
|
|
|
this.hasLogger = false,
|
|
|
|
this.isWithToken = true,
|
|
|
|
this.overrideDomain,
|
|
|
|
this.customToken,
|
|
|
|
});
|
2022-05-20 22:56:50 +00:00
|
|
|
@override
|
2022-02-16 07:28:29 +00:00
|
|
|
bool hasLogger;
|
2022-05-20 22:56:50 +00:00
|
|
|
@override
|
2021-03-25 23:30:34 +00:00
|
|
|
bool isWithToken;
|
2022-05-13 13:57:56 +00:00
|
|
|
String? overrideDomain;
|
2022-05-18 10:39:11 +00:00
|
|
|
String? customToken;
|
2021-03-25 23:30:34 +00:00
|
|
|
|
2022-05-24 18:55:39 +00:00
|
|
|
@override
|
2021-03-25 23:30:34 +00:00
|
|
|
BaseOptions get options {
|
2022-07-22 16:22:10 +00:00
|
|
|
BaseOptions options = BaseOptions(
|
|
|
|
connectTimeout: 10000,
|
|
|
|
receiveTimeout: 10000,
|
|
|
|
);
|
2021-03-25 23:30:34 +00:00
|
|
|
|
|
|
|
if (isWithToken) {
|
2022-07-25 14:06:55 +00:00
|
|
|
final ServerDomain? serverDomain = getIt<ApiConfigModel>().serverDomain;
|
|
|
|
final String domainName = serverDomain!.domainName;
|
2022-06-05 19:36:32 +00:00
|
|
|
final String? apiToken = getIt<ApiConfigModel>().serverDetails?.apiToken;
|
2021-03-25 23:30:34 +00:00
|
|
|
|
2022-06-05 22:40:34 +00:00
|
|
|
options = BaseOptions(
|
|
|
|
baseUrl: 'https://api.$domainName',
|
2022-07-22 16:22:10 +00:00
|
|
|
connectTimeout: 10000,
|
|
|
|
receiveTimeout: 10000,
|
2022-06-05 22:40:34 +00:00
|
|
|
headers: {
|
|
|
|
'Authorization': 'Bearer $apiToken',
|
|
|
|
},
|
|
|
|
);
|
2021-03-25 23:30:34 +00:00
|
|
|
}
|
|
|
|
|
2022-05-13 13:57:56 +00:00
|
|
|
if (overrideDomain != null) {
|
2022-05-18 10:39:11 +00:00
|
|
|
options = BaseOptions(
|
|
|
|
baseUrl: 'https://api.$overrideDomain',
|
2022-07-22 16:22:10 +00:00
|
|
|
connectTimeout: 10000,
|
|
|
|
receiveTimeout: 10000,
|
2022-09-05 04:34:47 +00:00
|
|
|
headers: customToken != null
|
|
|
|
? {'Authorization': 'Bearer $customToken'}
|
|
|
|
: null,
|
2022-05-18 10:39:11 +00:00
|
|
|
);
|
2022-05-13 13:57:56 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 23:30:34 +00:00
|
|
|
return options;
|
2021-01-21 21:01:42 +00:00
|
|
|
}
|
|
|
|
|
2022-05-13 13:57:56 +00:00
|
|
|
Future<String?> getApiVersion() async {
|
|
|
|
Response response;
|
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2022-05-24 18:55:39 +00:00
|
|
|
String? apiVersion;
|
2022-05-13 13:57:56 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
response = await client.get('/api/version');
|
|
|
|
apiVersion = response.data['version'];
|
|
|
|
} on DioError catch (e) {
|
|
|
|
print(e.message);
|
|
|
|
} finally {
|
|
|
|
close(client);
|
|
|
|
}
|
2022-05-24 18:55:39 +00:00
|
|
|
return apiVersion;
|
2022-05-13 13:57:56 +00:00
|
|
|
}
|
|
|
|
|
2021-01-21 21:01:42 +00:00
|
|
|
Future<bool> isHttpServerWorking() async {
|
2022-05-13 13:57:56 +00:00
|
|
|
bool res = false;
|
2021-01-21 21:01:42 +00:00
|
|
|
Response response;
|
2021-03-25 23:30:34 +00:00
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2021-01-21 21:01:42 +00:00
|
|
|
try {
|
2022-05-13 13:57:56 +00:00
|
|
|
response = await client.get('/services/status');
|
2021-01-21 21:01:42 +00:00
|
|
|
res = response.statusCode == HttpStatus.ok;
|
2022-08-24 05:35:49 +00:00
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
2022-05-13 13:57:56 +00:00
|
|
|
} finally {
|
|
|
|
close(client);
|
2021-01-21 07:35:38 +00:00
|
|
|
}
|
2022-05-24 18:55:39 +00:00
|
|
|
return res;
|
2021-01-21 07:35:38 +00:00
|
|
|
}
|
2021-01-21 21:01:42 +00:00
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
Future<ApiResponse<User>> createUser(final User user) async {
|
2022-05-13 13:57:56 +00:00
|
|
|
Response response;
|
2022-05-02 11:56:46 +00:00
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2022-05-02 11:56:46 +00:00
|
|
|
try {
|
|
|
|
response = await client.post(
|
|
|
|
'/users',
|
|
|
|
data: {
|
|
|
|
'username': user.login,
|
|
|
|
'password': user.password,
|
|
|
|
},
|
|
|
|
);
|
2022-05-13 13:57:56 +00:00
|
|
|
} on DioError catch (e) {
|
|
|
|
print(e.message);
|
2022-03-03 17:38:30 +00:00
|
|
|
return ApiResponse(
|
2022-05-13 13:57:56 +00:00
|
|
|
errorMessage: e.error.toString(),
|
|
|
|
statusCode: e.response?.statusCode ?? HttpStatus.internalServerError,
|
2022-03-03 17:38:30 +00:00
|
|
|
data: User(
|
|
|
|
login: user.login,
|
2022-09-04 11:29:05 +00:00
|
|
|
type: UserType.normal,
|
2022-03-03 17:38:30 +00:00
|
|
|
password: user.password,
|
2022-05-13 13:57:56 +00:00
|
|
|
isFoundOnServer: false,
|
2021-06-20 21:08:52 +00:00
|
|
|
),
|
|
|
|
);
|
2022-05-13 13:57:56 +00:00
|
|
|
} finally {
|
|
|
|
close(client);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isFoundOnServer = false;
|
2022-05-14 02:54:40 +00:00
|
|
|
int code = 0;
|
2022-05-13 13:57:56 +00:00
|
|
|
|
2022-09-05 04:34:47 +00:00
|
|
|
final bool isUserCreated = (response.statusCode != null) &&
|
|
|
|
(response.statusCode == HttpStatus.created);
|
2022-05-13 13:57:56 +00:00
|
|
|
|
|
|
|
if (isUserCreated) {
|
|
|
|
isFoundOnServer = true;
|
2022-05-14 02:54:40 +00:00
|
|
|
code = response.statusCode!;
|
2022-05-02 11:56:46 +00:00
|
|
|
} else {
|
2022-05-13 13:57:56 +00:00
|
|
|
isFoundOnServer = false;
|
2022-05-14 02:54:40 +00:00
|
|
|
code = HttpStatus.notAcceptable;
|
2022-03-03 17:38:30 +00:00
|
|
|
}
|
2022-05-13 13:57:56 +00:00
|
|
|
|
|
|
|
return ApiResponse(
|
2022-05-14 02:54:40 +00:00
|
|
|
statusCode: code,
|
2022-05-13 13:57:56 +00:00
|
|
|
data: User(
|
|
|
|
login: user.login,
|
2022-09-04 11:29:05 +00:00
|
|
|
type: UserType.normal,
|
2022-05-13 13:57:56 +00:00
|
|
|
password: user.password,
|
|
|
|
isFoundOnServer: isFoundOnServer,
|
|
|
|
),
|
|
|
|
);
|
2022-03-03 17:38:30 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 22:40:34 +00:00
|
|
|
Future<ApiResponse<List<String>>> getUsersList({
|
|
|
|
final withMainUser = false,
|
|
|
|
}) async {
|
2022-06-05 19:36:32 +00:00
|
|
|
final List<String> res = [];
|
2022-03-03 17:38:30 +00:00
|
|
|
Response response;
|
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2022-03-03 17:38:30 +00:00
|
|
|
try {
|
2022-06-05 22:40:34 +00:00
|
|
|
response = await client.get(
|
|
|
|
'/users',
|
|
|
|
queryParameters: withMainUser ? {'withMainUser': 'true'} : null,
|
|
|
|
);
|
2022-06-05 19:36:32 +00:00
|
|
|
for (final user in response.data) {
|
2022-03-23 14:07:52 +00:00
|
|
|
res.add(user.toString());
|
|
|
|
}
|
2022-05-13 13:57:56 +00:00
|
|
|
} on DioError catch (e) {
|
|
|
|
print(e.message);
|
2022-05-14 02:54:40 +00:00
|
|
|
return ApiResponse(
|
|
|
|
errorMessage: e.message,
|
|
|
|
statusCode: e.response?.statusCode ?? HttpStatus.internalServerError,
|
|
|
|
data: [],
|
|
|
|
);
|
2022-03-03 17:38:30 +00:00
|
|
|
} catch (e) {
|
2022-03-23 14:07:52 +00:00
|
|
|
print(e);
|
2022-05-14 02:54:40 +00:00
|
|
|
return ApiResponse(
|
|
|
|
errorMessage: e.toString(),
|
|
|
|
statusCode: HttpStatus.internalServerError,
|
|
|
|
data: [],
|
|
|
|
);
|
2022-05-13 13:57:56 +00:00
|
|
|
} finally {
|
|
|
|
close(client);
|
2022-03-03 17:38:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 02:54:40 +00:00
|
|
|
final int code = response.statusCode ?? HttpStatus.internalServerError;
|
|
|
|
|
2022-05-13 13:57:56 +00:00
|
|
|
return ApiResponse(
|
|
|
|
statusCode: code,
|
2022-03-03 17:38:30 +00:00
|
|
|
data: res,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-06-05 22:40:34 +00:00
|
|
|
Future<ApiResponse<void>> addUserSshKey(
|
|
|
|
final User user,
|
|
|
|
final String sshKey,
|
|
|
|
) async {
|
2022-05-14 02:54:40 +00:00
|
|
|
late Response response;
|
2022-03-03 17:38:30 +00:00
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2022-05-13 13:57:56 +00:00
|
|
|
try {
|
|
|
|
response = await client.post(
|
|
|
|
'/services/ssh/keys/${user.login}',
|
|
|
|
data: {
|
|
|
|
'public_key': sshKey,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} on DioError catch (e) {
|
|
|
|
print(e.message);
|
2022-05-14 02:54:40 +00:00
|
|
|
return ApiResponse<void>(
|
|
|
|
errorMessage: e.message,
|
|
|
|
statusCode: e.response?.statusCode ?? HttpStatus.internalServerError,
|
|
|
|
data: null,
|
|
|
|
);
|
2022-05-13 13:57:56 +00:00
|
|
|
} finally {
|
|
|
|
close(client);
|
|
|
|
}
|
2022-03-03 17:38:30 +00:00
|
|
|
|
2022-05-14 02:54:40 +00:00
|
|
|
final int code = response.statusCode ?? HttpStatus.internalServerError;
|
|
|
|
|
2022-03-03 17:38:30 +00:00
|
|
|
return ApiResponse<void>(
|
2022-05-14 02:54:40 +00:00
|
|
|
statusCode: code,
|
2022-03-03 17:38:30 +00:00
|
|
|
data: null,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
Future<ApiResponse<void>> addRootSshKey(final String ssh) async {
|
2022-05-14 02:54:40 +00:00
|
|
|
late Response response;
|
2022-03-03 17:38:30 +00:00
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2022-05-14 02:54:40 +00:00
|
|
|
try {
|
|
|
|
response = await client.put(
|
|
|
|
'/services/ssh/key/send',
|
2022-05-24 18:55:39 +00:00
|
|
|
data: {'public_key': ssh},
|
2022-05-14 02:54:40 +00:00
|
|
|
);
|
|
|
|
} on DioError catch (e) {
|
|
|
|
print(e.message);
|
|
|
|
return ApiResponse<void>(
|
|
|
|
errorMessage: e.message,
|
|
|
|
statusCode: e.response?.statusCode ?? HttpStatus.internalServerError,
|
|
|
|
data: null,
|
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
close(client);
|
|
|
|
}
|
|
|
|
|
|
|
|
final int code = response.statusCode ?? HttpStatus.internalServerError;
|
2022-03-03 17:38:30 +00:00
|
|
|
|
|
|
|
return ApiResponse<void>(
|
2022-05-14 02:54:40 +00:00
|
|
|
statusCode: code,
|
2022-03-03 17:38:30 +00:00
|
|
|
data: null,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
Future<ApiResponse<List<String>>> getUserSshKeys(final User user) async {
|
2022-03-03 17:38:30 +00:00
|
|
|
List<String> res;
|
|
|
|
Response response;
|
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2022-03-03 17:38:30 +00:00
|
|
|
try {
|
2022-05-14 02:54:40 +00:00
|
|
|
response = await client.get('/services/ssh/keys/${user.login}');
|
2022-09-05 04:34:47 +00:00
|
|
|
res = (response.data as List<dynamic>)
|
|
|
|
.map((final e) => e as String)
|
|
|
|
.toList();
|
2022-05-14 02:54:40 +00:00
|
|
|
} on DioError catch (e) {
|
|
|
|
print(e.message);
|
|
|
|
return ApiResponse<List<String>>(
|
|
|
|
errorMessage: e.message,
|
|
|
|
statusCode: e.response?.statusCode ?? HttpStatus.internalServerError,
|
|
|
|
data: [],
|
|
|
|
);
|
2021-06-08 18:52:44 +00:00
|
|
|
} catch (e) {
|
2022-05-14 02:54:40 +00:00
|
|
|
return ApiResponse<List<String>>(
|
|
|
|
errorMessage: e.toString(),
|
|
|
|
statusCode: HttpStatus.internalServerError,
|
|
|
|
data: [],
|
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
close(client);
|
2021-06-08 18:52:44 +00:00
|
|
|
}
|
2021-06-20 21:08:52 +00:00
|
|
|
|
2022-05-14 02:54:40 +00:00
|
|
|
final int code = response.statusCode ?? HttpStatus.internalServerError;
|
|
|
|
|
2022-03-03 17:38:30 +00:00
|
|
|
return ApiResponse<List<String>>(
|
2022-05-14 02:54:40 +00:00
|
|
|
statusCode: code,
|
2022-03-03 17:38:30 +00:00
|
|
|
data: res,
|
|
|
|
errorMessage: response.data is List
|
|
|
|
? null
|
|
|
|
: response.data?.containsKey('error') ?? false
|
|
|
|
? response.data['error']
|
|
|
|
: null,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-06-05 22:40:34 +00:00
|
|
|
Future<ApiResponse<void>> deleteUserSshKey(
|
|
|
|
final User user,
|
|
|
|
final String sshKey,
|
|
|
|
) async {
|
2022-03-03 17:38:30 +00:00
|
|
|
Response response;
|
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2022-05-14 02:54:40 +00:00
|
|
|
try {
|
|
|
|
response = await client.delete(
|
|
|
|
'/services/ssh/keys/${user.login}',
|
2022-05-24 18:55:39 +00:00
|
|
|
data: {'public_key': sshKey},
|
2022-05-14 02:54:40 +00:00
|
|
|
);
|
|
|
|
} on DioError catch (e) {
|
|
|
|
print(e.message);
|
|
|
|
return ApiResponse<void>(
|
|
|
|
errorMessage: e.message,
|
|
|
|
statusCode: e.response?.statusCode ?? HttpStatus.internalServerError,
|
|
|
|
data: null,
|
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
close(client);
|
|
|
|
}
|
|
|
|
|
|
|
|
final int code = response.statusCode ?? HttpStatus.internalServerError;
|
2022-03-03 17:38:30 +00:00
|
|
|
|
|
|
|
return ApiResponse<void>(
|
2022-05-14 02:54:40 +00:00
|
|
|
statusCode: code,
|
2022-03-03 17:38:30 +00:00
|
|
|
data: null,
|
2022-09-05 04:34:47 +00:00
|
|
|
errorMessage: response.data?.containsKey('error') ?? false
|
|
|
|
? response.data['error']
|
|
|
|
: null,
|
2022-03-03 17:38:30 +00:00
|
|
|
);
|
2021-06-08 18:52:44 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
Future<bool> deleteUser(final User user) async {
|
2022-05-14 02:54:40 +00:00
|
|
|
bool res = false;
|
2021-12-20 15:25:31 +00:00
|
|
|
Response response;
|
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2021-12-20 15:25:31 +00:00
|
|
|
try {
|
2022-05-14 02:54:40 +00:00
|
|
|
response = await client.delete('/users/${user.login}');
|
2022-09-05 04:34:47 +00:00
|
|
|
res = response.statusCode == HttpStatus.ok ||
|
|
|
|
response.statusCode == HttpStatus.notFound;
|
2022-05-14 02:54:40 +00:00
|
|
|
} on DioError catch (e) {
|
|
|
|
print(e.message);
|
2021-12-20 15:25:31 +00:00
|
|
|
res = false;
|
2022-05-14 02:54:40 +00:00
|
|
|
} finally {
|
|
|
|
close(client);
|
2021-12-20 15:25:31 +00:00
|
|
|
}
|
2022-05-24 18:55:39 +00:00
|
|
|
return res;
|
2021-12-20 15:25:31 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 18:55:39 +00:00
|
|
|
@override
|
2022-09-05 04:34:47 +00:00
|
|
|
String get rootAddress =>
|
|
|
|
throw UnimplementedError('not used in with implementation');
|
2021-07-29 05:24:42 +00:00
|
|
|
|
|
|
|
Future<bool> apply() async {
|
2022-05-14 02:54:40 +00:00
|
|
|
bool res = false;
|
2021-07-29 05:24:42 +00:00
|
|
|
Response response;
|
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2021-07-29 05:24:42 +00:00
|
|
|
try {
|
2022-05-14 02:54:40 +00:00
|
|
|
response = await client.get('/system/configuration/apply');
|
2021-07-29 05:24:42 +00:00
|
|
|
res = response.statusCode == HttpStatus.ok;
|
2022-05-14 02:54:40 +00:00
|
|
|
} on DioError catch (e) {
|
|
|
|
print(e.message);
|
2021-07-29 05:24:42 +00:00
|
|
|
res = false;
|
2022-05-14 02:54:40 +00:00
|
|
|
} finally {
|
|
|
|
close(client);
|
2021-07-29 05:24:42 +00:00
|
|
|
}
|
2022-05-24 18:55:39 +00:00
|
|
|
return res;
|
2021-07-29 05:24:42 +00:00
|
|
|
}
|
2021-08-29 13:54:28 +00:00
|
|
|
|
2022-06-05 22:40:34 +00:00
|
|
|
Future<void> switchService(
|
|
|
|
final ServiceTypes type,
|
|
|
|
final bool needToTurnOn,
|
|
|
|
) async {
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2022-05-14 02:54:40 +00:00
|
|
|
try {
|
|
|
|
client.post(
|
|
|
|
'/services/${type.url}/${needToTurnOn ? 'enable' : 'disable'}',
|
|
|
|
);
|
|
|
|
} on DioError catch (e) {
|
|
|
|
print(e.message);
|
|
|
|
} finally {
|
|
|
|
close(client);
|
|
|
|
}
|
2021-09-02 19:32:07 +00:00
|
|
|
}
|
2021-09-29 13:08:19 +00:00
|
|
|
|
|
|
|
Future<Map<ServiceTypes, bool>> servicesPowerCheck() async {
|
2022-05-14 02:54:40 +00:00
|
|
|
Response response;
|
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2022-05-14 02:54:40 +00:00
|
|
|
try {
|
|
|
|
response = await client.get('/services/status');
|
|
|
|
} on DioError catch (e) {
|
|
|
|
print(e.message);
|
|
|
|
return {};
|
|
|
|
} finally {
|
|
|
|
close(client);
|
|
|
|
}
|
2021-09-29 13:08:19 +00:00
|
|
|
|
|
|
|
return {
|
2022-08-29 20:35:06 +00:00
|
|
|
ServiceTypes.bitwarden: response.data['bitwarden'] == 0,
|
|
|
|
ServiceTypes.gitea: response.data['gitea'] == 0,
|
|
|
|
ServiceTypes.nextcloud: response.data['nextcloud'] == 0,
|
|
|
|
ServiceTypes.ocserv: response.data['ocserv'] == 0,
|
|
|
|
ServiceTypes.pleroma: response.data['pleroma'] == 0,
|
2021-09-29 13:08:19 +00:00
|
|
|
};
|
|
|
|
}
|
2021-12-06 18:31:19 +00:00
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
Future<void> uploadBackblazeConfig(final BackblazeBucket bucket) async {
|
|
|
|
final Dio client = await getClient();
|
2022-05-14 02:54:40 +00:00
|
|
|
try {
|
|
|
|
client.put(
|
|
|
|
'/services/restic/backblaze/config',
|
|
|
|
data: {
|
|
|
|
'accountId': bucket.applicationKeyId,
|
|
|
|
'accountKey': bucket.applicationKey,
|
|
|
|
'bucket': bucket.bucketName,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} on DioError catch (e) {
|
|
|
|
print(e.message);
|
|
|
|
} finally {
|
|
|
|
close(client);
|
|
|
|
}
|
2021-12-06 18:31:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> startBackup() async {
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2022-05-14 02:54:40 +00:00
|
|
|
try {
|
|
|
|
client.put('/services/restic/backup/create');
|
|
|
|
} on DioError catch (e) {
|
|
|
|
print(e.message);
|
|
|
|
} finally {
|
|
|
|
close(client);
|
|
|
|
}
|
2021-12-06 18:31:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<Backup>> getBackups() async {
|
|
|
|
Response response;
|
2022-05-14 02:54:40 +00:00
|
|
|
List<Backup> backups = [];
|
2021-12-06 18:31:19 +00:00
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2021-12-06 18:31:19 +00:00
|
|
|
try {
|
2022-05-14 02:54:40 +00:00
|
|
|
response = await client.get('/services/restic/backup/list');
|
2022-09-05 04:34:47 +00:00
|
|
|
backups =
|
|
|
|
response.data.map<Backup>((final e) => Backup.fromJson(e)).toList();
|
2022-05-14 02:54:40 +00:00
|
|
|
} on DioError catch (e) {
|
|
|
|
print(e.message);
|
2021-12-06 18:31:19 +00:00
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
2022-05-14 02:54:40 +00:00
|
|
|
} finally {
|
|
|
|
close(client);
|
2021-12-06 18:31:19 +00:00
|
|
|
}
|
2022-05-24 18:55:39 +00:00
|
|
|
return backups;
|
2021-12-06 18:31:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<BackupStatus> getBackupStatus() async {
|
|
|
|
Response response;
|
2022-05-14 02:54:40 +00:00
|
|
|
BackupStatus status = BackupStatus(
|
2021-12-06 18:31:19 +00:00
|
|
|
status: BackupStatusEnum.error,
|
|
|
|
errorMessage: 'Network error',
|
|
|
|
progress: 0,
|
|
|
|
);
|
2022-05-14 02:54:40 +00:00
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2022-05-14 02:54:40 +00:00
|
|
|
try {
|
|
|
|
response = await client.get('/services/restic/backup/status');
|
|
|
|
status = BackupStatus.fromJson(response.data);
|
|
|
|
} on DioError catch (e) {
|
|
|
|
print(e.message);
|
|
|
|
} finally {
|
|
|
|
close(client);
|
|
|
|
}
|
2022-05-24 18:55:39 +00:00
|
|
|
return status;
|
2021-12-06 18:31:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> forceBackupListReload() async {
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2022-05-14 02:54:40 +00:00
|
|
|
try {
|
|
|
|
client.get('/services/restic/backup/reload');
|
|
|
|
} on DioError catch (e) {
|
|
|
|
print(e.message);
|
|
|
|
} finally {
|
|
|
|
close(client);
|
|
|
|
}
|
2021-12-06 18:31:19 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
Future<void> restoreBackup(final String backupId) async {
|
|
|
|
final Dio client = await getClient();
|
2022-05-14 02:54:40 +00:00
|
|
|
try {
|
|
|
|
client.put(
|
|
|
|
'/services/restic/backup/restore',
|
|
|
|
data: {'backupId': backupId},
|
|
|
|
);
|
|
|
|
} on DioError catch (e) {
|
|
|
|
print(e.message);
|
|
|
|
} finally {
|
|
|
|
close(client);
|
|
|
|
}
|
2021-12-06 18:31:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> pullConfigurationUpdate() async {
|
2022-05-14 02:54:40 +00:00
|
|
|
Response response;
|
|
|
|
bool result = false;
|
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2022-05-14 02:54:40 +00:00
|
|
|
try {
|
|
|
|
response = await client.get('/system/configuration/pull');
|
2022-09-05 04:34:47 +00:00
|
|
|
result = (response.statusCode != null)
|
|
|
|
? (response.statusCode == HttpStatus.ok)
|
|
|
|
: false;
|
2022-05-14 02:54:40 +00:00
|
|
|
} on DioError catch (e) {
|
|
|
|
print(e.message);
|
|
|
|
} finally {
|
|
|
|
close(client);
|
|
|
|
}
|
2022-05-24 18:55:39 +00:00
|
|
|
return result;
|
2021-12-06 18:31:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> reboot() async {
|
2022-05-14 02:54:40 +00:00
|
|
|
Response response;
|
|
|
|
bool result = false;
|
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2022-05-14 02:54:40 +00:00
|
|
|
try {
|
|
|
|
response = await client.get('/system/reboot');
|
2022-09-05 04:34:47 +00:00
|
|
|
result = (response.statusCode != null)
|
|
|
|
? (response.statusCode == HttpStatus.ok)
|
|
|
|
: false;
|
2022-05-14 02:54:40 +00:00
|
|
|
} on DioError catch (e) {
|
|
|
|
print(e.message);
|
|
|
|
} finally {
|
|
|
|
close(client);
|
|
|
|
}
|
2022-05-24 18:55:39 +00:00
|
|
|
return result;
|
2021-12-06 18:31:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> upgrade() async {
|
2022-05-14 02:54:40 +00:00
|
|
|
Response response;
|
|
|
|
bool result = false;
|
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2022-05-14 02:54:40 +00:00
|
|
|
try {
|
|
|
|
response = await client.get('/system/configuration/upgrade');
|
2022-09-05 04:34:47 +00:00
|
|
|
result = (response.statusCode != null)
|
|
|
|
? (response.statusCode == HttpStatus.ok)
|
|
|
|
: false;
|
2022-05-14 02:54:40 +00:00
|
|
|
} on DioError catch (e) {
|
|
|
|
print(e.message);
|
|
|
|
} finally {
|
|
|
|
close(client);
|
|
|
|
}
|
2022-05-24 18:55:39 +00:00
|
|
|
return result;
|
2021-12-06 18:31:19 +00:00
|
|
|
}
|
2022-02-01 01:56:05 +00:00
|
|
|
|
2022-02-08 21:01:08 +00:00
|
|
|
Future<AutoUpgradeSettings> getAutoUpgradeSettings() async {
|
2022-05-14 02:54:40 +00:00
|
|
|
Response response;
|
2022-05-24 18:55:39 +00:00
|
|
|
AutoUpgradeSettings settings = const AutoUpgradeSettings(
|
2022-05-14 02:54:40 +00:00
|
|
|
enable: false,
|
|
|
|
allowReboot: false,
|
2022-05-10 20:42:33 +00:00
|
|
|
);
|
2022-05-14 02:54:40 +00:00
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2022-05-14 02:54:40 +00:00
|
|
|
try {
|
|
|
|
response = await client.get('/system/configuration/autoUpgrade');
|
|
|
|
if (response.data != null) {
|
|
|
|
settings = AutoUpgradeSettings.fromJson(response.data);
|
|
|
|
}
|
|
|
|
} on DioError catch (e) {
|
|
|
|
print(e.message);
|
|
|
|
} finally {
|
|
|
|
close(client);
|
|
|
|
}
|
2022-05-24 18:55:39 +00:00
|
|
|
return settings;
|
2022-02-08 21:01:08 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 22:40:34 +00:00
|
|
|
Future<void> updateAutoUpgradeSettings(
|
|
|
|
final AutoUpgradeSettings settings,
|
|
|
|
) async {
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2022-05-14 02:54:40 +00:00
|
|
|
try {
|
|
|
|
await client.put(
|
|
|
|
'/system/configuration/autoUpgrade',
|
|
|
|
data: settings.toJson(),
|
|
|
|
);
|
|
|
|
} on DioError catch (e) {
|
|
|
|
print(e.message);
|
|
|
|
} finally {
|
|
|
|
close(client);
|
|
|
|
}
|
2022-02-08 21:01:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<TimeZoneSettings> getServerTimezone() async {
|
2022-05-14 02:54:40 +00:00
|
|
|
// I am not sure how to initialize TimeZoneSettings with default value...
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2022-09-05 04:34:47 +00:00
|
|
|
final Response response =
|
|
|
|
await client.get('/system/configuration/timezone');
|
2022-03-03 17:38:30 +00:00
|
|
|
close(client);
|
2022-02-08 21:01:08 +00:00
|
|
|
|
|
|
|
return TimeZoneSettings.fromString(response.data);
|
|
|
|
}
|
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
Future<void> updateServerTimezone(final TimeZoneSettings settings) async {
|
|
|
|
final Dio client = await getClient();
|
2022-05-14 02:54:40 +00:00
|
|
|
try {
|
|
|
|
await client.put(
|
|
|
|
'/system/configuration/timezone',
|
|
|
|
data: settings.toString(),
|
|
|
|
);
|
|
|
|
} on DioError catch (e) {
|
|
|
|
print(e.message);
|
|
|
|
} finally {
|
|
|
|
close(client);
|
|
|
|
}
|
2022-02-07 07:53:13 +00:00
|
|
|
}
|
2022-02-17 23:37:15 +00:00
|
|
|
|
2022-08-29 20:21:59 +00:00
|
|
|
Future<String?> getDkim() async {
|
2022-05-14 02:54:40 +00:00
|
|
|
Response response;
|
2022-08-29 20:21:59 +00:00
|
|
|
String? dkim;
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2022-05-14 02:54:40 +00:00
|
|
|
try {
|
|
|
|
response = await client.get('/services/mailserver/dkim');
|
2022-08-29 20:21:59 +00:00
|
|
|
final Codec<String, String> base64toString = utf8.fuse(base64);
|
2022-09-05 04:34:47 +00:00
|
|
|
dkim = base64toString
|
|
|
|
.decode(response.data)
|
|
|
|
.split('(')[1]
|
|
|
|
.split(')')[0]
|
|
|
|
.replaceAll('"', '');
|
2022-05-14 02:54:40 +00:00
|
|
|
} on DioError catch (e) {
|
|
|
|
print(e.message);
|
|
|
|
} finally {
|
|
|
|
close(client);
|
|
|
|
}
|
2022-02-01 01:56:05 +00:00
|
|
|
|
2022-08-29 20:21:59 +00:00
|
|
|
return dkim;
|
2022-02-01 01:56:05 +00:00
|
|
|
}
|
2022-05-09 23:16:36 +00:00
|
|
|
|
2022-05-24 17:45:13 +00:00
|
|
|
Future<ApiResponse<RecoveryKeyStatus?>> getRecoveryTokenStatus() async {
|
2022-05-14 02:54:40 +00:00
|
|
|
Response response;
|
2022-05-09 23:16:36 +00:00
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2022-05-14 02:54:40 +00:00
|
|
|
try {
|
|
|
|
response = await client.get('/auth/recovery_token');
|
|
|
|
} on DioError catch (e) {
|
|
|
|
print(e.message);
|
2022-05-09 23:16:36 +00:00
|
|
|
return ApiResponse(
|
2022-06-05 22:40:34 +00:00
|
|
|
errorMessage: e.message,
|
|
|
|
statusCode: e.response?.statusCode ?? HttpStatus.internalServerError,
|
|
|
|
data: const RecoveryKeyStatus(exists: false, valid: false),
|
|
|
|
);
|
2022-05-14 02:54:40 +00:00
|
|
|
} finally {
|
|
|
|
close(client);
|
2022-05-09 23:16:36 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 02:54:40 +00:00
|
|
|
final int code = response.statusCode ?? HttpStatus.internalServerError;
|
|
|
|
|
2022-05-09 23:16:36 +00:00
|
|
|
return ApiResponse(
|
2022-06-05 22:40:34 +00:00
|
|
|
statusCode: code,
|
2022-09-05 04:34:47 +00:00
|
|
|
data: response.data != null
|
|
|
|
? RecoveryKeyStatus.fromJson(response.data)
|
|
|
|
: null,
|
2022-06-05 22:40:34 +00:00
|
|
|
);
|
2022-05-09 23:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<ApiResponse<String>> generateRecoveryToken(
|
2022-06-05 19:36:32 +00:00
|
|
|
final DateTime? expiration,
|
|
|
|
final int? uses,
|
2022-05-24 16:09:47 +00:00
|
|
|
) async {
|
2022-05-14 02:54:40 +00:00
|
|
|
Response response;
|
2022-05-09 23:16:36 +00:00
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
|
|
|
final Map data = {};
|
2022-05-24 16:09:47 +00:00
|
|
|
if (expiration != null) {
|
2022-05-30 23:06:08 +00:00
|
|
|
data['expiration'] = '${expiration.toIso8601String()}Z';
|
|
|
|
print(data['expiration']);
|
2022-05-24 16:09:47 +00:00
|
|
|
}
|
|
|
|
if (uses != null) {
|
|
|
|
data['uses'] = uses;
|
|
|
|
}
|
2022-05-14 02:54:40 +00:00
|
|
|
try {
|
|
|
|
response = await client.post(
|
|
|
|
'/auth/recovery_token',
|
2022-05-24 16:09:47 +00:00
|
|
|
data: data,
|
2022-05-14 02:54:40 +00:00
|
|
|
);
|
|
|
|
} on DioError catch (e) {
|
|
|
|
print(e.message);
|
2022-05-09 23:16:36 +00:00
|
|
|
return ApiResponse(
|
2022-06-05 22:40:34 +00:00
|
|
|
errorMessage: e.message,
|
|
|
|
statusCode: e.response?.statusCode ?? HttpStatus.internalServerError,
|
|
|
|
data: '',
|
|
|
|
);
|
2022-05-14 02:54:40 +00:00
|
|
|
} finally {
|
|
|
|
close(client);
|
2022-05-09 23:16:36 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 02:54:40 +00:00
|
|
|
final int code = response.statusCode ?? HttpStatus.internalServerError;
|
|
|
|
|
|
|
|
return ApiResponse(
|
2022-06-05 22:40:34 +00:00
|
|
|
statusCode: code,
|
|
|
|
data: response.data != null ? response.data['token'] : '',
|
|
|
|
);
|
2022-05-09 23:16:36 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
Future<ApiResponse<String>> useRecoveryToken(final DeviceToken token) async {
|
2022-05-14 02:54:40 +00:00
|
|
|
Response response;
|
2022-05-09 23:16:36 +00:00
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2022-05-14 02:54:40 +00:00
|
|
|
try {
|
|
|
|
response = await client.post(
|
|
|
|
'/auth/recovery_token/use',
|
|
|
|
data: {
|
|
|
|
'token': token.token,
|
|
|
|
'device': token.device,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} on DioError catch (e) {
|
|
|
|
print(e.message);
|
2022-05-09 23:16:36 +00:00
|
|
|
return ApiResponse(
|
2022-06-05 22:40:34 +00:00
|
|
|
errorMessage: e.message,
|
|
|
|
statusCode: e.response?.statusCode ?? HttpStatus.internalServerError,
|
|
|
|
data: '',
|
|
|
|
);
|
2022-05-14 02:54:40 +00:00
|
|
|
} finally {
|
|
|
|
client.close();
|
2022-05-09 23:16:36 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 02:54:40 +00:00
|
|
|
final int code = response.statusCode ?? HttpStatus.internalServerError;
|
|
|
|
|
|
|
|
return ApiResponse(
|
2022-06-05 22:40:34 +00:00
|
|
|
statusCode: code,
|
|
|
|
data: response.data != null ? response.data['token'] : '',
|
|
|
|
);
|
2022-05-09 23:16:36 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
Future<ApiResponse<String>> authorizeDevice(final DeviceToken token) async {
|
2022-05-14 02:54:40 +00:00
|
|
|
Response response;
|
2022-05-09 23:16:36 +00:00
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2022-05-14 02:54:40 +00:00
|
|
|
try {
|
|
|
|
response = await client.post(
|
|
|
|
'/auth/new_device/authorize',
|
|
|
|
data: {
|
|
|
|
'token': token.token,
|
|
|
|
'device': token.device,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} on DioError catch (e) {
|
|
|
|
print(e.message);
|
2022-05-09 23:16:36 +00:00
|
|
|
return ApiResponse(
|
2022-06-05 22:40:34 +00:00
|
|
|
errorMessage: e.message,
|
|
|
|
statusCode: e.response?.statusCode ?? HttpStatus.internalServerError,
|
|
|
|
data: '',
|
|
|
|
);
|
2022-05-14 02:54:40 +00:00
|
|
|
} finally {
|
|
|
|
client.close();
|
2022-05-09 23:16:36 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 02:54:40 +00:00
|
|
|
final int code = response.statusCode ?? HttpStatus.internalServerError;
|
|
|
|
|
2022-05-24 18:55:39 +00:00
|
|
|
return ApiResponse(statusCode: code, data: response.data['token'] ?? '');
|
2022-05-09 23:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<ApiResponse<String>> createDeviceToken() async {
|
2022-05-14 02:54:40 +00:00
|
|
|
Response response;
|
2022-05-09 23:16:36 +00:00
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2022-05-14 02:54:40 +00:00
|
|
|
try {
|
|
|
|
response = await client.post('/auth/new_device');
|
|
|
|
} on DioError catch (e) {
|
|
|
|
print(e.message);
|
2022-05-09 23:16:36 +00:00
|
|
|
return ApiResponse(
|
2022-06-05 22:40:34 +00:00
|
|
|
errorMessage: e.message,
|
|
|
|
statusCode: e.response?.statusCode ?? HttpStatus.internalServerError,
|
|
|
|
data: '',
|
|
|
|
);
|
2022-05-14 02:54:40 +00:00
|
|
|
} finally {
|
|
|
|
client.close();
|
2022-05-09 23:16:36 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 02:54:40 +00:00
|
|
|
final int code = response.statusCode ?? HttpStatus.internalServerError;
|
|
|
|
|
|
|
|
return ApiResponse(
|
2022-06-05 22:40:34 +00:00
|
|
|
statusCode: code,
|
|
|
|
data: response.data != null ? response.data['token'] : '',
|
|
|
|
);
|
2022-05-09 23:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<ApiResponse<String>> deleteDeviceToken() async {
|
2022-05-14 02:54:40 +00:00
|
|
|
Response response;
|
2022-05-09 23:16:36 +00:00
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2022-05-14 02:54:40 +00:00
|
|
|
try {
|
|
|
|
response = await client.delete('/auth/new_device');
|
|
|
|
} on DioError catch (e) {
|
|
|
|
print(e.message);
|
2022-05-09 23:16:36 +00:00
|
|
|
return ApiResponse(
|
2022-06-05 22:40:34 +00:00
|
|
|
errorMessage: e.message,
|
|
|
|
statusCode: e.response?.statusCode ?? HttpStatus.internalServerError,
|
|
|
|
data: '',
|
|
|
|
);
|
2022-05-14 02:54:40 +00:00
|
|
|
} finally {
|
|
|
|
client.close();
|
2022-05-09 23:16:36 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 02:54:40 +00:00
|
|
|
final int code = response.statusCode ?? HttpStatus.internalServerError;
|
|
|
|
|
2022-05-24 18:55:39 +00:00
|
|
|
return ApiResponse(statusCode: code, data: response.data ?? '');
|
2022-05-09 23:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<ApiResponse<List<ApiToken>>> getApiTokens() async {
|
2022-05-14 02:54:40 +00:00
|
|
|
Response response;
|
2022-05-09 23:16:36 +00:00
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2022-05-14 02:54:40 +00:00
|
|
|
try {
|
|
|
|
response = await client.get('/auth/tokens');
|
|
|
|
} on DioError catch (e) {
|
|
|
|
print(e.message);
|
2022-05-09 23:16:36 +00:00
|
|
|
return ApiResponse(
|
2022-06-05 22:40:34 +00:00
|
|
|
errorMessage: e.message,
|
|
|
|
statusCode: e.response?.statusCode ?? HttpStatus.internalServerError,
|
|
|
|
data: [],
|
|
|
|
);
|
2022-05-14 02:54:40 +00:00
|
|
|
} finally {
|
|
|
|
client.close();
|
2022-05-09 23:16:36 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 02:54:40 +00:00
|
|
|
final int code = response.statusCode ?? HttpStatus.internalServerError;
|
|
|
|
|
|
|
|
return ApiResponse(
|
2022-06-05 22:40:34 +00:00
|
|
|
statusCode: code,
|
2022-09-05 04:34:47 +00:00
|
|
|
data: (response.data != null)
|
|
|
|
? response.data
|
|
|
|
.map<ApiToken>((final e) => ApiToken.fromJson(e))
|
|
|
|
.toList()
|
|
|
|
: [],
|
2022-06-05 22:40:34 +00:00
|
|
|
);
|
2022-05-09 23:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<ApiResponse<String>> refreshCurrentApiToken() async {
|
2022-05-14 02:54:40 +00:00
|
|
|
Response response;
|
2022-05-09 23:16:36 +00:00
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2022-05-14 02:54:40 +00:00
|
|
|
try {
|
|
|
|
response = await client.post('/auth/tokens');
|
|
|
|
} on DioError catch (e) {
|
|
|
|
print(e.message);
|
2022-05-09 23:16:36 +00:00
|
|
|
return ApiResponse(
|
2022-06-05 22:40:34 +00:00
|
|
|
errorMessage: e.message,
|
|
|
|
statusCode: e.response?.statusCode ?? HttpStatus.internalServerError,
|
|
|
|
data: '',
|
|
|
|
);
|
2022-05-14 02:54:40 +00:00
|
|
|
} finally {
|
|
|
|
client.close();
|
2022-05-09 23:16:36 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 02:54:40 +00:00
|
|
|
final int code = response.statusCode ?? HttpStatus.internalServerError;
|
|
|
|
|
|
|
|
return ApiResponse(
|
2022-06-05 22:40:34 +00:00
|
|
|
statusCode: code,
|
|
|
|
data: response.data != null ? response.data['token'] : '',
|
|
|
|
);
|
2022-05-09 23:16:36 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
Future<ApiResponse<void>> deleteApiToken(final String device) async {
|
2022-05-14 02:54:40 +00:00
|
|
|
Response response;
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2022-05-14 02:54:40 +00:00
|
|
|
try {
|
|
|
|
response = await client.delete(
|
|
|
|
'/auth/tokens',
|
|
|
|
data: {
|
2022-05-31 14:30:44 +00:00
|
|
|
'token_name': device,
|
2022-05-14 02:54:40 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
} on DioError catch (e) {
|
|
|
|
print(e.message);
|
|
|
|
return ApiResponse(
|
2022-06-05 22:40:34 +00:00
|
|
|
errorMessage: e.message,
|
|
|
|
statusCode: e.response?.statusCode ?? HttpStatus.internalServerError,
|
|
|
|
data: null,
|
|
|
|
);
|
2022-05-14 02:54:40 +00:00
|
|
|
} finally {
|
|
|
|
client.close();
|
2022-05-09 23:16:36 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 02:54:40 +00:00
|
|
|
final int code = response.statusCode ?? HttpStatus.internalServerError;
|
|
|
|
return ApiResponse(statusCode: code, data: null);
|
2022-05-09 23:16:36 +00:00
|
|
|
}
|
2021-08-29 15:02:51 +00:00
|
|
|
}
|
2021-08-29 13:54:28 +00:00
|
|
|
|
|
|
|
extension UrlServerExt on ServiceTypes {
|
|
|
|
String get url {
|
|
|
|
switch (this) {
|
|
|
|
// case ServiceTypes.mail:
|
2022-02-16 07:28:29 +00:00
|
|
|
// return ''; // cannot be switch off
|
2021-08-29 13:54:28 +00:00
|
|
|
// case ServiceTypes.messenger:
|
|
|
|
// return ''; // external service
|
|
|
|
// case ServiceTypes.video:
|
2022-02-16 07:28:29 +00:00
|
|
|
// return ''; // jitsi meet not working
|
2022-08-29 20:35:06 +00:00
|
|
|
case ServiceTypes.bitwarden:
|
2021-08-29 13:54:28 +00:00
|
|
|
return 'bitwarden';
|
2022-08-29 20:35:06 +00:00
|
|
|
case ServiceTypes.nextcloud:
|
2021-08-29 13:54:28 +00:00
|
|
|
return 'nextcloud';
|
2022-08-29 20:35:06 +00:00
|
|
|
case ServiceTypes.pleroma:
|
2021-08-29 13:54:28 +00:00
|
|
|
return 'pleroma';
|
2022-08-29 20:35:06 +00:00
|
|
|
case ServiceTypes.gitea:
|
2021-08-29 13:54:28 +00:00
|
|
|
return 'gitea';
|
2022-08-29 20:35:06 +00:00
|
|
|
case ServiceTypes.ocserv:
|
2021-08-29 13:54:28 +00:00
|
|
|
return 'ocserv';
|
|
|
|
default:
|
|
|
|
throw Exception('wrong state');
|
|
|
|
}
|
|
|
|
}
|
2021-08-29 15:02:51 +00:00
|
|
|
}
|