refactor(server-api): Generalize and unify api response objects

- Separate response success from  business logic operation success
- Remove ApiResponse, replace with GenericResult
- Make GenericMutationResult inherit GenericResult
- Add generic error message for when a response couldn't be sent or received
pull/145/head
NaiJi ✨ 2022-10-28 12:17:08 +04:00
parent 25362665a4
commit 68811efc1e
13 changed files with 182 additions and 163 deletions

View File

@ -419,7 +419,8 @@
"create_ssh_key": "Create SSH key for {}",
"delete_ssh_key": "Delete SSH key for {}",
"server_jobs": "Jobs on the server",
"reset_user_password": "Reset password of user"
"reset_user_password": "Reset password of user",
"generic_error": "Couldn't connect to the server!"
},
"validations": {
"required": "Required",

View File

@ -420,7 +420,8 @@
"create_ssh_key": "Создать SSH ключ для {}",
"delete_ssh_key": "Удалить SSH ключ для {}",
"server_jobs": "Задачи на сервере",
"reset_user_password": "Сбросить пароль пользователя"
"reset_user_password": "Сбросить пароль пользователя",
"generic_error": "Не удалось подключиться к серверу!"
},
"validations": {
"required": "Обязательное поле",

View File

@ -22,20 +22,22 @@ mixin JobsApi on ApiMap {
return jobsList;
}
Future<GenericMutationResult> removeApiJob(final String uid) async {
Future<GenericMutationResult<bool>> removeApiJob(final String uid) async {
try {
final GraphQLClient client = await getClient();
final variables = Variables$Mutation$RemoveJob(jobId: uid);
final mutation = Options$Mutation$RemoveJob(variables: variables);
final response = await client.mutate$RemoveJob(mutation);
return GenericMutationResult(
success: response.parsedData?.removeJob.success ?? false,
data: response.parsedData?.removeJob.success ?? false,
success: true,
code: response.parsedData?.removeJob.code ?? 0,
message: response.parsedData?.removeJob.message,
);
} catch (e) {
print(e);
return GenericMutationResult(
data: false,
success: false,
code: 0,
message: e.toString(),

View File

@ -26,39 +26,28 @@ part 'services_api.dart';
part 'users_api.dart';
part 'volume_api.dart';
class GenericMutationResult {
GenericMutationResult({
required this.success,
required this.code,
this.message,
});
final bool success;
final int code;
final String? message;
}
class GenericJobMutationReturn extends GenericMutationResult {
GenericJobMutationReturn({
required super.success,
required super.code,
super.message,
this.job,
});
final ServerJob? job;
}
@Deprecated(
'Extend GenericMutationResult for mutations, return data for queries',
)
class ApiResponse<D> {
ApiResponse({
class GenericResult<T> {
GenericResult({
required this.success,
required this.data,
this.message,
});
/// Whether was a response successfully received,
/// doesn't represent success of the request if `data<T>` is `bool`
final bool success;
final String? message;
final D data;
final T data;
}
class GenericMutationResult<T> extends GenericResult<T> {
GenericMutationResult({
required super.success,
required this.code,
required super.data,
super.message,
});
final int code;
}
class ServerApi extends ApiMap
@ -196,7 +185,7 @@ class ServerApi extends ApiMap
return settings;
}
Future<ApiResponse<RecoveryKeyStatus?>> getRecoveryTokenStatus() async {
Future<GenericResult<RecoveryKeyStatus?>> getRecoveryTokenStatus() async {
RecoveryKeyStatus? key;
QueryResult<Query$RecoveryKey> response;
String? error;
@ -213,18 +202,18 @@ class ServerApi extends ApiMap
print(e);
}
return ApiResponse<RecoveryKeyStatus?>(
return GenericResult<RecoveryKeyStatus?>(
success: error == null,
data: key,
message: error,
);
}
Future<ApiResponse<String>> generateRecoveryToken(
Future<GenericResult<String>> generateRecoveryToken(
final DateTime? expirationDate,
final int? numberOfUses,
) async {
ApiResponse<String> key;
GenericResult<String> key;
QueryResult<Mutation$GetNewRecoveryApiKey> response;
try {
@ -245,19 +234,19 @@ class ServerApi extends ApiMap
);
if (response.hasException) {
print(response.exception.toString());
key = ApiResponse<String>(
key = GenericResult<String>(
success: false,
data: '',
message: response.exception.toString(),
);
}
key = ApiResponse<String>(
key = GenericResult<String>(
success: true,
data: response.parsedData!.getNewRecoveryApiKey.key!,
);
} catch (e) {
print(e);
key = ApiResponse<String>(
key = GenericResult<String>(
success: false,
data: '',
message: e.toString(),
@ -297,8 +286,8 @@ class ServerApi extends ApiMap
return dkim;
}
Future<ApiResponse<List<ApiToken>>> getApiTokens() async {
ApiResponse<List<ApiToken>> tokens;
Future<GenericResult<List<ApiToken>>> getApiTokens() async {
GenericResult<List<ApiToken>> tokens;
QueryResult<Query$GetApiTokens> response;
try {
@ -307,7 +296,7 @@ class ServerApi extends ApiMap
if (response.hasException) {
final message = response.exception.toString();
print(message);
tokens = ApiResponse<List<ApiToken>>(
tokens = GenericResult<List<ApiToken>>(
success: false,
data: [],
message: message,
@ -321,13 +310,13 @@ class ServerApi extends ApiMap
ApiToken.fromGraphQL(device),
)
.toList();
tokens = ApiResponse<List<ApiToken>>(
tokens = GenericResult<List<ApiToken>>(
success: true,
data: parsed,
);
} catch (e) {
print(e);
tokens = ApiResponse<List<ApiToken>>(
tokens = GenericResult<List<ApiToken>>(
success: false,
data: [],
message: e.toString(),
@ -337,8 +326,8 @@ class ServerApi extends ApiMap
return tokens;
}
Future<ApiResponse<void>> deleteApiToken(final String name) async {
ApiResponse<void> returnable;
Future<GenericResult<void>> deleteApiToken(final String name) async {
GenericResult<void> returnable;
QueryResult<Mutation$DeleteDeviceApiToken> response;
try {
@ -355,19 +344,19 @@ class ServerApi extends ApiMap
);
if (response.hasException) {
print(response.exception.toString());
returnable = ApiResponse<void>(
returnable = GenericResult<void>(
success: false,
data: null,
message: response.exception.toString(),
);
}
returnable = ApiResponse<void>(
returnable = GenericResult<void>(
success: true,
data: null,
);
} catch (e) {
print(e);
returnable = ApiResponse<void>(
returnable = GenericResult<void>(
success: false,
data: null,
message: e.toString(),
@ -377,8 +366,8 @@ class ServerApi extends ApiMap
return returnable;
}
Future<ApiResponse<String>> createDeviceToken() async {
ApiResponse<String> token;
Future<GenericResult<String>> createDeviceToken() async {
GenericResult<String> token;
QueryResult<Mutation$GetNewDeviceApiKey> response;
try {
@ -390,19 +379,19 @@ class ServerApi extends ApiMap
);
if (response.hasException) {
print(response.exception.toString());
token = ApiResponse<String>(
token = GenericResult<String>(
success: false,
data: '',
message: response.exception.toString(),
);
}
token = ApiResponse<String>(
token = GenericResult<String>(
success: true,
data: response.parsedData!.getNewDeviceApiKey.key!,
);
} catch (e) {
print(e);
token = ApiResponse<String>(
token = GenericResult<String>(
success: false,
data: '',
message: e.toString(),
@ -414,10 +403,10 @@ class ServerApi extends ApiMap
Future<bool> isHttpServerWorking() async => (await getApiVersion()) != null;
Future<ApiResponse<String>> authorizeDevice(
Future<GenericResult<String>> authorizeDevice(
final DeviceToken deviceToken,
) async {
ApiResponse<String> token;
GenericResult<String> token;
QueryResult<Mutation$AuthorizeWithNewDeviceApiKey> response;
try {
@ -439,19 +428,19 @@ class ServerApi extends ApiMap
);
if (response.hasException) {
print(response.exception.toString());
token = ApiResponse<String>(
token = GenericResult<String>(
success: false,
data: '',
message: response.exception.toString(),
);
}
token = ApiResponse<String>(
token = GenericResult<String>(
success: true,
data: response.parsedData!.authorizeWithNewDeviceApiKey.token!,
);
} catch (e) {
print(e);
token = ApiResponse<String>(
token = GenericResult<String>(
success: false,
data: '',
message: e.toString(),
@ -461,10 +450,10 @@ class ServerApi extends ApiMap
return token;
}
Future<ApiResponse<String>> useRecoveryToken(
Future<GenericResult<String>> useRecoveryToken(
final DeviceToken deviceToken,
) async {
ApiResponse<String> token;
GenericResult<String> token;
QueryResult<Mutation$UseRecoveryApiKey> response;
try {
@ -486,19 +475,19 @@ class ServerApi extends ApiMap
);
if (response.hasException) {
print(response.exception.toString());
token = ApiResponse<String>(
token = GenericResult<String>(
success: false,
data: '',
message: response.exception.toString(),
);
}
token = ApiResponse<String>(
token = GenericResult<String>(
success: true,
data: response.parsedData!.useRecoveryApiKey.token!,
);
} catch (e) {
print(e);
token = ApiResponse<String>(
token = GenericResult<String>(
success: false,
data: '',
message: e.toString(),

View File

@ -20,20 +20,24 @@ mixin ServicesApi on ApiMap {
return services;
}
Future<GenericMutationResult> enableService(final String serviceId) async {
Future<GenericMutationResult<bool>> enableService(
final String serviceId,
) async {
try {
final GraphQLClient client = await getClient();
final variables = Variables$Mutation$EnableService(serviceId: serviceId);
final mutation = Options$Mutation$EnableService(variables: variables);
final response = await client.mutate$EnableService(mutation);
return GenericMutationResult(
success: response.parsedData?.enableService.success ?? false,
data: response.parsedData?.enableService.success ?? false,
success: true,
code: response.parsedData?.enableService.code ?? 0,
message: response.parsedData?.enableService.message,
);
} catch (e) {
print(e);
return GenericMutationResult(
data: false,
success: false,
code: 0,
message: e.toString(),
@ -41,13 +45,16 @@ mixin ServicesApi on ApiMap {
}
}
Future<GenericMutationResult> disableService(final String serviceId) async {
Future<GenericMutationResult<void>> disableService(
final String serviceId,
) async {
try {
final GraphQLClient client = await getClient();
final variables = Variables$Mutation$DisableService(serviceId: serviceId);
final mutation = Options$Mutation$DisableService(variables: variables);
final response = await client.mutate$DisableService(mutation);
return GenericMutationResult(
data: null,
success: response.parsedData?.disableService.success ?? false,
code: response.parsedData?.disableService.code ?? 0,
message: response.parsedData?.disableService.message,
@ -55,6 +62,7 @@ mixin ServicesApi on ApiMap {
} catch (e) {
print(e);
return GenericMutationResult(
data: null,
success: false,
code: 0,
message: e.toString(),
@ -62,20 +70,24 @@ mixin ServicesApi on ApiMap {
}
}
Future<GenericMutationResult> stopService(final String serviceId) async {
Future<GenericMutationResult<bool>> stopService(
final String serviceId,
) async {
try {
final GraphQLClient client = await getClient();
final variables = Variables$Mutation$StopService(serviceId: serviceId);
final mutation = Options$Mutation$StopService(variables: variables);
final response = await client.mutate$StopService(mutation);
return GenericMutationResult(
success: response.parsedData?.stopService.success ?? false,
data: response.parsedData?.stopService.success ?? false,
success: true,
code: response.parsedData?.stopService.code ?? 0,
message: response.parsedData?.stopService.message,
);
} catch (e) {
print(e);
return GenericMutationResult(
data: false,
success: false,
code: 0,
message: e.toString(),
@ -90,6 +102,7 @@ mixin ServicesApi on ApiMap {
final mutation = Options$Mutation$StartService(variables: variables);
final response = await client.mutate$StartService(mutation);
return GenericMutationResult(
data: null,
success: response.parsedData?.startService.success ?? false,
code: response.parsedData?.startService.code ?? 0,
message: response.parsedData?.startService.message,
@ -97,6 +110,7 @@ mixin ServicesApi on ApiMap {
} catch (e) {
print(e);
return GenericMutationResult(
data: null,
success: false,
code: 0,
message: e.toString(),
@ -104,20 +118,24 @@ mixin ServicesApi on ApiMap {
}
}
Future<GenericMutationResult> restartService(final String serviceId) async {
Future<GenericMutationResult<bool>> restartService(
final String serviceId,
) async {
try {
final GraphQLClient client = await getClient();
final variables = Variables$Mutation$RestartService(serviceId: serviceId);
final mutation = Options$Mutation$RestartService(variables: variables);
final response = await client.mutate$RestartService(mutation);
return GenericMutationResult(
success: response.parsedData?.restartService.success ?? false,
data: response.parsedData?.restartService.success ?? false,
success: true,
code: response.parsedData?.restartService.code ?? 0,
message: response.parsedData?.restartService.message,
);
} catch (e) {
print(e);
return GenericMutationResult(
data: false,
success: false,
code: 0,
message: e.toString(),
@ -125,7 +143,7 @@ mixin ServicesApi on ApiMap {
}
}
Future<GenericJobMutationReturn> moveService(
Future<GenericMutationResult<ServerJob?>> moveService(
final String serviceId,
final String destination,
) async {
@ -140,19 +158,19 @@ mixin ServicesApi on ApiMap {
final mutation = Options$Mutation$MoveService(variables: variables);
final response = await client.mutate$MoveService(mutation);
final jobJson = response.parsedData?.moveService.job?.toJson();
return GenericJobMutationReturn(
success: response.parsedData?.moveService.success ?? false,
return GenericMutationResult(
success: true,
code: response.parsedData?.moveService.code ?? 0,
message: response.parsedData?.moveService.message,
job: jobJson != null ? ServerJob.fromJson(jobJson) : null,
data: jobJson != null ? ServerJob.fromJson(jobJson) : null,
);
} catch (e) {
print(e);
return GenericJobMutationReturn(
return GenericMutationResult(
success: false,
code: 0,
message: e.toString(),
job: null,
data: null,
);
}
}

View File

@ -1,16 +1,5 @@
part of 'server.dart';
class UserMutationResult extends GenericMutationResult {
UserMutationResult({
required super.success,
required super.code,
super.message,
this.user,
});
final User? user;
}
mixin UsersApi on ApiMap {
Future<List<User>> getAllUsers() async {
QueryResult<Query$AllUsers> response;
@ -56,7 +45,7 @@ mixin UsersApi on ApiMap {
return user;
}
Future<UserMutationResult> createUser(
Future<GenericMutationResult<User?>> createUser(
final String username,
final String password,
) async {
@ -67,25 +56,26 @@ mixin UsersApi on ApiMap {
);
final mutation = Options$Mutation$CreateUser(variables: variables);
final response = await client.mutate$CreateUser(mutation);
return UserMutationResult(
success: response.parsedData?.createUser.success ?? false,
return GenericMutationResult(
success: true,
code: response.parsedData?.createUser.code ?? 500,
message: response.parsedData?.createUser.message,
user: response.parsedData?.createUser.user != null
data: response.parsedData?.createUser.user != null
? User.fromGraphQL(response.parsedData!.createUser.user!)
: null,
);
} catch (e) {
print(e);
return UserMutationResult(
return GenericMutationResult(
success: false,
code: 0,
message: e.toString(),
data: null,
);
}
}
Future<GenericMutationResult> deleteUser(
Future<GenericMutationResult<bool>> deleteUser(
final String username,
) async {
try {
@ -94,13 +84,15 @@ mixin UsersApi on ApiMap {
final mutation = Options$Mutation$DeleteUser(variables: variables);
final response = await client.mutate$DeleteUser(mutation);
return GenericMutationResult(
success: response.parsedData?.deleteUser.success ?? false,
data: response.parsedData?.deleteUser.success ?? false,
success: true,
code: response.parsedData?.deleteUser.code ?? 500,
message: response.parsedData?.deleteUser.message,
);
} catch (e) {
print(e);
return GenericMutationResult(
data: false,
success: false,
code: 500,
message: e.toString(),
@ -108,7 +100,7 @@ mixin UsersApi on ApiMap {
}
}
Future<UserMutationResult> updateUser(
Future<GenericMutationResult<User?>> updateUser(
final String username,
final String password,
) async {
@ -119,17 +111,18 @@ mixin UsersApi on ApiMap {
);
final mutation = Options$Mutation$UpdateUser(variables: variables);
final response = await client.mutate$UpdateUser(mutation);
return UserMutationResult(
success: response.parsedData?.updateUser.success ?? false,
return GenericMutationResult(
success: true,
code: response.parsedData?.updateUser.code ?? 500,
message: response.parsedData?.updateUser.message,
user: response.parsedData?.updateUser.user != null
data: response.parsedData?.updateUser.user != null
? User.fromGraphQL(response.parsedData!.updateUser.user!)
: null,
);
} catch (e) {
print(e);
return UserMutationResult(
return GenericMutationResult(
data: null,
success: false,
code: 0,
message: e.toString(),
@ -137,7 +130,7 @@ mixin UsersApi on ApiMap {
}
}
Future<UserMutationResult> addSshKey(
Future<GenericMutationResult<User?>> addSshKey(
final String username,
final String sshKey,
) async {
@ -151,17 +144,18 @@ mixin UsersApi on ApiMap {
);
final mutation = Options$Mutation$AddSshKey(variables: variables);
final response = await client.mutate$AddSshKey(mutation);
return UserMutationResult(
success: response.parsedData?.addSshKey.success ?? false,
return GenericMutationResult(
success: true,
code: response.parsedData?.addSshKey.code ?? 500,
message: response.parsedData?.addSshKey.message,
user: response.parsedData?.addSshKey.user != null
data: response.parsedData?.addSshKey.user != null
? User.fromGraphQL(response.parsedData!.addSshKey.user!)
: null,
);
} catch (e) {
print(e);
return UserMutationResult(
return GenericMutationResult(
data: null,
success: false,
code: 0,
message: e.toString(),
@ -169,7 +163,7 @@ mixin UsersApi on ApiMap {
}
}
Future<UserMutationResult> removeSshKey(
Future<GenericMutationResult<User?>> removeSshKey(
final String username,
final String sshKey,
) async {
@ -183,17 +177,18 @@ mixin UsersApi on ApiMap {
);
final mutation = Options$Mutation$RemoveSshKey(variables: variables);
final response = await client.mutate$RemoveSshKey(mutation);
return UserMutationResult(
return GenericMutationResult(
success: response.parsedData?.removeSshKey.success ?? false,
code: response.parsedData?.removeSshKey.code ?? 500,
message: response.parsedData?.removeSshKey.message,
user: response.parsedData?.removeSshKey.user != null
data: response.parsedData?.removeSshKey.user != null
? User.fromGraphQL(response.parsedData!.removeSshKey.user!)
: null,
);
} catch (e) {
print(e);
return UserMutationResult(
return GenericMutationResult(
data: null,
success: false,
code: 0,
message: e.toString(),

View File

@ -1,15 +1,5 @@
part of 'server.dart';
class MigrateToBindsMutationReturn extends GenericMutationResult {
MigrateToBindsMutationReturn({
required super.success,
required super.code,
super.message,
this.jobUid,
});
final String? jobUid;
}
mixin VolumeApi on ApiMap {
Future<List<ServerDiskVolume>> getServerDiskVolumes() async {
QueryResult response;
@ -67,10 +57,10 @@ mixin VolumeApi on ApiMap {
}
}
Future<MigrateToBindsMutationReturn> migrateToBinds(
Future<GenericMutationResult<String?>> migrateToBinds(
final Map<String, String> serviceToDisk,
) async {
MigrateToBindsMutationReturn? mutation;
GenericMutationResult<String?>? mutation;
try {
final GraphQLClient client = await getClient();
@ -88,19 +78,19 @@ mixin VolumeApi on ApiMap {
await client.mutate$MigrateToBinds(
migrateMutation,
);
mutation = mutation = MigrateToBindsMutationReturn(
success: result.parsedData!.migrateToBinds.success,
mutation = mutation = GenericMutationResult(
success: true,
code: result.parsedData!.migrateToBinds.code,
message: result.parsedData!.migrateToBinds.message,
jobUid: result.parsedData!.migrateToBinds.job?.uid,
data: result.parsedData!.migrateToBinds.job?.uid,
);
} catch (e) {
print(e);
mutation = MigrateToBindsMutationReturn(
mutation = GenericMutationResult(
success: false,
code: 0,
message: e.toString(),
jobUid: null,
data: null,
);
}

View File

@ -35,7 +35,7 @@ class ApiDevicesCubit
}
Future<List<ApiToken>?> _getApiTokens() async {
final ApiResponse<List<ApiToken>> response = await api.getApiTokens();
final GenericResult<List<ApiToken>> response = await api.getApiTokens();
if (response.success) {
return response.data;
} else {
@ -44,7 +44,7 @@ class ApiDevicesCubit
}
Future<void> deleteDevice(final ApiToken device) async {
final ApiResponse<void> response = await api.deleteApiToken(device.name);
final GenericResult<void> response = await api.deleteApiToken(device.name);
if (response.success) {
emit(
ApiDevicesState(
@ -59,7 +59,7 @@ class ApiDevicesCubit
}
Future<String?> getNewDeviceKey() async {
final ApiResponse<String> response = await api.createDeviceToken();
final GenericResult<String> response = await api.createDeviceToken();
if (response.success) {
return response.data;
} else {

View File

@ -32,7 +32,7 @@ class RecoveryKeyCubit
}
Future<RecoveryKeyStatus?> _getRecoveryKeyStatus() async {
final ApiResponse<RecoveryKeyStatus?> response =
final GenericResult<RecoveryKeyStatus?> response =
await api.getRecoveryTokenStatus();
if (response.success) {
return response.data;
@ -57,7 +57,7 @@ class RecoveryKeyCubit
final DateTime? expirationDate,
final int? numberOfUses,
}) async {
final ApiResponse<String> response =
final GenericResult<String> response =
await api.generateRecoveryToken(expirationDate, numberOfUses);
if (response.success) {
refresh();

View File

@ -494,13 +494,13 @@ class ServerInstallationRepository {
overrideDomain: serverDomain.domainName,
);
final String serverIp = await getServerIpFromDomain(serverDomain);
final ApiResponse<String> apiResponse = await serverApi.authorizeDevice(
final GenericResult<String> result = await serverApi.authorizeDevice(
DeviceToken(device: await getDeviceName(), token: newDeviceKey),
);
if (apiResponse.success) {
if (result.success) {
return ServerHostingDetails(
apiToken: apiResponse.data,
apiToken: result.data,
volume: ServerVolume(
id: 0,
name: '',
@ -517,7 +517,7 @@ class ServerInstallationRepository {
}
throw ServerAuthorizationException(
apiResponse.message ?? apiResponse.data,
result.message ?? result.data,
);
}
@ -531,13 +531,13 @@ class ServerInstallationRepository {
overrideDomain: serverDomain.domainName,
);
final String serverIp = await getServerIpFromDomain(serverDomain);
final ApiResponse<String> apiResponse = await serverApi.useRecoveryToken(
final GenericResult<String> result = await serverApi.useRecoveryToken(
DeviceToken(device: await getDeviceName(), token: recoveryKey),
);
if (apiResponse.success) {
if (result.success) {
return ServerHostingDetails(
apiToken: apiResponse.data,
apiToken: result.data,
volume: ServerVolume(
id: 0,
name: '',
@ -554,7 +554,7 @@ class ServerInstallationRepository {
}
throw ServerAuthorizationException(
apiResponse.message ?? apiResponse.data,
result.message ?? result.data,
);
}
@ -592,15 +592,15 @@ class ServerInstallationRepository {
);
}
}
final ApiResponse<String> deviceAuthKey =
final GenericResult<String> deviceAuthKey =
await serverApi.createDeviceToken();
final ApiResponse<String> apiResponse = await serverApi.authorizeDevice(
final GenericResult<String> result = await serverApi.authorizeDevice(
DeviceToken(device: await getDeviceName(), token: deviceAuthKey.data),
);
if (apiResponse.success) {
if (result.success) {
return ServerHostingDetails(
apiToken: apiResponse.data,
apiToken: result.data,
volume: ServerVolume(
id: 0,
name: '',
@ -617,7 +617,7 @@ class ServerInstallationRepository {
}
throw ServerAuthorizationException(
apiResponse.message ?? apiResponse.data,
result.message ?? result.data,
);
}

View File

@ -1,5 +1,6 @@
import 'dart:async';
import 'package:easy_localization/easy_localization.dart';
import 'package:selfprivacy/config/get_it_config.dart';
import 'package:selfprivacy/logic/api_maps/graphql_maps/server_api/server.dart';
import 'package:selfprivacy/logic/cubit/app_config_dependent/authentication_dependend_cubit.dart';
@ -46,14 +47,14 @@ class ServerJobsCubit
Future<void> migrateToBinds(final Map<String, String> serviceToDisk) async {
final result = await api.migrateToBinds(serviceToDisk);
if (!result.success || result.jobUid == null) {
if (result.data == null) {
getIt<NavigationService>().showSnackBar(result.message!);
return;
}
emit(
ServerJobsState(
migrationJobUid: result.jobUid,
migrationJobUid: result.data,
),
);
}
@ -75,7 +76,13 @@ class ServerJobsCubit
Future<void> removeServerJob(final String uid) async {
final result = await api.removeApiJob(uid);
if (!result.success) {
getIt<NavigationService>().showSnackBar(result.message!);
getIt<NavigationService>().showSnackBar('jobs.generic_error'.tr());
return;
}
if (!result.data) {
getIt<NavigationService>()
.showSnackBar(result.message ?? 'jobs.generic_error'.tr());
return;
}
@ -87,7 +94,6 @@ class ServerJobsCubit
],
),
);
print('removed job $uid');
}
Future<void> removeAllFinishedJobs() async {

View File

@ -1,5 +1,7 @@
import 'dart:async';
import 'package:easy_localization/easy_localization.dart';
import 'package:selfprivacy/config/get_it_config.dart';
import 'package:selfprivacy/logic/api_maps/graphql_maps/server_api/server.dart';
import 'package:selfprivacy/logic/cubit/app_config_dependent/authentication_dependend_cubit.dart';
import 'package:selfprivacy/logic/models/service.dart';
@ -39,7 +41,17 @@ class ServicesCubit extends ServerInstallationDependendCubit<ServicesState> {
Future<void> restart(final String serviceId) async {
emit(state.copyWith(lockedServices: [...state.lockedServices, serviceId]));
await api.restartService(serviceId);
final result = await api.restartService(serviceId);
if (!result.success) {
getIt<NavigationService>().showSnackBar('jobs.generic_error'.tr());
return;
}
if (!result.data) {
getIt<NavigationService>()
.showSnackBar(result.message ?? 'jobs.generic_error'.tr());
return;
}
await Future.delayed(const Duration(seconds: 2));
reload();
await Future.delayed(const Duration(seconds: 10));

View File

@ -78,17 +78,16 @@ class UsersCubit extends ServerInstallationDependendCubit<UsersState> {
return;
}
// If API returned error, do nothing
final UserMutationResult result =
final GenericMutationResult<User?> result =
await api.createUser(user.login, password);
final User? createdUser = result.user;
if (!result.success || createdUser == null) {
if (result.data == null) {
getIt<NavigationService>()
.showSnackBar(result.message ?? 'users.could_not_create_user'.tr());
return;
}
final List<User> loadedUsers = List<User>.from(state.users);
loadedUsers.add(createdUser);
loadedUsers.add(result.data!);
await box.clear();
await box.addAll(loadedUsers);
emit(state.copyWith(users: loadedUsers));
@ -103,14 +102,20 @@ class UsersCubit extends ServerInstallationDependendCubit<UsersState> {
}
final List<User> loadedUsers = List<User>.from(state.users);
final GenericMutationResult result = await api.deleteUser(user.login);
if (result.success) {
if (result.success && result.data) {
loadedUsers.removeWhere((final User u) => u.login == user.login);
await box.clear();
await box.addAll(loadedUsers);
emit(state.copyWith(users: loadedUsers));
} else {
}
if (!result.success) {
getIt<NavigationService>().showSnackBar('jobs.generic_error'.tr());
}
if (!result.data) {
getIt<NavigationService>()
.showSnackBar(result.message ?? 'users.could_not_delete_user'.tr());
.showSnackBar(result.message ?? 'jobs.generic_error'.tr());
}
}
@ -123,9 +128,9 @@ class UsersCubit extends ServerInstallationDependendCubit<UsersState> {
.showSnackBar('users.could_not_change_password'.tr());
return;
}
final UserMutationResult result =
final GenericMutationResult<User?> result =
await api.updateUser(user.login, newPassword);
if (!result.success) {
if (result.data == null) {
getIt<NavigationService>().showSnackBar(
result.message ?? 'users.could_not_change_password'.tr(),
);
@ -133,10 +138,10 @@ class UsersCubit extends ServerInstallationDependendCubit<UsersState> {
}
Future<void> addSshKey(final User user, final String publicKey) async {
final UserMutationResult result =
final GenericMutationResult<User?> result =
await api.addSshKey(user.login, publicKey);
if (result.success) {
final User updatedUser = result.user!;
if (result.data != null) {
final User updatedUser = result.data!;
final int index =
state.users.indexWhere((final User u) => u.login == user.login);
await box.putAt(index, updatedUser);
@ -152,10 +157,10 @@ class UsersCubit extends ServerInstallationDependendCubit<UsersState> {
}
Future<void> deleteSshKey(final User user, final String publicKey) async {
final UserMutationResult result =
final GenericMutationResult<User?> result =
await api.removeSshKey(user.login, publicKey);
if (result.success) {
final User updatedUser = result.user!;
if (result.data != null) {
final User updatedUser = result.data!;
final int index =
state.users.indexWhere((final User u) => u.login == user.login);
await box.putAt(index, updatedUser);