mirror of
https://git.selfprivacy.org/kherel/selfprivacy.org.app.git
synced 2025-01-08 00:51:20 +00:00
refactor: Remove low level ApiController and replace Dns interfaces
This commit is contained in:
parent
5cbf399a58
commit
72a04a54b3
|
@ -1,21 +0,0 @@
|
|||
import 'package:selfprivacy/logic/api_maps/rest_maps/api_factory_creator.dart';
|
||||
import 'package:selfprivacy/logic/providers/provider_settings.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/rest_maps/dns_providers/dns_provider_factory.dart';
|
||||
|
||||
class ApiController {
|
||||
static DnsProviderApiFactory? get currentDnsProviderApiFactory =>
|
||||
_dnsProviderApiFactory;
|
||||
|
||||
static void initDnsProviderApiFactory(
|
||||
final DnsProviderSettings settings,
|
||||
) {
|
||||
_dnsProviderApiFactory =
|
||||
ApiFactoryCreator.createDnsProviderApiFactory(settings);
|
||||
}
|
||||
|
||||
static void clearProviderApiFactories() {
|
||||
_dnsProviderApiFactory = null;
|
||||
}
|
||||
|
||||
static DnsProviderApiFactory? _dnsProviderApiFactory;
|
||||
}
|
|
@ -1,57 +0,0 @@
|
|||
import 'package:selfprivacy/logic/providers/provider_settings.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/rest_maps/dns_providers/cloudflare/cloudflare_factory.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/rest_maps/dns_providers/digital_ocean_dns/digital_ocean_dns_factory.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/rest_maps/dns_providers/dns_provider_factory.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/rest_maps/server_providers/digital_ocean/digital_ocean_api_factory.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/rest_maps/server_providers/hetzner/hetzner_api_factory.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/rest_maps/server_providers/server_provider_factory.dart';
|
||||
import 'package:selfprivacy/logic/models/hive/server_details.dart';
|
||||
import 'package:selfprivacy/logic/models/hive/server_domain.dart';
|
||||
|
||||
class UnknownApiProviderException implements Exception {
|
||||
UnknownApiProviderException(this.message);
|
||||
final String message;
|
||||
}
|
||||
|
||||
class ApiFactoryCreator {
|
||||
static ServerProviderApiFactory createServerProviderApiFactory(
|
||||
final ServerProviderSettings settings,
|
||||
) {
|
||||
switch (settings.provider) {
|
||||
case ServerProviderType.hetzner:
|
||||
return HetznerApiFactory(region: settings.location);
|
||||
case ServerProviderType.digitalOcean:
|
||||
return DigitalOceanApiFactory(region: settings.location);
|
||||
case ServerProviderType.unknown:
|
||||
throw UnknownApiProviderException('Unknown server provider');
|
||||
}
|
||||
}
|
||||
|
||||
static DnsProviderApiFactory createDnsProviderApiFactory(
|
||||
final DnsProviderSettings settings,
|
||||
) {
|
||||
switch (settings.provider) {
|
||||
case DnsProviderType.cloudflare:
|
||||
return CloudflareApiFactory();
|
||||
case DnsProviderType.digitalOcean:
|
||||
return DigitalOceanDnsApiFactory();
|
||||
case DnsProviderType.unknown:
|
||||
throw UnknownApiProviderException('Unknown DNS provider');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class VolumeApiFactoryCreator {
|
||||
static VolumeProviderApiFactory createVolumeProviderApiFactory(
|
||||
final ServerProviderSettings settings,
|
||||
) {
|
||||
switch (settings.provider) {
|
||||
case ServerProviderType.hetzner:
|
||||
return HetznerApiFactory();
|
||||
case ServerProviderType.digitalOcean:
|
||||
return DigitalOceanApiFactory();
|
||||
case ServerProviderType.unknown:
|
||||
throw UnknownApiProviderException('Unknown volume provider');
|
||||
}
|
||||
}
|
||||
}
|
|
@ -7,7 +7,6 @@ import 'package:selfprivacy/logic/api_maps/rest_maps/server_providers/server_pro
|
|||
import 'package:selfprivacy/logic/api_maps/staging_options.dart';
|
||||
import 'package:selfprivacy/logic/models/disk_size.dart';
|
||||
import 'package:selfprivacy/logic/models/hive/user.dart';
|
||||
import 'package:selfprivacy/logic/models/price.dart';
|
||||
import 'package:selfprivacy/logic/models/server_provider_location.dart';
|
||||
import 'package:selfprivacy/utils/password_generator.dart';
|
||||
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
import 'package:cubit_form/cubit_form.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/rest_maps/api_controller.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/rest_maps/dns_providers/dns_provider.dart';
|
||||
import 'package:selfprivacy/logic/cubit/app_config_dependent/authentication_dependend_cubit.dart';
|
||||
import 'package:selfprivacy/logic/models/hive/server_domain.dart';
|
||||
import 'package:selfprivacy/logic/models/json/dns_records.dart';
|
||||
|
||||
import 'package:selfprivacy/logic/api_maps/graphql_maps/server_api/server_api.dart';
|
||||
import 'package:selfprivacy/logic/providers/providers_controller.dart';
|
||||
import 'package:selfprivacy/utils/network_utils.dart';
|
||||
|
||||
part 'dns_records_state.dart';
|
||||
|
@ -25,14 +25,12 @@ class DnsRecordsCubit
|
|||
emit(
|
||||
DnsRecordsState(
|
||||
dnsState: DnsRecordsStatus.refreshing,
|
||||
dnsRecords: ApiController.currentDnsProviderApiFactory!
|
||||
.getDnsProvider()
|
||||
.getDesiredDnsRecords(
|
||||
domainName:
|
||||
serverInstallationCubit.state.serverDomain?.domainName,
|
||||
dkimPublicKey: '',
|
||||
ipAddress: '',
|
||||
),
|
||||
dnsRecords:
|
||||
ProvidersController.currentDnsProvider!.getDesiredDnsRecords(
|
||||
domainName: serverInstallationCubit.state.serverDomain?.domainName,
|
||||
dkimPublicKey: '',
|
||||
ipAddress: '',
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
|
@ -41,20 +39,17 @@ class DnsRecordsCubit
|
|||
final String? ipAddress =
|
||||
serverInstallationCubit.state.serverDetails?.ip4;
|
||||
if (domain != null && ipAddress != null) {
|
||||
final List<DnsRecord> records = await ApiController
|
||||
.currentDnsProviderApiFactory!
|
||||
.getDnsProvider()
|
||||
final List<DnsRecord> records = await ProvidersController
|
||||
.currentDnsProvider!
|
||||
.getDnsRecords(domain: domain);
|
||||
final String? dkimPublicKey =
|
||||
extractDkimRecord(await api.getDnsRecords())?.content;
|
||||
final List<DesiredDnsRecord> desiredRecords = ApiController
|
||||
.currentDnsProviderApiFactory!
|
||||
.getDnsProvider()
|
||||
.getDesiredDnsRecords(
|
||||
domainName: domain.domainName,
|
||||
ipAddress: ipAddress,
|
||||
dkimPublicKey: dkimPublicKey,
|
||||
);
|
||||
final List<DesiredDnsRecord> desiredRecords =
|
||||
ProvidersController.currentDnsProvider!.getDesiredDnsRecords(
|
||||
domainName: domain.domainName,
|
||||
ipAddress: ipAddress,
|
||||
dkimPublicKey: dkimPublicKey,
|
||||
);
|
||||
final List<DesiredDnsRecord> foundRecords = [];
|
||||
for (final DesiredDnsRecord desiredRecord in desiredRecords) {
|
||||
if (desiredRecord.description == 'record.dkim') {
|
||||
|
@ -128,10 +123,10 @@ class DnsRecordsCubit
|
|||
emit(state.copyWith(dnsState: DnsRecordsStatus.refreshing));
|
||||
final ServerDomain? domain = serverInstallationCubit.state.serverDomain;
|
||||
final String? ipAddress = serverInstallationCubit.state.serverDetails?.ip4;
|
||||
final DnsProviderApi dnsProviderApi =
|
||||
ApiController.currentDnsProviderApiFactory!.getDnsProvider();
|
||||
await dnsProviderApi.removeSimilarRecords(domain: domain!);
|
||||
await dnsProviderApi.createMultipleDnsRecords(
|
||||
await ProvidersController.currentDnsProvider!.removeSimilarRecords(
|
||||
domain: domain!,
|
||||
);
|
||||
await ProvidersController.currentDnsProvider!.createMultipleDnsRecords(
|
||||
domain: domain,
|
||||
ip4: ipAddress,
|
||||
);
|
||||
|
@ -139,7 +134,10 @@ class DnsRecordsCubit
|
|||
final List<DnsRecord> records = await api.getDnsRecords();
|
||||
final DnsRecord? dkimRecord = extractDkimRecord(records);
|
||||
if (dkimRecord != null) {
|
||||
await dnsProviderApi.setDnsRecord(dkimRecord, domain);
|
||||
await ProvidersController.currentDnsProvider!.setDnsRecord(
|
||||
dkimRecord,
|
||||
domain,
|
||||
);
|
||||
}
|
||||
|
||||
await load();
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import 'package:cubit_form/cubit_form.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/rest_maps/api_controller.dart';
|
||||
import 'package:selfprivacy/logic/cubit/server_installation/server_installation_cubit.dart';
|
||||
import 'package:selfprivacy/logic/models/hive/server_domain.dart';
|
||||
import 'package:selfprivacy/logic/providers/providers_controller.dart';
|
||||
|
||||
class DomainSetupCubit extends Cubit<DomainSetupState> {
|
||||
DomainSetupCubit(this.serverInstallationCubit) : super(Initial());
|
||||
|
@ -10,9 +10,8 @@ class DomainSetupCubit extends Cubit<DomainSetupState> {
|
|||
|
||||
Future<void> load() async {
|
||||
emit(Loading(LoadingTypes.loadingDomain));
|
||||
final List<String> list = await ApiController.currentDnsProviderApiFactory!
|
||||
.getDnsProvider()
|
||||
.domainList();
|
||||
final List<String> list =
|
||||
await ProvidersController.currentDnsProvider!.domainList();
|
||||
if (list.isEmpty) {
|
||||
emit(Empty());
|
||||
} else if (list.length == 1) {
|
||||
|
@ -28,9 +27,8 @@ class DomainSetupCubit extends Cubit<DomainSetupState> {
|
|||
|
||||
emit(Loading(LoadingTypes.saving));
|
||||
|
||||
final String? zoneId = await ApiController.currentDnsProviderApiFactory!
|
||||
.getDnsProvider()
|
||||
.getZoneId(domainName);
|
||||
final String? zoneId =
|
||||
await ProvidersController.currentDnsProvider!.getZoneId(domainName);
|
||||
|
||||
if (zoneId != null) {
|
||||
final ServerDomain domain = ServerDomain(
|
||||
|
|
|
@ -5,11 +5,9 @@ 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/server_api/server_api.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/rest_maps/api_controller.dart';
|
||||
import 'package:selfprivacy/logic/models/callback_dialogue_branching.dart';
|
||||
import 'package:selfprivacy/logic/models/launch_installation_data.dart';
|
||||
import 'package:selfprivacy/logic/providers/provider_settings.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/rest_maps/dns_providers/dns_provider_api_settings.dart';
|
||||
import 'package:selfprivacy/logic/providers/providers_controller.dart';
|
||||
import 'package:selfprivacy/logic/models/hive/backblaze_credential.dart';
|
||||
import 'package:selfprivacy/logic/models/hive/server_details.dart';
|
||||
|
@ -68,7 +66,7 @@ class ServerInstallationCubit extends Cubit<ServerInstallationState> {
|
|||
|
||||
void setDnsProviderType(final DnsProviderType providerType) async {
|
||||
await repository.saveDnsProviderType(providerType);
|
||||
ApiController.initDnsProviderApiFactory(
|
||||
ProvidersController.initDnsProvider(
|
||||
DnsProviderSettings(
|
||||
provider: providerType,
|
||||
),
|
||||
|
@ -97,11 +95,9 @@ class ServerInstallationCubit extends Cubit<ServerInstallationState> {
|
|||
final String providerToken,
|
||||
) async {
|
||||
final GenericResult<bool> apiResponse =
|
||||
await ApiController.currentDnsProviderApiFactory!
|
||||
.getDnsProvider(
|
||||
settings: const DnsProviderApiSettings(isWithToken: false),
|
||||
)
|
||||
.isApiTokenValid(providerToken);
|
||||
await ProvidersController.currentDnsProvider!.tryInitApiByToken(
|
||||
providerToken,
|
||||
);
|
||||
|
||||
if (!apiResponse.success) {
|
||||
getIt<NavigationService>().showSnackBar(
|
||||
|
@ -250,8 +246,7 @@ class ServerInstallationCubit extends Cubit<ServerInstallationState> {
|
|||
serverTypeId: state.serverTypeIdentificator!,
|
||||
errorCallback: clearAppConfig,
|
||||
successCallback: onCreateServerSuccess,
|
||||
dnsProviderApi:
|
||||
ApiController.currentDnsProviderApiFactory!.getDnsProvider(),
|
||||
dnsProvider: ProvidersController.currentDnsProvider!,
|
||||
);
|
||||
|
||||
final result =
|
||||
|
@ -754,7 +749,6 @@ class ServerInstallationCubit extends Cubit<ServerInstallationState> {
|
|||
|
||||
void clearAppConfig() {
|
||||
closeTimer();
|
||||
ApiController.clearProviderApiFactories();
|
||||
ProvidersController.clearProviders();
|
||||
repository.clearAppConfig();
|
||||
emit(const ServerInstallationEmpty());
|
||||
|
|
|
@ -10,12 +10,10 @@ import 'package:hive/hive.dart';
|
|||
import 'package:pub_semver/pub_semver.dart';
|
||||
import 'package:selfprivacy/config/get_it_config.dart';
|
||||
import 'package:selfprivacy/config/hive_config.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/rest_maps/api_controller.dart';
|
||||
import 'package:selfprivacy/logic/providers/provider_settings.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/rest_maps/dns_providers/dns_provider.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/rest_maps/dns_providers/dns_provider_api_settings.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/graphql_maps/server_api/server_api.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/rest_maps/server_providers/server_provider.dart';
|
||||
import 'package:selfprivacy/logic/cubit/server_installation/server_installation_cubit.dart';
|
||||
import 'package:selfprivacy/logic/models/hive/backblaze_credential.dart';
|
||||
import 'package:selfprivacy/logic/models/hive/server_details.dart';
|
||||
|
@ -72,7 +70,7 @@ class ServerInstallationRepository {
|
|||
if (dnsProvider != null ||
|
||||
(serverDomain != null &&
|
||||
serverDomain.provider != ServerProviderType.unknown)) {
|
||||
ApiController.initDnsProviderApiFactory(
|
||||
ProvidersController.initDnsProvider(
|
||||
DnsProviderSettings(
|
||||
provider: dnsProvider ?? serverDomain!.provider,
|
||||
),
|
||||
|
@ -239,8 +237,6 @@ class ServerInstallationRepository {
|
|||
final ServerDomain domain, {
|
||||
required final void Function() onCancel,
|
||||
}) async {
|
||||
final DnsProviderApi dnsProviderApi =
|
||||
ApiController.currentDnsProviderApiFactory!.getDnsProvider();
|
||||
final serverProvider = ProvidersController.currentServerProvider!;
|
||||
|
||||
void showDomainErrorPopUp(final String error) {
|
||||
|
@ -259,7 +255,7 @@ class ServerInstallationRepository {
|
|||
}
|
||||
|
||||
final GenericResult removingResult =
|
||||
await dnsProviderApi.removeSimilarRecords(
|
||||
await ProvidersController.currentDnsProvider!.removeSimilarRecords(
|
||||
ip4: serverDetails.ip4,
|
||||
domain: domain,
|
||||
);
|
||||
|
@ -272,8 +268,9 @@ class ServerInstallationRepository {
|
|||
bool createdSuccessfully = false;
|
||||
String errorMessage = 'domain.error'.tr();
|
||||
try {
|
||||
final GenericResult createResult =
|
||||
await dnsProviderApi.createMultipleDnsRecords(
|
||||
final GenericResult createResult = await ProvidersController
|
||||
.currentDnsProvider!
|
||||
.createMultipleDnsRecords(
|
||||
ip4: serverDetails.ip4,
|
||||
domain: domain,
|
||||
);
|
||||
|
@ -293,8 +290,6 @@ class ServerInstallationRepository {
|
|||
}
|
||||
|
||||
Future<void> createDkimRecord(final ServerDomain cloudFlareDomain) async {
|
||||
final DnsProviderApi dnsProviderApi =
|
||||
ApiController.currentDnsProviderApiFactory!.getDnsProvider();
|
||||
final ServerApi api = ServerApi();
|
||||
|
||||
late DnsRecord record;
|
||||
|
@ -305,7 +300,10 @@ class ServerInstallationRepository {
|
|||
rethrow;
|
||||
}
|
||||
|
||||
await dnsProviderApi.setDnsRecord(record, cloudFlareDomain);
|
||||
await ProvidersController.currentDnsProvider!.setDnsRecord(
|
||||
record,
|
||||
cloudFlareDomain,
|
||||
);
|
||||
}
|
||||
|
||||
Future<bool> isHttpServerWorking() async {
|
||||
|
@ -677,9 +675,8 @@ class ServerInstallationRepository {
|
|||
await box.put(BNames.isLoading, false);
|
||||
await box.put(BNames.serverDetails, null);
|
||||
|
||||
final GenericResult<void> removalResult = await ApiController
|
||||
.currentDnsProviderApiFactory!
|
||||
.getDnsProvider()
|
||||
final GenericResult<void> removalResult = await ProvidersController
|
||||
.currentDnsProvider!
|
||||
.removeSimilarRecords(domain: serverDomain);
|
||||
|
||||
if (!removalResult.success) {
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
import 'package:selfprivacy/logic/api_maps/rest_maps/dns_providers/dns_provider.dart';
|
||||
import 'package:selfprivacy/logic/models/hive/server_details.dart';
|
||||
import 'package:selfprivacy/logic/models/hive/server_domain.dart';
|
||||
import 'package:selfprivacy/logic/models/hive/user.dart';
|
||||
import 'package:selfprivacy/logic/providers/dns_providers/dns_provider.dart';
|
||||
|
||||
class LaunchInstallationData {
|
||||
LaunchInstallationData({
|
||||
required this.rootUser,
|
||||
required this.dnsApiToken,
|
||||
required this.dnsProviderType,
|
||||
required this.dnsProviderApi,
|
||||
required this.dnsProvider,
|
||||
required this.serverDomain,
|
||||
required this.serverTypeId,
|
||||
required this.errorCallback,
|
||||
|
@ -19,7 +19,7 @@ class LaunchInstallationData {
|
|||
final String dnsApiToken;
|
||||
final ServerDomain serverDomain;
|
||||
final DnsProviderType dnsProviderType;
|
||||
final DnsProviderApi dnsProviderApi;
|
||||
final DnsProvider dnsProvider;
|
||||
final String serverTypeId;
|
||||
final Function() errorCallback;
|
||||
final Function(ServerHostingDetails details) successCallback;
|
||||
|
|
|
@ -580,11 +580,11 @@ class HetznerServerProvider extends ServerProvider {
|
|||
}
|
||||
|
||||
await installationData.successCallback(serverDetails);
|
||||
await installationData.dnsProviderApi.removeSimilarRecords(
|
||||
await installationData.dnsProvider.removeSimilarRecords(
|
||||
ip4: serverDetails.ip4,
|
||||
domain: installationData.serverDomain,
|
||||
);
|
||||
await installationData.dnsProviderApi.createMultipleDnsRecords(
|
||||
await installationData.dnsProvider.createMultipleDnsRecords(
|
||||
ip4: serverDetails.ip4,
|
||||
domain: installationData.serverDomain,
|
||||
);
|
||||
|
|
Loading…
Reference in a new issue