/ping to validate the endpoint

This commit is contained in:
vishnukvmd 2024-03-07 12:51:18 +05:30
parent 981e3866d3
commit 7bb65af482
3 changed files with 20 additions and 8 deletions

View file

@ -409,11 +409,10 @@
"waitingForBrowserRequest": "Waiting for browser request...",
"launchPasskeyUrlAgain": "Launch passkey URL again",
"passkey": "Passkey",
"developerMode":"Developer mode",
"developerModeWarning":"Are you sure that you want to modify Developer settings?",
"developerSettingsWarning":"Are you sure that you want to modify Developer settings?",
"developerSettings": "Developer settings",
"serverEndpoint": "Server endpoint",
"invalidURL": "Invalid URL",
"invalidURLMessage": "Sorry, the URL you entered is invalid. Please enter a valid URL and try again.",
"invalidEndpoint": "Invalid endpoint",
"invalidEndpointMessage": "Sorry, the endpoint you entered is invalid. Please enter a valid endpoint and try again.",
"endpointUpdatedMessage": "Endpoint updated successfully"
}

View file

@ -68,9 +68,9 @@ class _OnboardingPageState extends State<OnboardingPage> {
_developerModeTapCount = 0;
final result = await showChoiceDialog(
context,
title: l10n.developerMode,
title: l10n.developerSettings,
firstButtonLabel: l10n.yes,
body: l10n.developerModeWarning,
body: l10n.developerSettingsWarning,
isDismissible: false,
);
if (result?.action == ButtonAction.first) {

View file

@ -1,3 +1,4 @@
import 'package:dio/dio.dart';
import 'package:ente_auth/core/configuration.dart';
import 'package:ente_auth/l10n/l10n.dart';
import 'package:ente_auth/ui/common/gradient_button.dart';
@ -52,6 +53,7 @@ class _DeveloperSettingsPageState extends State<DeveloperSettingsPage> {
try {
final uri = Uri.parse(url);
if ((uri.scheme == "http" || uri.scheme == "https")) {
await _ping(url);
await Configuration.instance.setHttpEndpoint(url);
showToast(context, context.l10n.endpointUpdatedMessage);
Navigator.of(context).pop();
@ -61,8 +63,8 @@ class _DeveloperSettingsPageState extends State<DeveloperSettingsPage> {
} catch (e) {
showErrorDialog(
context,
context.l10n.invalidURL,
context.l10n.invalidURLMessage,
context.l10n.invalidEndpoint,
context.l10n.invalidEndpointMessage,
);
}
},
@ -73,4 +75,15 @@ class _DeveloperSettingsPageState extends State<DeveloperSettingsPage> {
),
);
}
Future<void> _ping(String endpoint) async {
try {
final response = await Dio().get(endpoint + '/ping');
if (response.data['message'] != 'pong') {
throw Exception('Invalid response');
}
} catch (e) {
throw Exception('Error occurred: $e');
}
}
}