mirror of
https://git.selfprivacy.org/kherel/selfprivacy.org.app.git
synced 2025-01-11 18:39:45 +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> {
|
class ApiResponse<D> {
|
||||||
ApiResponse({
|
ApiResponse({
|
||||||
required this.statusCode,
|
required this.success,
|
||||||
required this.data,
|
required this.data,
|
||||||
this.errorMessage,
|
this.message,
|
||||||
});
|
});
|
||||||
final int statusCode;
|
final bool success;
|
||||||
final String? errorMessage;
|
final String? message;
|
||||||
final D data;
|
final D data;
|
||||||
|
|
||||||
bool get isSuccess => statusCode >= 200 && statusCode < 300;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class ServerApi extends ApiMap
|
class ServerApi extends ApiMap
|
||||||
|
@ -214,12 +212,76 @@ class ServerApi extends ApiMap
|
||||||
return settings;
|
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(
|
Future<ApiResponse<String>> generateRecoveryToken(
|
||||||
final DateTime? expirationDate,
|
final DateTime? expirationDate,
|
||||||
final int? numberOfUses,
|
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 {}
|
Future<String?> getDkim() async {}
|
||||||
|
|
||||||
|
|
|
@ -36,7 +36,7 @@ class ApiDevicesCubit
|
||||||
|
|
||||||
Future<List<ApiToken>?> _getApiTokens() async {
|
Future<List<ApiToken>?> _getApiTokens() async {
|
||||||
final ApiResponse<List<ApiToken>> response = await api.getApiTokens();
|
final ApiResponse<List<ApiToken>> response = await api.getApiTokens();
|
||||||
if (response.isSuccess) {
|
if (response.success) {
|
||||||
return response.data;
|
return response.data;
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
|
@ -45,7 +45,7 @@ class ApiDevicesCubit
|
||||||
|
|
||||||
Future<void> deleteDevice(final ApiToken device) async {
|
Future<void> deleteDevice(final ApiToken device) async {
|
||||||
final ApiResponse<void> response = await api.deleteApiToken(device.name);
|
final ApiResponse<void> response = await api.deleteApiToken(device.name);
|
||||||
if (response.isSuccess) {
|
if (response.success) {
|
||||||
emit(
|
emit(
|
||||||
ApiDevicesState(
|
ApiDevicesState(
|
||||||
state.devices.where((final d) => d.name != device.name).toList(),
|
state.devices.where((final d) => d.name != device.name).toList(),
|
||||||
|
@ -54,17 +54,17 @@ class ApiDevicesCubit
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
getIt<NavigationService>()
|
getIt<NavigationService>()
|
||||||
.showSnackBar(response.errorMessage ?? 'Error deleting device');
|
.showSnackBar(response.message ?? 'Error deleting device');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<String?> getNewDeviceKey() async {
|
Future<String?> getNewDeviceKey() async {
|
||||||
final ApiResponse<String> response = await api.createDeviceToken();
|
final ApiResponse<String> response = await api.createDeviceToken();
|
||||||
if (response.isSuccess) {
|
if (response.success) {
|
||||||
return response.data;
|
return response.data;
|
||||||
} else {
|
} else {
|
||||||
getIt<NavigationService>().showSnackBar(
|
getIt<NavigationService>().showSnackBar(
|
||||||
response.errorMessage ?? 'Error getting new device key',
|
response.message ?? 'Error getting new device key',
|
||||||
);
|
);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,7 +34,7 @@ class RecoveryKeyCubit
|
||||||
Future<RecoveryKeyStatus?> _getRecoveryKeyStatus() async {
|
Future<RecoveryKeyStatus?> _getRecoveryKeyStatus() async {
|
||||||
final ApiResponse<RecoveryKeyStatus?> response =
|
final ApiResponse<RecoveryKeyStatus?> response =
|
||||||
await api.getRecoveryTokenStatus();
|
await api.getRecoveryTokenStatus();
|
||||||
if (response.isSuccess) {
|
if (response.success) {
|
||||||
return response.data;
|
return response.data;
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
|
@ -59,11 +59,11 @@ class RecoveryKeyCubit
|
||||||
}) async {
|
}) async {
|
||||||
final ApiResponse<String> response =
|
final ApiResponse<String> response =
|
||||||
await api.generateRecoveryToken(expirationDate, numberOfUses);
|
await api.generateRecoveryToken(expirationDate, numberOfUses);
|
||||||
if (response.isSuccess) {
|
if (response.success) {
|
||||||
refresh();
|
refresh();
|
||||||
return response.data;
|
return response.data;
|
||||||
} else {
|
} 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),
|
DeviceToken(device: await getDeviceName(), token: newDeviceKey),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (apiResponse.isSuccess) {
|
if (apiResponse.success) {
|
||||||
return ServerHostingDetails(
|
return ServerHostingDetails(
|
||||||
apiToken: apiResponse.data,
|
apiToken: apiResponse.data,
|
||||||
volume: ServerVolume(
|
volume: ServerVolume(
|
||||||
|
@ -517,7 +517,7 @@ class ServerInstallationRepository {
|
||||||
}
|
}
|
||||||
|
|
||||||
throw ServerAuthorizationException(
|
throw ServerAuthorizationException(
|
||||||
apiResponse.errorMessage ?? apiResponse.data,
|
apiResponse.message ?? apiResponse.data,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -535,7 +535,7 @@ class ServerInstallationRepository {
|
||||||
DeviceToken(device: await getDeviceName(), token: recoveryKey),
|
DeviceToken(device: await getDeviceName(), token: recoveryKey),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (apiResponse.isSuccess) {
|
if (apiResponse.success) {
|
||||||
return ServerHostingDetails(
|
return ServerHostingDetails(
|
||||||
apiToken: apiResponse.data,
|
apiToken: apiResponse.data,
|
||||||
volume: ServerVolume(
|
volume: ServerVolume(
|
||||||
|
@ -554,7 +554,7 @@ class ServerInstallationRepository {
|
||||||
}
|
}
|
||||||
|
|
||||||
throw ServerAuthorizationException(
|
throw ServerAuthorizationException(
|
||||||
apiResponse.errorMessage ?? apiResponse.data,
|
apiResponse.message ?? apiResponse.data,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -600,7 +600,7 @@ class ServerInstallationRepository {
|
||||||
DeviceToken(device: await getDeviceName(), token: deviceAuthKey.data),
|
DeviceToken(device: await getDeviceName(), token: deviceAuthKey.data),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (apiResponse.isSuccess) {
|
if (apiResponse.success) {
|
||||||
return ServerHostingDetails(
|
return ServerHostingDetails(
|
||||||
apiToken: apiResponse.data,
|
apiToken: apiResponse.data,
|
||||||
volume: ServerVolume(
|
volume: ServerVolume(
|
||||||
|
@ -619,7 +619,7 @@ class ServerInstallationRepository {
|
||||||
}
|
}
|
||||||
|
|
||||||
throw ServerAuthorizationException(
|
throw ServerAuthorizationException(
|
||||||
apiResponse.errorMessage ?? apiResponse.data,
|
apiResponse.message ?? apiResponse.data,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -636,7 +636,7 @@ class ServerInstallationRepository {
|
||||||
final String? serverApiVersion = await serverApi.getApiVersion();
|
final String? serverApiVersion = await serverApi.getApiVersion();
|
||||||
final ApiResponse<List<String>> users =
|
final ApiResponse<List<String>> users =
|
||||||
await serverApi.getUsersList(withMainUser: true);
|
await serverApi.getUsersList(withMainUser: true);
|
||||||
if (serverApiVersion == null || !users.isSuccess) {
|
if (serverApiVersion == null || !users.success) {
|
||||||
return fallbackUser;
|
return fallbackUser;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import 'package:equatable/equatable.dart';
|
import 'package:equatable/equatable.dart';
|
||||||
import 'package:json_annotation/json_annotation.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';
|
part 'recovery_token_status.g.dart';
|
||||||
|
|
||||||
|
@ -15,6 +16,16 @@ class RecoveryKeyStatus extends Equatable {
|
||||||
this.usesLeft,
|
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 bool exists;
|
||||||
final DateTime? date;
|
final DateTime? date;
|
||||||
final DateTime? expiration;
|
final DateTime? expiration;
|
||||||
|
|
Loading…
Reference in a new issue