mirror of
https://git.selfprivacy.org/kherel/selfprivacy.org.app.git
synced 2025-02-04 07:20:39 +00:00
Refactor graphql api, separate by logic
This commit is contained in:
parent
9526035176
commit
d934a6a9cb
|
@ -172,6 +172,7 @@
|
|||
"disk_total": "{} GB total · {}",
|
||||
"gb": "{} GB",
|
||||
"mb": "{} MB",
|
||||
"kb": "{} KB",
|
||||
"extend_volume_button": "Extend volume",
|
||||
"extending_volume_title": "Extending volume",
|
||||
"extending_volume_description": "Resizing volume will allow you to store more data on your server without extending the server itself. Volume can only be extended: shrinking is not possible.",
|
||||
|
|
|
@ -1,235 +0,0 @@
|
|||
import 'package:graphql/client.dart';
|
||||
import 'package:selfprivacy/config/get_it_config.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/graphql_maps/api_map.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/graphql_maps/schema/server_api.graphql.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/graphql_maps/schema/disk_volumes.graphql.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/graphql_maps/schema/services.graphql.dart';
|
||||
import 'package:selfprivacy/logic/models/hive/server_domain.dart';
|
||||
import 'package:selfprivacy/logic/models/json/api_token.dart';
|
||||
import 'package:selfprivacy/logic/models/json/server_disk_volume.dart';
|
||||
import 'package:selfprivacy/logic/models/json/server_job.dart';
|
||||
|
||||
class ServerApi extends ApiMap {
|
||||
ServerApi({
|
||||
this.hasLogger = false,
|
||||
this.isWithToken = true,
|
||||
this.customToken = '',
|
||||
}) {
|
||||
final ServerDomain? serverDomain = getIt<ApiConfigModel>().serverDomain;
|
||||
rootAddress = serverDomain?.domainName ?? '';
|
||||
}
|
||||
@override
|
||||
bool hasLogger;
|
||||
@override
|
||||
bool isWithToken;
|
||||
@override
|
||||
String customToken;
|
||||
@override
|
||||
String? rootAddress;
|
||||
|
||||
Future<bool> _commonBoolRequest(final Function graphQLMethod) async {
|
||||
QueryResult response;
|
||||
bool result = false;
|
||||
|
||||
try {
|
||||
response = await graphQLMethod();
|
||||
if (response.hasException) {
|
||||
print(response.exception.toString());
|
||||
result = false;
|
||||
} else {
|
||||
result = true;
|
||||
}
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Future<String?> getApiVersion() async {
|
||||
QueryResult response;
|
||||
String? apiVersion;
|
||||
|
||||
try {
|
||||
final GraphQLClient client = await getClient();
|
||||
response = await client.query$GetApiVersion();
|
||||
if (response.hasException) {
|
||||
print(response.exception.toString());
|
||||
}
|
||||
apiVersion = response.data!['api']['version'];
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
return apiVersion;
|
||||
}
|
||||
|
||||
Future<List<ApiToken>> getApiTokens() async {
|
||||
QueryResult response;
|
||||
List<ApiToken> tokens = [];
|
||||
|
||||
try {
|
||||
final GraphQLClient client = await getClient();
|
||||
response = await client.query$GetApiTokens();
|
||||
if (response.hasException) {
|
||||
print(response.exception.toString());
|
||||
}
|
||||
tokens = response.data!['api']['devices']
|
||||
.map<ApiToken>((final e) => ApiToken.fromJson(e))
|
||||
.toList();
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
|
||||
return tokens;
|
||||
}
|
||||
|
||||
Future<List<ServerDiskVolume>> getServerDiskVolumes() async {
|
||||
QueryResult response;
|
||||
List<ServerDiskVolume> volumes = [];
|
||||
|
||||
try {
|
||||
final GraphQLClient client = await getClient();
|
||||
response = await client.query$GetServerDiskVolumes();
|
||||
if (response.hasException) {
|
||||
print(response.exception.toString());
|
||||
}
|
||||
volumes = response.data!['storage']['volumes']
|
||||
.map<ServerDiskVolume>((final e) => ServerDiskVolume.fromJson(e))
|
||||
.toList();
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
|
||||
return volumes;
|
||||
}
|
||||
|
||||
Future<void> mountVolume(final String volumeName) async {
|
||||
try {
|
||||
final GraphQLClient client = await getClient();
|
||||
final variables = Variables$Mutation$MountVolume(name: volumeName);
|
||||
final mountVolumeMutation =
|
||||
Options$Mutation$MountVolume(variables: variables);
|
||||
await client.mutate$MountVolume(mountVolumeMutation);
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> unmountVolume(final String volumeName) async {
|
||||
try {
|
||||
final GraphQLClient client = await getClient();
|
||||
final variables = Variables$Mutation$UnmountVolume(name: volumeName);
|
||||
final unmountVolumeMutation =
|
||||
Options$Mutation$UnmountVolume(variables: variables);
|
||||
await client.mutate$UnmountVolume(unmountVolumeMutation);
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> resizeVolume(final String volumeName) async {
|
||||
try {
|
||||
final GraphQLClient client = await getClient();
|
||||
final variables = Variables$Mutation$ResizeVolume(name: volumeName);
|
||||
final resizeVolumeMutation =
|
||||
Options$Mutation$ResizeVolume(variables: variables);
|
||||
await client.mutate$ResizeVolume(resizeVolumeMutation);
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<ServerJob>> getServerJobs() async {
|
||||
QueryResult response;
|
||||
List<ServerJob> jobs = [];
|
||||
|
||||
try {
|
||||
final GraphQLClient client = await getClient();
|
||||
response = await client.query$GetApiJobs();
|
||||
if (response.hasException) {
|
||||
print(response.exception.toString());
|
||||
}
|
||||
jobs = response.data!['jobs']
|
||||
.map<ServerJob>((final e) => ServerJob.fromJson(e))
|
||||
.toList();
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
|
||||
return jobs;
|
||||
}
|
||||
|
||||
Future<void> removeApiJob(final String uid) async {
|
||||
try {
|
||||
final GraphQLClient client = await getClient();
|
||||
//await client.query$GetApiJobsQuery();
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> reboot() async {
|
||||
try {
|
||||
final GraphQLClient client = await getClient();
|
||||
return await _commonBoolRequest(
|
||||
() async {
|
||||
await client.mutate$RebootSystem();
|
||||
},
|
||||
);
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> pullConfigurationUpdate() async {
|
||||
try {
|
||||
final GraphQLClient client = await getClient();
|
||||
return await _commonBoolRequest(
|
||||
() async {
|
||||
await client.mutate$PullRepositoryChanges();
|
||||
},
|
||||
);
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> upgrade() async {
|
||||
try {
|
||||
final GraphQLClient client = await getClient();
|
||||
return await _commonBoolRequest(
|
||||
() async {
|
||||
await client.mutate$RunSystemUpgrade();
|
||||
},
|
||||
);
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> switchService(final String uid, final bool needTurnOn) async {
|
||||
try {
|
||||
final GraphQLClient client = await getClient();
|
||||
if (needTurnOn) {
|
||||
final variables = Variables$Mutation$EnableService(serviceId: uid);
|
||||
final mutation = Options$Mutation$EnableService(variables: variables);
|
||||
await client.mutate$EnableService(mutation);
|
||||
} else {
|
||||
final variables = Variables$Mutation$DisableService(serviceId: uid);
|
||||
final mutation = Options$Mutation$DisableService(variables: variables);
|
||||
await client.mutate$DisableService(mutation);
|
||||
}
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> apply() async {
|
||||
try {
|
||||
final GraphQLClient client = await getClient();
|
||||
await client.mutate$RunSystemRebuild();
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
}
|
||||
}
|
34
lib/logic/api_maps/graphql_maps/server_api/jobs_api.dart
Normal file
34
lib/logic/api_maps/graphql_maps/server_api/jobs_api.dart
Normal file
|
@ -0,0 +1,34 @@
|
|||
part of 'server.dart';
|
||||
|
||||
mixin JobsApi on ApiMap {
|
||||
Future<List<ServerJob>> getServerJobs() async {
|
||||
QueryResult response;
|
||||
List<ServerJob> jobs = [];
|
||||
|
||||
try {
|
||||
final GraphQLClient client = await getClient();
|
||||
response = await client.query$GetApiJobs();
|
||||
if (response.hasException) {
|
||||
print(response.exception.toString());
|
||||
}
|
||||
jobs = response.data!['jobs']
|
||||
.map<ServerJob>((final e) => ServerJob.fromJson(e))
|
||||
.toList();
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
|
||||
return jobs;
|
||||
}
|
||||
|
||||
Future<void> 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);
|
||||
await client.mutate$RemoveJob(mutation);
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
}
|
||||
}
|
88
lib/logic/api_maps/graphql_maps/server_api/server.dart
Normal file
88
lib/logic/api_maps/graphql_maps/server_api/server.dart
Normal file
|
@ -0,0 +1,88 @@
|
|||
import 'package:graphql/client.dart';
|
||||
import 'package:selfprivacy/config/get_it_config.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/graphql_maps/api_map.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/graphql_maps/schema/server_api.graphql.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/graphql_maps/schema/disk_volumes.graphql.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/graphql_maps/schema/services.graphql.dart';
|
||||
import 'package:selfprivacy/logic/models/hive/server_domain.dart';
|
||||
import 'package:selfprivacy/logic/models/json/api_token.dart';
|
||||
import 'package:selfprivacy/logic/models/json/server_disk_volume.dart';
|
||||
import 'package:selfprivacy/logic/models/json/server_job.dart';
|
||||
|
||||
part 'volume_api.dart';
|
||||
part 'jobs_api.dart';
|
||||
part 'server_actions_api.dart';
|
||||
part 'services_api.dart';
|
||||
|
||||
class ServerApi extends ApiMap with VolumeApi, JobsApi, ServerActionsApi {
|
||||
ServerApi({
|
||||
this.hasLogger = false,
|
||||
this.isWithToken = true,
|
||||
this.customToken = '',
|
||||
}) {
|
||||
final ServerDomain? serverDomain = getIt<ApiConfigModel>().serverDomain;
|
||||
rootAddress = serverDomain?.domainName ?? '';
|
||||
}
|
||||
@override
|
||||
bool hasLogger;
|
||||
@override
|
||||
bool isWithToken;
|
||||
@override
|
||||
String customToken;
|
||||
@override
|
||||
String? rootAddress;
|
||||
|
||||
Future<String?> getApiVersion() async {
|
||||
QueryResult response;
|
||||
String? apiVersion;
|
||||
|
||||
try {
|
||||
final GraphQLClient client = await getClient();
|
||||
response = await client.query$GetApiVersion();
|
||||
if (response.hasException) {
|
||||
print(response.exception.toString());
|
||||
}
|
||||
apiVersion = response.data!['api']['version'];
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
return apiVersion;
|
||||
}
|
||||
|
||||
Future<List<ApiToken>> getApiTokens() async {
|
||||
QueryResult response;
|
||||
List<ApiToken> tokens = [];
|
||||
|
||||
try {
|
||||
final GraphQLClient client = await getClient();
|
||||
response = await client.query$GetApiTokens();
|
||||
if (response.hasException) {
|
||||
print(response.exception.toString());
|
||||
}
|
||||
tokens = response.data!['api']['devices']
|
||||
.map<ApiToken>((final e) => ApiToken.fromJson(e))
|
||||
.toList();
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
|
||||
return tokens;
|
||||
}
|
||||
|
||||
Future<void> switchService(final String uid, final bool needTurnOn) async {
|
||||
try {
|
||||
final GraphQLClient client = await getClient();
|
||||
if (needTurnOn) {
|
||||
final variables = Variables$Mutation$EnableService(serviceId: uid);
|
||||
final mutation = Options$Mutation$EnableService(variables: variables);
|
||||
await client.mutate$EnableService(mutation);
|
||||
} else {
|
||||
final variables = Variables$Mutation$DisableService(serviceId: uid);
|
||||
final mutation = Options$Mutation$DisableService(variables: variables);
|
||||
await client.mutate$DisableService(mutation);
|
||||
}
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
part of 'server.dart';
|
||||
|
||||
mixin ServerActionsApi on ApiMap {
|
||||
Future<bool> _commonBoolRequest(final Function graphQLMethod) async {
|
||||
QueryResult response;
|
||||
bool result = false;
|
||||
|
||||
try {
|
||||
response = await graphQLMethod();
|
||||
if (response.hasException) {
|
||||
print(response.exception.toString());
|
||||
result = false;
|
||||
} else {
|
||||
result = true;
|
||||
}
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Future<bool> reboot() async {
|
||||
try {
|
||||
final GraphQLClient client = await getClient();
|
||||
return await _commonBoolRequest(
|
||||
() async {
|
||||
await client.mutate$RebootSystem();
|
||||
},
|
||||
);
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> pullConfigurationUpdate() async {
|
||||
try {
|
||||
final GraphQLClient client = await getClient();
|
||||
return await _commonBoolRequest(
|
||||
() async {
|
||||
await client.mutate$PullRepositoryChanges();
|
||||
},
|
||||
);
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> upgrade() async {
|
||||
try {
|
||||
final GraphQLClient client = await getClient();
|
||||
return await _commonBoolRequest(
|
||||
() async {
|
||||
await client.mutate$RunSystemUpgrade();
|
||||
},
|
||||
);
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> apply() async {
|
||||
try {
|
||||
final GraphQLClient client = await getClient();
|
||||
await client.mutate$RunSystemRebuild();
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
part of 'server.dart';
|
||||
|
||||
mixin ServicesApi on ApiMap {}
|
59
lib/logic/api_maps/graphql_maps/server_api/volume_api.dart
Normal file
59
lib/logic/api_maps/graphql_maps/server_api/volume_api.dart
Normal file
|
@ -0,0 +1,59 @@
|
|||
part of 'server.dart';
|
||||
|
||||
mixin VolumeApi on ApiMap {
|
||||
Future<List<ServerDiskVolume>> getServerDiskVolumes() async {
|
||||
QueryResult response;
|
||||
List<ServerDiskVolume> volumes = [];
|
||||
|
||||
try {
|
||||
final GraphQLClient client = await getClient();
|
||||
response = await client.query$GetServerDiskVolumes();
|
||||
if (response.hasException) {
|
||||
print(response.exception.toString());
|
||||
}
|
||||
volumes = response.data!['storage']['volumes']
|
||||
.map<ServerDiskVolume>((final e) => ServerDiskVolume.fromJson(e))
|
||||
.toList();
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
|
||||
return volumes;
|
||||
}
|
||||
|
||||
Future<void> mountVolume(final String volumeName) async {
|
||||
try {
|
||||
final GraphQLClient client = await getClient();
|
||||
final variables = Variables$Mutation$MountVolume(name: volumeName);
|
||||
final mountVolumeMutation =
|
||||
Options$Mutation$MountVolume(variables: variables);
|
||||
await client.mutate$MountVolume(mountVolumeMutation);
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> unmountVolume(final String volumeName) async {
|
||||
try {
|
||||
final GraphQLClient client = await getClient();
|
||||
final variables = Variables$Mutation$UnmountVolume(name: volumeName);
|
||||
final unmountVolumeMutation =
|
||||
Options$Mutation$UnmountVolume(variables: variables);
|
||||
await client.mutate$UnmountVolume(unmountVolumeMutation);
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> resizeVolume(final String volumeName) async {
|
||||
try {
|
||||
final GraphQLClient client = await getClient();
|
||||
final variables = Variables$Mutation$ResizeVolume(name: volumeName);
|
||||
final resizeVolumeMutation =
|
||||
Options$Mutation$ResizeVolume(variables: variables);
|
||||
await client.mutate$ResizeVolume(resizeVolumeMutation);
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,7 +4,7 @@ import 'package:easy_localization/easy_localization.dart';
|
|||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:selfprivacy/config/get_it_config.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/graphql_maps/schema/server.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/graphql_maps/server_api/server.dart';
|
||||
import 'package:selfprivacy/logic/cubit/services/services_cubit.dart';
|
||||
import 'package:selfprivacy/logic/cubit/users/users_cubit.dart';
|
||||
import 'package:selfprivacy/logic/models/job.dart';
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import 'package:selfprivacy/config/get_it_config.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/graphql_maps/schema/server.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/graphql_maps/server_api/server.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/rest_maps/api_factory_creator.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/rest_maps/server_providers/server_provider_factory.dart';
|
||||
import 'package:selfprivacy/logic/common_enum/common_enum.dart';
|
||||
|
|
|
@ -4,7 +4,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
|
|||
import 'package:easy_localization/easy_localization.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:selfprivacy/config/get_it_config.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/graphql_maps/schema/server.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/graphql_maps/server_api/server.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/rest_maps/dns_providers/dns_provider_factory.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/rest_maps/provider_api_settings.dart';
|
||||
import 'package:selfprivacy/logic/models/hive/backblaze_credential.dart';
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import 'package:selfprivacy/logic/api_maps/graphql_maps/schema/server.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/graphql_maps/server_api/server.dart';
|
||||
import 'package:selfprivacy/logic/common_enum/common_enum.dart';
|
||||
import 'package:selfprivacy/logic/cubit/app_config_dependent/authentication_dependend_cubit.dart';
|
||||
import 'package:selfprivacy/logic/models/json/server_disk_volume.dart';
|
||||
|
|
|
@ -1,9 +1,24 @@
|
|||
import 'package:easy_localization/easy_localization.dart';
|
||||
|
||||
class DiskSize {
|
||||
DiskSize({final this.byte = 0});
|
||||
|
||||
int byte;
|
||||
|
||||
double asKb() => byte / 1024.0;
|
||||
double asMb() => byte / 1024.0 / 1024.0;
|
||||
double asGb() => byte / 1024.0 / 1024.0 / 1024.0;
|
||||
|
||||
int byte;
|
||||
@override
|
||||
String toString() {
|
||||
if (byte < 1024) {
|
||||
return '${byte.toStringAsFixed(0)} ${tr('bytes')}';
|
||||
} else if (byte < 1024 * 1024) {
|
||||
return 'providers.storage.kb'.tr(args: [asKb().toStringAsFixed(1)]);
|
||||
} else if (byte < 1024 * 1024 * 1024) {
|
||||
return 'providers.storage.mb'.tr(args: [asMb().toStringAsFixed(1)]);
|
||||
} else {
|
||||
return 'providers.storage.gb'.tr(args: [asGb().toStringAsFixed(1)]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,16 +24,12 @@ class ServerJob {
|
|||
final String description;
|
||||
final String status;
|
||||
final String uid;
|
||||
@JsonKey(name: 'updated_at')
|
||||
final String updatedAt;
|
||||
@JsonKey(name: 'created_at')
|
||||
final DateTime createdAt;
|
||||
|
||||
final String? error;
|
||||
final int? progress;
|
||||
final String? result;
|
||||
@JsonKey(name: 'status_text')
|
||||
final String? statusText;
|
||||
@JsonKey(name: 'finished_at')
|
||||
final String? finishedAt;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:selfprivacy/config/brand_theme.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/graphql_maps/schema/server.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/graphql_maps/server_api/server.dart';
|
||||
import 'package:selfprivacy/ui/components/brand_divider/brand_divider.dart';
|
||||
import 'package:selfprivacy/ui/components/brand_header/brand_header.dart';
|
||||
import 'package:selfprivacy/ui/components/brand_text/brand_text.dart';
|
||||
|
|
Loading…
Reference in a new issue