mirror of
https://git.selfprivacy.org/kherel/selfprivacy.org.app.git
synced 2025-03-11 17:24:09 +00:00
feat: Implement distinction for connection errors on dns provider page
Now user gets notified when connection error occurs
This commit is contained in:
parent
1df5f6594d
commit
e62e8bf916
4 changed files with 55 additions and 22 deletions
lib/logic
api_maps/rest_maps/dns_providers
cubit
forms/setup/initializing
server_installation
|
@ -46,33 +46,50 @@ class CloudflareApi extends DnsProviderApi {
|
||||||
String rootAddress = 'https://api.cloudflare.com/client/v4';
|
String rootAddress = 'https://api.cloudflare.com/client/v4';
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<bool> isApiTokenValid(final String token) async {
|
Future<APIGenericResult<bool>> isApiTokenValid(final String token) async {
|
||||||
bool isValid = false;
|
bool isValid = false;
|
||||||
Response? response;
|
Response? response;
|
||||||
|
String message = '';
|
||||||
final Dio client = await getClient();
|
final Dio client = await getClient();
|
||||||
try {
|
try {
|
||||||
response = await client.get(
|
response = await client.get(
|
||||||
'/user/tokens/verify',
|
'/user/tokens/verify',
|
||||||
options: Options(headers: {'Authorization': 'Bearer $token'}),
|
options: Options(
|
||||||
|
followRedirects: false,
|
||||||
|
validateStatus: (final status) =>
|
||||||
|
status != null && (status >= 200 || status == 401),
|
||||||
|
headers: {'Authorization': 'Bearer $token'},
|
||||||
|
),
|
||||||
);
|
);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print(e);
|
print(e);
|
||||||
isValid = false;
|
isValid = false;
|
||||||
|
message = e.toString();
|
||||||
} finally {
|
} finally {
|
||||||
close(client);
|
close(client);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (response != null) {
|
if (response == null) {
|
||||||
if (response.statusCode == HttpStatus.ok) {
|
return APIGenericResult(
|
||||||
isValid = true;
|
data: isValid,
|
||||||
} else if (response.statusCode == HttpStatus.unauthorized) {
|
success: false,
|
||||||
isValid = false;
|
message: message,
|
||||||
} else {
|
);
|
||||||
throw Exception('code: ${response.statusCode}');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return isValid;
|
if (response.statusCode == HttpStatus.ok) {
|
||||||
|
isValid = true;
|
||||||
|
} else if (response.statusCode == HttpStatus.unauthorized) {
|
||||||
|
isValid = false;
|
||||||
|
} else {
|
||||||
|
throw Exception('code: ${response.statusCode}');
|
||||||
|
}
|
||||||
|
|
||||||
|
return APIGenericResult(
|
||||||
|
data: isValid,
|
||||||
|
success: true,
|
||||||
|
message: response.statusMessage,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
|
import 'package:selfprivacy/logic/api_maps/api_generic_result.dart';
|
||||||
import 'package:selfprivacy/logic/api_maps/rest_maps/api_map.dart';
|
import 'package:selfprivacy/logic/api_maps/rest_maps/api_map.dart';
|
||||||
import 'package:selfprivacy/logic/models/hive/server_domain.dart';
|
import 'package:selfprivacy/logic/models/hive/server_domain.dart';
|
||||||
import 'package:selfprivacy/logic/models/json/dns_records.dart';
|
import 'package:selfprivacy/logic/models/json/dns_records.dart';
|
||||||
|
|
||||||
|
export 'package:selfprivacy/logic/api_maps/api_generic_result.dart';
|
||||||
|
|
||||||
class DomainNotFoundException implements Exception {
|
class DomainNotFoundException implements Exception {
|
||||||
DomainNotFoundException(this.message);
|
DomainNotFoundException(this.message);
|
||||||
final String message;
|
final String message;
|
||||||
|
@ -26,6 +29,6 @@ abstract class DnsProviderApi extends ApiMap {
|
||||||
Future<String?> getZoneId(final String domain);
|
Future<String?> getZoneId(final String domain);
|
||||||
Future<List<String>> domainList();
|
Future<List<String>> domainList();
|
||||||
|
|
||||||
Future<bool> isApiTokenValid(final String token);
|
Future<APIGenericResult<bool>> isApiTokenValid(final String token);
|
||||||
RegExp getApiTokenValidation();
|
RegExp getApiTokenValidation();
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,21 +28,24 @@ class DnsProviderFormCubit extends FormCubit {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
FutureOr<bool> asyncValidation() async {
|
FutureOr<bool> asyncValidation() async {
|
||||||
late bool isKeyValid;
|
bool? isKeyValid;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
isKeyValid = await initializingCubit
|
isKeyValid = await initializingCubit
|
||||||
.isDnsProviderApiTokenValid(apiKey.state.value);
|
.isDnsProviderApiTokenValid(apiKey.state.value);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
addError(e);
|
addError(e);
|
||||||
isKeyValid = false;
|
}
|
||||||
|
|
||||||
|
if (isKeyValid == null) {
|
||||||
|
apiKey.setError('');
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isKeyValid) {
|
if (!isKeyValid) {
|
||||||
apiKey.setError('initializing.cloudflare_bad_key_error'.tr());
|
apiKey.setError('initializing.cloudflare_bad_key_error'.tr());
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return isKeyValid;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -98,7 +98,7 @@ class ServerInstallationCubit extends Cubit<ServerInstallationState> {
|
||||||
return apiResponse.data;
|
return apiResponse.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<bool> isDnsProviderApiTokenValid(
|
Future<bool?> isDnsProviderApiTokenValid(
|
||||||
final String providerToken,
|
final String providerToken,
|
||||||
) async {
|
) async {
|
||||||
if (ApiController.currentDnsProviderApiFactory == null) {
|
if (ApiController.currentDnsProviderApiFactory == null) {
|
||||||
|
@ -111,11 +111,21 @@ class ServerInstallationCubit extends Cubit<ServerInstallationState> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ApiController.currentDnsProviderApiFactory!
|
final APIGenericResult<bool> apiResponse =
|
||||||
.getDnsProvider(
|
await ApiController.currentDnsProviderApiFactory!
|
||||||
settings: const DnsProviderApiSettings(isWithToken: false),
|
.getDnsProvider(
|
||||||
)
|
settings: const DnsProviderApiSettings(isWithToken: false),
|
||||||
.isApiTokenValid(providerToken);
|
)
|
||||||
|
.isApiTokenValid(providerToken);
|
||||||
|
|
||||||
|
if (!apiResponse.success) {
|
||||||
|
getIt<NavigationService>().showSnackBar(
|
||||||
|
'initializing.could_not_connect'.tr(),
|
||||||
|
);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return apiResponse.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<ServerProviderLocation>> fetchAvailableLocations() async {
|
Future<List<ServerProviderLocation>> fetchAvailableLocations() async {
|
||||||
|
|
Loading…
Add table
Reference in a new issue