mirror of
https://git.selfprivacy.org/kherel/selfprivacy.org.app.git
synced 2025-01-23 01:06:44 +00:00
Catch cloudflare exceptions
This commit is contained in:
parent
06fbcff9a9
commit
e4bb35d5d8
|
@ -76,22 +76,23 @@ class CloudflareApi extends DnsProviderApi {
|
|||
}
|
||||
|
||||
@override
|
||||
Future<String> getZoneId(final String domain) async {
|
||||
validateStatus = (final status) =>
|
||||
status == HttpStatus.ok || status == HttpStatus.forbidden;
|
||||
Future<String?> getZoneId(final String domain) async {
|
||||
String? zoneId;
|
||||
|
||||
final Dio client = await getClient();
|
||||
final Response response = await client.get(
|
||||
'/zones',
|
||||
queryParameters: {'name': domain},
|
||||
);
|
||||
|
||||
close(client);
|
||||
|
||||
if (response.data['result'].isEmpty) {
|
||||
throw DomainNotFoundException('No domains found');
|
||||
} else {
|
||||
return response.data['result'][0]['id'];
|
||||
try {
|
||||
final Response response = await client.get(
|
||||
'/zones',
|
||||
queryParameters: {'name': domain},
|
||||
);
|
||||
zoneId = response.data['result'][0]['id'];
|
||||
} catch (e) {
|
||||
print(e);
|
||||
} finally {
|
||||
close(client);
|
||||
}
|
||||
|
||||
return zoneId;
|
||||
}
|
||||
|
||||
@override
|
||||
|
@ -105,21 +106,25 @@ class CloudflareApi extends DnsProviderApi {
|
|||
final String url = '/zones/$domainZoneId/dns_records';
|
||||
|
||||
final Dio client = await getClient();
|
||||
final Response response = await client.get(url);
|
||||
try {
|
||||
final Response response = await client.get(url);
|
||||
|
||||
final List records = response.data['result'] ?? [];
|
||||
final List<Future> allDeleteFutures = <Future>[];
|
||||
final List records = response.data['result'] ?? [];
|
||||
final List<Future> allDeleteFutures = <Future>[];
|
||||
|
||||
for (final record in records) {
|
||||
if (record['zone_name'] == domainName) {
|
||||
allDeleteFutures.add(
|
||||
client.delete('$url/${record["id"]}'),
|
||||
);
|
||||
for (final record in records) {
|
||||
if (record['zone_name'] == domainName) {
|
||||
allDeleteFutures.add(
|
||||
client.delete('$url/${record["id"]}'),
|
||||
);
|
||||
}
|
||||
}
|
||||
await Future.wait(allDeleteFutures);
|
||||
} catch (e) {
|
||||
print(e);
|
||||
} finally {
|
||||
close(client);
|
||||
}
|
||||
|
||||
await Future.wait(allDeleteFutures);
|
||||
close(client);
|
||||
}
|
||||
|
||||
@override
|
||||
|
@ -252,27 +257,38 @@ class CloudflareApi extends DnsProviderApi {
|
|||
);
|
||||
|
||||
final Dio client = await getClient();
|
||||
await client.post(
|
||||
url,
|
||||
data: dkimRecord.toJson(),
|
||||
);
|
||||
|
||||
client.close();
|
||||
try {
|
||||
await client.post(
|
||||
url,
|
||||
data: dkimRecord.toJson(),
|
||||
);
|
||||
} catch (e) {
|
||||
print(e);
|
||||
} finally {
|
||||
close(client);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<String>> domainList() async {
|
||||
final String url = '$rootAddress/zones';
|
||||
List<String> domains = [];
|
||||
|
||||
final Dio client = await getClient();
|
||||
try {
|
||||
final Response response = await client.get(
|
||||
url,
|
||||
queryParameters: {'per_page': 50},
|
||||
);
|
||||
domains = response.data['result']
|
||||
.map<String>((final el) => el['name'] as String)
|
||||
.toList();
|
||||
} catch (e) {
|
||||
print(e);
|
||||
} finally {
|
||||
close(client);
|
||||
}
|
||||
|
||||
final Response response = await client.get(
|
||||
url,
|
||||
queryParameters: {'per_page': 50},
|
||||
);
|
||||
|
||||
close(client);
|
||||
return response.data['result']
|
||||
.map<String>((final el) => el['name'] as String)
|
||||
.toList();
|
||||
return domains;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@ abstract class DnsProviderApi extends ApiMap {
|
|||
final String dkimRecordString,
|
||||
final ServerDomain domain,
|
||||
);
|
||||
Future<String> getZoneId(final String domain);
|
||||
Future<String?> getZoneId(final String domain);
|
||||
Future<List<String>> domainList();
|
||||
|
||||
Future<bool> isApiTokenValid(final String token);
|
||||
|
|
|
@ -31,19 +31,21 @@ class DomainSetupCubit extends Cubit<DomainSetupState> {
|
|||
|
||||
emit(Loading(LoadingTypes.saving));
|
||||
|
||||
final String zoneId = await serverInstallationCubit
|
||||
final String? zoneId = await serverInstallationCubit
|
||||
.repository.dnsProviderApiFactory!
|
||||
.getDnsProvider()
|
||||
.getZoneId(domainName);
|
||||
|
||||
final ServerDomain domain = ServerDomain(
|
||||
domainName: domainName,
|
||||
zoneId: zoneId,
|
||||
provider: DnsProvider.cloudflare,
|
||||
);
|
||||
if (zoneId != null) {
|
||||
final ServerDomain domain = ServerDomain(
|
||||
domainName: domainName,
|
||||
zoneId: zoneId,
|
||||
provider: DnsProvider.cloudflare,
|
||||
);
|
||||
|
||||
serverInstallationCubit.setDomain(domain);
|
||||
emit(DomainSet());
|
||||
serverInstallationCubit.setDomain(domain);
|
||||
emit(DomainSet());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -169,12 +169,8 @@ class ServerInstallationRepository {
|
|||
),
|
||||
);
|
||||
|
||||
try {
|
||||
final String domainId = await dnsProviderApi.getZoneId(domain);
|
||||
return domainId;
|
||||
} on DomainNotFoundException {
|
||||
return null;
|
||||
}
|
||||
final String? domainId = await dnsProviderApi.getZoneId(domain);
|
||||
return domainId;
|
||||
}
|
||||
|
||||
Future<Map<String, bool>> isDnsAddressesMatch(
|
||||
|
|
Loading…
Reference in a new issue