selfprivacy.org.app/lib/logic/api_maps/rest_maps/server.dart

899 lines
23 KiB
Dart
Raw Normal View History

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';
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';
import 'package:selfprivacy/logic/models/hive/user.dart';
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';
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
class ApiResponse<D> {
ApiResponse({
required this.statusCode,
required this.data,
2022-06-05 19:36:32 +00:00
this.errorMessage,
});
2022-06-05 19:36:32 +00:00
final int statusCode;
final String? errorMessage;
final D data;
bool get isSuccess => statusCode >= 200 && statusCode < 300;
}
2021-03-25 23:30:34 +00:00
class ServerApi extends ApiMap {
ServerApi({
this.hasLogger = false,
this.isWithToken = true,
this.overrideDomain,
this.customToken,
});
@override
2022-02-16 07:28:29 +00:00
bool hasLogger;
@override
2021-03-25 23:30:34 +00:00
bool isWithToken;
String? overrideDomain;
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 {
BaseOptions options = BaseOptions(
connectTimeout: 10000,
receiveTimeout: 10000,
);
2021-03-25 23:30:34 +00:00
if (isWithToken) {
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
options = BaseOptions(
baseUrl: 'https://api.$domainName',
connectTimeout: 10000,
receiveTimeout: 10000,
headers: {
'Authorization': 'Bearer $apiToken',
},
);
2021-03-25 23:30:34 +00:00
}
if (overrideDomain != null) {
options = BaseOptions(
baseUrl: 'https://api.$overrideDomain',
connectTimeout: 10000,
receiveTimeout: 10000,
2022-09-04 11:29:05 +00:00
headers: customToken != null ? {'Authorization': 'Bearer $customToken'} : null,
);
}
2021-03-25 23:30:34 +00:00
return options;
2021-01-21 21:01:42 +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;
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;
}
2021-01-21 21:01:42 +00:00
Future<bool> isHttpServerWorking() async {
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 {
response = await client.get('/services/status');
2021-01-21 21:01:42 +00:00
res = response.statusCode == HttpStatus.ok;
} catch (e) {
print(e);
} 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 {
Response response;
2022-06-05 19:36:32 +00:00
final Dio client = await getClient();
try {
response = await client.post(
'/users',
data: {
'username': user.login,
'password': user.password,
},
);
} on DioError catch (e) {
print(e.message);
return ApiResponse(
errorMessage: e.error.toString(),
statusCode: e.response?.statusCode ?? HttpStatus.internalServerError,
data: User(
login: user.login,
2022-09-04 11:29:05 +00:00
type: UserType.normal,
password: user.password,
isFoundOnServer: false,
2021-06-20 21:08:52 +00:00
),
);
} finally {
close(client);
}
bool isFoundOnServer = false;
int code = 0;
2022-09-04 11:29:05 +00:00
final bool isUserCreated = (response.statusCode != null) && (response.statusCode == HttpStatus.created);
if (isUserCreated) {
isFoundOnServer = true;
code = response.statusCode!;
} else {
isFoundOnServer = false;
code = HttpStatus.notAcceptable;
}
return ApiResponse(
statusCode: code,
data: User(
login: user.login,
2022-09-04 11:29:05 +00:00
type: UserType.normal,
password: user.password,
isFoundOnServer: isFoundOnServer,
),
);
}
Future<ApiResponse<List<String>>> getUsersList({
final withMainUser = false,
}) async {
2022-06-05 19:36:32 +00:00
final List<String> res = [];
Response response;
2022-06-05 19:36:32 +00:00
final Dio client = await getClient();
try {
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());
}
} on DioError catch (e) {
print(e.message);
return ApiResponse(
errorMessage: e.message,
statusCode: e.response?.statusCode ?? HttpStatus.internalServerError,
data: [],
);
} catch (e) {
2022-03-23 14:07:52 +00:00
print(e);
return ApiResponse(
errorMessage: e.toString(),
statusCode: HttpStatus.internalServerError,
data: [],
);
} finally {
close(client);
}
final int code = response.statusCode ?? HttpStatus.internalServerError;
return ApiResponse(
statusCode: code,
data: res,
);
}
Future<ApiResponse<void>> addUserSshKey(
final User user,
final String sshKey,
) async {
late Response response;
2022-06-05 19:36:32 +00:00
final Dio client = await getClient();
try {
response = await client.post(
'/services/ssh/keys/${user.login}',
data: {
'public_key': sshKey,
},
);
} 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;
return ApiResponse<void>(
statusCode: code,
data: null,
);
}
2022-06-05 19:36:32 +00:00
Future<ApiResponse<void>> addRootSshKey(final String ssh) async {
late Response response;
2022-06-05 19:36:32 +00:00
final Dio client = await getClient();
try {
response = await client.put(
'/services/ssh/key/send',
2022-05-24 18:55:39 +00:00
data: {'public_key': ssh},
);
} 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;
return ApiResponse<void>(
statusCode: code,
data: null,
);
}
2022-06-05 19:36:32 +00:00
Future<ApiResponse<List<String>>> getUserSshKeys(final User user) async {
List<String> res;
Response response;
2022-06-05 19:36:32 +00:00
final Dio client = await getClient();
try {
response = await client.get('/services/ssh/keys/${user.login}');
2022-09-04 11:29:05 +00:00
res = (response.data as List<dynamic>).map((final e) => e as String).toList();
} 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) {
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
final int code = response.statusCode ?? HttpStatus.internalServerError;
return ApiResponse<List<String>>(
statusCode: code,
data: res,
errorMessage: response.data is List
? null
: response.data?.containsKey('error') ?? false
? response.data['error']
: null,
);
}
Future<ApiResponse<void>> deleteUserSshKey(
final User user,
final String sshKey,
) async {
Response response;
2022-06-05 19:36:32 +00:00
final Dio client = await getClient();
try {
response = await client.delete(
'/services/ssh/keys/${user.login}',
2022-05-24 18:55:39 +00:00
data: {'public_key': sshKey},
);
} 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;
return ApiResponse<void>(
statusCode: code,
data: null,
2022-09-04 11:29:05 +00:00
errorMessage: response.data?.containsKey('error') ?? false ? response.data['error'] : null,
);
2021-06-08 18:52:44 +00:00
}
2022-06-05 19:36:32 +00:00
Future<bool> deleteUser(final User user) async {
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 {
response = await client.delete('/users/${user.login}');
2022-09-04 11:29:05 +00:00
res = response.statusCode == HttpStatus.ok || response.statusCode == HttpStatus.notFound;
} on DioError catch (e) {
print(e.message);
2021-12-20 15:25:31 +00:00
res = false;
} 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-04 11:29:05 +00:00
String get rootAddress => throw UnimplementedError('not used in with implementation');
2021-07-29 05:24:42 +00:00
Future<bool> apply() async {
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 {
response = await client.get('/system/configuration/apply');
2021-07-29 05:24:42 +00:00
res = response.statusCode == HttpStatus.ok;
} on DioError catch (e) {
print(e.message);
2021-07-29 05:24:42 +00:00
res = false;
} 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
Future<void> switchService(
final ServiceTypes type,
final bool needToTurnOn,
) async {
2022-06-05 19:36:32 +00:00
final Dio client = await getClient();
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
}
Future<Map<ServiceTypes, bool>> servicesPowerCheck() async {
Response response;
2022-06-05 19:36:32 +00:00
final Dio client = await getClient();
try {
response = await client.get('/services/status');
} on DioError catch (e) {
print(e.message);
return {};
} finally {
close(client);
}
return {
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-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();
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();
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;
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 {
response = await client.get('/services/restic/backup/list');
2022-09-04 11:29:05 +00:00
backups = response.data.map<Backup>((final e) => Backup.fromJson(e)).toList();
} on DioError catch (e) {
print(e.message);
2021-12-06 18:31:19 +00:00
} catch (e) {
print(e);
} 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;
BackupStatus status = BackupStatus(
2021-12-06 18:31:19 +00:00
status: BackupStatusEnum.error,
errorMessage: 'Network error',
progress: 0,
);
2022-06-05 19:36:32 +00:00
final Dio client = await getClient();
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();
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();
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 {
Response response;
bool result = false;
2022-06-05 19:36:32 +00:00
final Dio client = await getClient();
try {
response = await client.get('/system/configuration/pull');
2022-09-04 11:29:05 +00:00
result = (response.statusCode != null) ? (response.statusCode == HttpStatus.ok) : false;
} 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 {
Response response;
bool result = false;
2022-06-05 19:36:32 +00:00
final Dio client = await getClient();
try {
response = await client.get('/system/reboot');
2022-09-04 11:29:05 +00:00
result = (response.statusCode != null) ? (response.statusCode == HttpStatus.ok) : false;
} 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 {
Response response;
bool result = false;
2022-06-05 19:36:32 +00:00
final Dio client = await getClient();
try {
response = await client.get('/system/configuration/upgrade');
2022-09-04 11:29:05 +00:00
result = (response.statusCode != null) ? (response.statusCode == HttpStatus.ok) : false;
} 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 {
Response response;
2022-05-24 18:55:39 +00:00
AutoUpgradeSettings settings = const AutoUpgradeSettings(
enable: false,
allowReboot: false,
);
2022-06-05 19:36:32 +00:00
final Dio client = await getClient();
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
}
Future<void> updateAutoUpgradeSettings(
final AutoUpgradeSettings settings,
) async {
2022-06-05 19:36:32 +00:00
final Dio client = await getClient();
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 {
// 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-04 11:29:05 +00:00
final Response response = await client.get('/system/configuration/timezone');
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();
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-08-29 20:21:59 +00:00
Future<String?> getDkim() async {
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();
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-04 11:29:05 +00:00
dkim = base64toString.decode(response.data).split('(')[1].split(')')[0].replaceAll('"', '');
} 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
}
Future<ApiResponse<RecoveryKeyStatus?>> getRecoveryTokenStatus() async {
Response response;
2022-06-05 19:36:32 +00:00
final Dio client = await getClient();
try {
response = await client.get('/auth/recovery_token');
} on DioError catch (e) {
print(e.message);
return ApiResponse(
errorMessage: e.message,
statusCode: e.response?.statusCode ?? HttpStatus.internalServerError,
data: const RecoveryKeyStatus(exists: false, valid: false),
);
} finally {
close(client);
}
final int code = response.statusCode ?? HttpStatus.internalServerError;
return ApiResponse(
statusCode: code,
2022-09-04 11:29:05 +00:00
data: response.data != null ? RecoveryKeyStatus.fromJson(response.data) : null,
);
}
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 {
Response response;
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;
}
try {
response = await client.post(
'/auth/recovery_token',
2022-05-24 16:09:47 +00:00
data: data,
);
} on DioError catch (e) {
print(e.message);
return ApiResponse(
errorMessage: e.message,
statusCode: e.response?.statusCode ?? HttpStatus.internalServerError,
data: '',
);
} finally {
close(client);
}
final int code = response.statusCode ?? HttpStatus.internalServerError;
return ApiResponse(
statusCode: code,
data: response.data != null ? response.data['token'] : '',
);
}
2022-06-05 19:36:32 +00:00
Future<ApiResponse<String>> useRecoveryToken(final DeviceToken token) async {
Response response;
2022-06-05 19:36:32 +00:00
final Dio client = await getClient();
try {
response = await client.post(
'/auth/recovery_token/use',
data: {
'token': token.token,
'device': token.device,
},
);
} on DioError catch (e) {
print(e.message);
return ApiResponse(
errorMessage: e.message,
statusCode: e.response?.statusCode ?? HttpStatus.internalServerError,
data: '',
);
} finally {
client.close();
}
final int code = response.statusCode ?? HttpStatus.internalServerError;
return ApiResponse(
statusCode: code,
data: response.data != null ? response.data['token'] : '',
);
}
2022-06-05 19:36:32 +00:00
Future<ApiResponse<String>> authorizeDevice(final DeviceToken token) async {
Response response;
2022-06-05 19:36:32 +00:00
final Dio client = await getClient();
try {
response = await client.post(
'/auth/new_device/authorize',
data: {
'token': token.token,
'device': token.device,
},
);
} on DioError catch (e) {
print(e.message);
return ApiResponse(
errorMessage: e.message,
statusCode: e.response?.statusCode ?? HttpStatus.internalServerError,
data: '',
);
} finally {
client.close();
}
final int code = response.statusCode ?? HttpStatus.internalServerError;
2022-05-24 18:55:39 +00:00
return ApiResponse(statusCode: code, data: response.data['token'] ?? '');
}
Future<ApiResponse<String>> createDeviceToken() async {
Response response;
2022-06-05 19:36:32 +00:00
final Dio client = await getClient();
try {
response = await client.post('/auth/new_device');
} on DioError catch (e) {
print(e.message);
return ApiResponse(
errorMessage: e.message,
statusCode: e.response?.statusCode ?? HttpStatus.internalServerError,
data: '',
);
} finally {
client.close();
}
final int code = response.statusCode ?? HttpStatus.internalServerError;
return ApiResponse(
statusCode: code,
data: response.data != null ? response.data['token'] : '',
);
}
Future<ApiResponse<String>> deleteDeviceToken() async {
Response response;
2022-06-05 19:36:32 +00:00
final Dio client = await getClient();
try {
response = await client.delete('/auth/new_device');
} on DioError catch (e) {
print(e.message);
return ApiResponse(
errorMessage: e.message,
statusCode: e.response?.statusCode ?? HttpStatus.internalServerError,
data: '',
);
} finally {
client.close();
}
final int code = response.statusCode ?? HttpStatus.internalServerError;
2022-05-24 18:55:39 +00:00
return ApiResponse(statusCode: code, data: response.data ?? '');
}
Future<ApiResponse<List<ApiToken>>> getApiTokens() async {
Response response;
2022-06-05 19:36:32 +00:00
final Dio client = await getClient();
try {
response = await client.get('/auth/tokens');
} on DioError catch (e) {
print(e.message);
return ApiResponse(
errorMessage: e.message,
statusCode: e.response?.statusCode ?? HttpStatus.internalServerError,
data: [],
);
} finally {
client.close();
}
final int code = response.statusCode ?? HttpStatus.internalServerError;
return ApiResponse(
statusCode: code,
2022-09-04 11:29:05 +00:00
data: (response.data != null) ? response.data.map<ApiToken>((final e) => ApiToken.fromJson(e)).toList() : [],
);
}
Future<ApiResponse<String>> refreshCurrentApiToken() async {
Response response;
2022-06-05 19:36:32 +00:00
final Dio client = await getClient();
try {
response = await client.post('/auth/tokens');
} on DioError catch (e) {
print(e.message);
return ApiResponse(
errorMessage: e.message,
statusCode: e.response?.statusCode ?? HttpStatus.internalServerError,
data: '',
);
} finally {
client.close();
}
final int code = response.statusCode ?? HttpStatus.internalServerError;
return ApiResponse(
statusCode: code,
data: response.data != null ? response.data['token'] : '',
);
}
2022-06-05 19:36:32 +00:00
Future<ApiResponse<void>> deleteApiToken(final String device) async {
Response response;
2022-06-05 19:36:32 +00:00
final Dio client = await getClient();
try {
response = await client.delete(
'/auth/tokens',
data: {
2022-05-31 14:30:44 +00:00
'token_name': device,
},
);
} on DioError catch (e) {
print(e.message);
return ApiResponse(
errorMessage: e.message,
statusCode: e.response?.statusCode ?? HttpStatus.internalServerError,
data: null,
);
} finally {
client.close();
}
final int code = response.statusCode ?? HttpStatus.internalServerError;
return ApiResponse(statusCode: code, data: null);
}
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
case ServiceTypes.bitwarden:
2021-08-29 13:54:28 +00:00
return 'bitwarden';
case ServiceTypes.nextcloud:
2021-08-29 13:54:28 +00:00
return 'nextcloud';
case ServiceTypes.pleroma:
2021-08-29 13:54:28 +00:00
return 'pleroma';
case ServiceTypes.gitea:
2021-08-29 13:54:28 +00:00
return 'gitea';
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
}