mirror of
https://git.selfprivacy.org/kherel/selfprivacy.org.app.git
synced 2025-01-11 10:29:39 +00:00
fix(server-api): Adapt ApiResponse structure to GraphQL format
This commit is contained in:
parent
2b419d5923
commit
19b45ac142
|
@ -49,15 +49,13 @@ class GenericJobMutationReturn extends GenericMutationResult {
|
|||
|
||||
class ApiResponse<D> {
|
||||
ApiResponse({
|
||||
required this.statusCode,
|
||||
required this.success,
|
||||
required this.data,
|
||||
this.errorMessage,
|
||||
this.message,
|
||||
});
|
||||
final int statusCode;
|
||||
final String? errorMessage;
|
||||
final bool success;
|
||||
final String? message;
|
||||
final D data;
|
||||
|
||||
bool get isSuccess => statusCode >= 200 && statusCode < 300;
|
||||
}
|
||||
|
||||
class ServerApi extends ApiMap
|
||||
|
@ -214,12 +212,76 @@ class ServerApi extends ApiMap
|
|||
return settings;
|
||||
}
|
||||
|
||||
Future<ApiResponse<RecoveryKeyStatus?>> getRecoveryTokenStatus() async {}
|
||||
Future<ApiResponse<RecoveryKeyStatus?>> getRecoveryTokenStatus() async {
|
||||
RecoveryKeyStatus? key;
|
||||
QueryResult<Query$RecoveryKey> response;
|
||||
String? error;
|
||||
|
||||
try {
|
||||
final GraphQLClient client = await getClient();
|
||||
response = await client.query$RecoveryKey();
|
||||
if (response.hasException) {
|
||||
print(response.exception.toString());
|
||||
error = response.exception.toString();
|
||||
}
|
||||
key = RecoveryKeyStatus.fromGraphQL(response.parsedData!.api.recoveryKey);
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
|
||||
return ApiResponse<RecoveryKeyStatus?>(
|
||||
success: error == null,
|
||||
data: key,
|
||||
message: error,
|
||||
);
|
||||
}
|
||||
|
||||
Future<ApiResponse<String>> generateRecoveryToken(
|
||||
final DateTime? expirationDate,
|
||||
final int? numberOfUses,
|
||||
) async {}
|
||||
) async {
|
||||
ApiResponse<String> key;
|
||||
QueryResult<Mutation$GetNewRecoveryApiKey> response;
|
||||
|
||||
try {
|
||||
final GraphQLClient client = await getClient();
|
||||
|
||||
final input = Input$RecoveryKeyLimitsInput(
|
||||
expirationDate: expirationDate,
|
||||
uses: numberOfUses,
|
||||
);
|
||||
final variables = Variables$Mutation$GetNewRecoveryApiKey(
|
||||
limits: input,
|
||||
);
|
||||
final mutation = Options$Mutation$GetNewRecoveryApiKey(
|
||||
variables: variables,
|
||||
);
|
||||
response = await client.mutate$GetNewRecoveryApiKey(
|
||||
mutation,
|
||||
);
|
||||
if (response.hasException) {
|
||||
print(response.exception.toString());
|
||||
key = ApiResponse<String>(
|
||||
success: false,
|
||||
data: '',
|
||||
message: response.exception.toString(),
|
||||
);
|
||||
}
|
||||
key = ApiResponse<String>(
|
||||
success: true,
|
||||
data: response.parsedData!.getNewRecoveryApiKey.key!,
|
||||
);
|
||||
} catch (e) {
|
||||
print(e);
|
||||
key = ApiResponse<String>(
|
||||
success: false,
|
||||
data: '',
|
||||
message: e.toString(),
|
||||
);
|
||||
}
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
Future<String?> getDkim() async {}
|
||||
|
||||
|
|
|
@ -36,7 +36,7 @@ class ApiDevicesCubit
|
|||
|
||||
Future<List<ApiToken>?> _getApiTokens() async {
|
||||
final ApiResponse<List<ApiToken>> response = await api.getApiTokens();
|
||||
if (response.isSuccess) {
|
||||
if (response.success) {
|
||||
return response.data;
|
||||
} else {
|
||||
return null;
|
||||
|
@ -45,7 +45,7 @@ class ApiDevicesCubit
|
|||
|
||||
Future<void> deleteDevice(final ApiToken device) async {
|
||||
final ApiResponse<void> response = await api.deleteApiToken(device.name);
|
||||
if (response.isSuccess) {
|
||||
if (response.success) {
|
||||
emit(
|
||||
ApiDevicesState(
|
||||
state.devices.where((final d) => d.name != device.name).toList(),
|
||||
|
@ -54,17 +54,17 @@ class ApiDevicesCubit
|
|||
);
|
||||
} else {
|
||||
getIt<NavigationService>()
|
||||
.showSnackBar(response.errorMessage ?? 'Error deleting device');
|
||||
.showSnackBar(response.message ?? 'Error deleting device');
|
||||
}
|
||||
}
|
||||
|
||||
Future<String?> getNewDeviceKey() async {
|
||||
final ApiResponse<String> response = await api.createDeviceToken();
|
||||
if (response.isSuccess) {
|
||||
if (response.success) {
|
||||
return response.data;
|
||||
} else {
|
||||
getIt<NavigationService>().showSnackBar(
|
||||
response.errorMessage ?? 'Error getting new device key',
|
||||
response.message ?? 'Error getting new device key',
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -34,7 +34,7 @@ class RecoveryKeyCubit
|
|||
Future<RecoveryKeyStatus?> _getRecoveryKeyStatus() async {
|
||||
final ApiResponse<RecoveryKeyStatus?> response =
|
||||
await api.getRecoveryTokenStatus();
|
||||
if (response.isSuccess) {
|
||||
if (response.success) {
|
||||
return response.data;
|
||||
} else {
|
||||
return null;
|
||||
|
@ -59,11 +59,11 @@ class RecoveryKeyCubit
|
|||
}) async {
|
||||
final ApiResponse<String> response =
|
||||
await api.generateRecoveryToken(expirationDate, numberOfUses);
|
||||
if (response.isSuccess) {
|
||||
if (response.success) {
|
||||
refresh();
|
||||
return response.data;
|
||||
} else {
|
||||
throw GenerationError(response.errorMessage ?? 'Unknown error');
|
||||
throw GenerationError(response.message ?? 'Unknown error');
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -498,7 +498,7 @@ class ServerInstallationRepository {
|
|||
DeviceToken(device: await getDeviceName(), token: newDeviceKey),
|
||||
);
|
||||
|
||||
if (apiResponse.isSuccess) {
|
||||
if (apiResponse.success) {
|
||||
return ServerHostingDetails(
|
||||
apiToken: apiResponse.data,
|
||||
volume: ServerVolume(
|
||||
|
@ -517,7 +517,7 @@ class ServerInstallationRepository {
|
|||
}
|
||||
|
||||
throw ServerAuthorizationException(
|
||||
apiResponse.errorMessage ?? apiResponse.data,
|
||||
apiResponse.message ?? apiResponse.data,
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -535,7 +535,7 @@ class ServerInstallationRepository {
|
|||
DeviceToken(device: await getDeviceName(), token: recoveryKey),
|
||||
);
|
||||
|
||||
if (apiResponse.isSuccess) {
|
||||
if (apiResponse.success) {
|
||||
return ServerHostingDetails(
|
||||
apiToken: apiResponse.data,
|
||||
volume: ServerVolume(
|
||||
|
@ -554,7 +554,7 @@ class ServerInstallationRepository {
|
|||
}
|
||||
|
||||
throw ServerAuthorizationException(
|
||||
apiResponse.errorMessage ?? apiResponse.data,
|
||||
apiResponse.message ?? apiResponse.data,
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -600,7 +600,7 @@ class ServerInstallationRepository {
|
|||
DeviceToken(device: await getDeviceName(), token: deviceAuthKey.data),
|
||||
);
|
||||
|
||||
if (apiResponse.isSuccess) {
|
||||
if (apiResponse.success) {
|
||||
return ServerHostingDetails(
|
||||
apiToken: apiResponse.data,
|
||||
volume: ServerVolume(
|
||||
|
@ -619,7 +619,7 @@ class ServerInstallationRepository {
|
|||
}
|
||||
|
||||
throw ServerAuthorizationException(
|
||||
apiResponse.errorMessage ?? apiResponse.data,
|
||||
apiResponse.message ?? apiResponse.data,
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -636,7 +636,7 @@ class ServerInstallationRepository {
|
|||
final String? serverApiVersion = await serverApi.getApiVersion();
|
||||
final ApiResponse<List<String>> users =
|
||||
await serverApi.getUsersList(withMainUser: true);
|
||||
if (serverApiVersion == null || !users.isSuccess) {
|
||||
if (serverApiVersion == null || !users.success) {
|
||||
return fallbackUser;
|
||||
}
|
||||
try {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import 'package:equatable/equatable.dart';
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/graphql_maps/schema/server_api.graphql.dart';
|
||||
|
||||
part 'recovery_token_status.g.dart';
|
||||
|
||||
|
@ -15,6 +16,16 @@ class RecoveryKeyStatus extends Equatable {
|
|||
this.usesLeft,
|
||||
});
|
||||
|
||||
RecoveryKeyStatus.fromGraphQL(
|
||||
final Query$RecoveryKey$api$recoveryKey recoveryKey,
|
||||
) : this(
|
||||
exists: recoveryKey.exists,
|
||||
date: recoveryKey.creationDate,
|
||||
expiration: recoveryKey.expirationDate,
|
||||
usesLeft: recoveryKey.usesLeft,
|
||||
valid: recoveryKey.valid,
|
||||
);
|
||||
|
||||
final bool exists;
|
||||
final DateTime? date;
|
||||
final DateTime? expiration;
|
||||
|
|
Loading…
Reference in a new issue