From 7215ae028506919f096412918f703996a20623d9 Mon Sep 17 00:00:00 2001 From: tsutsu3 Date: Tue, 7 Jan 2025 10:57:15 +0900 Subject: [PATCH] Fix save v5 --- lib/gateways/api_gateway_interface.dart | 2 +- lib/gateways/v5/api_gateway_v5.dart | 18 ++++++++++-------- lib/gateways/v6/api_gateway_v6.dart | 18 ++++++++++-------- lib/screens/home/home_appbar.dart | 4 +++- lib/screens/servers/add_server_fullscreen.dart | 15 +++++++++++---- lib/screens/servers/servers_tile_item.dart | 4 +++- 6 files changed, 38 insertions(+), 23 deletions(-) diff --git a/lib/gateways/api_gateway_interface.dart b/lib/gateways/api_gateway_interface.dart index 8b98a33..91131fb 100644 --- a/lib/gateways/api_gateway_interface.dart +++ b/lib/gateways/api_gateway_interface.dart @@ -10,7 +10,7 @@ abstract interface class ApiGateway { /// /// ### Returns /// - A [LoginQueryResponse] object containing the result of the login query. - Future loginQuery(); + Future loginQuery({Server? server}); /// Fetches real-time status information from a Pi-hole server. /// diff --git a/lib/gateways/v5/api_gateway_v5.dart b/lib/gateways/v5/api_gateway_v5.dart index 3f4f266..5654598 100644 --- a/lib/gateways/v5/api_gateway_v5.dart +++ b/lib/gateways/v5/api_gateway_v5.dart @@ -115,15 +115,17 @@ class ApiGatewayV5 implements ApiGateway { /// 2. Toggles the Pi-hole's status between enabled and disabled depending on the current status. /// 3. Validates the response to determine the success or failure of the login attempt. @override - Future loginQuery() async { + Future loginQuery({Server? server}) async { + final targetServer = server ?? _server; + try { final status = await httpClient( method: 'get', url: - '${_server.address}/admin/api.php?auth=${_server.token}&summaryRaw', + '${targetServer.address}/admin/api.php?auth=${targetServer.token}&summaryRaw', basicAuth: { - 'username': _server.basicAuthUser, - 'password': _server.basicAuthPassword, + 'username': targetServer.basicAuthUser, + 'password': targetServer.basicAuthPassword, }, ); if (status.statusCode == 200) { @@ -133,11 +135,11 @@ class ApiGatewayV5 implements ApiGateway { final enableOrDisable = await httpClient( method: 'get', url: statusParsed['status'] == 'enabled' - ? '${_server.address}/admin/api.php?auth=${_server.token}&enable=0' - : '${_server.address}/admin/api.php?auth=${_server.token}&disable=0', + ? '${targetServer.address}/admin/api.php?auth=${targetServer.token}&enable=0' + : '${targetServer.address}/admin/api.php?auth=${targetServer.token}&disable=0', basicAuth: { - 'username': _server.basicAuthUser, - 'password': _server.basicAuthPassword, + 'username': targetServer.basicAuthUser, + 'password': targetServer.basicAuthPassword, }, ); if (enableOrDisable.statusCode == 200) { diff --git a/lib/gateways/v6/api_gateway_v6.dart b/lib/gateways/v6/api_gateway_v6.dart index 213947c..b26681d 100644 --- a/lib/gateways/v6/api_gateway_v6.dart +++ b/lib/gateways/v6/api_gateway_v6.dart @@ -102,14 +102,16 @@ class ApiGatewayV6 implements ApiGateway { /// Handles the login process to a Pi-hole server using its API. @override - Future loginQuery() async { + Future loginQuery({Server? server}) async { + final targetServer = server ?? _server; + try { // 1. Get DNS blocking status // If the session ID is already available, use it to get the status // Otherwise, re-login to get the session ID final enableOrDisable = await httpClient( method: 'get', - url: '${_server.address}/api/dns/blocking', + url: '${targetServer.address}/api/dns/blocking', maxRetries: 0, ); if (enableOrDisable.statusCode == 200) { @@ -119,11 +121,11 @@ class ApiGatewayV6 implements ApiGateway { return LoginQueryResponse( result: APiResponseType.success, status: enableOrDisableParsed.blocking, - sid: _server.sm.sid, + sid: targetServer.sm.sid, ); } - if (_server.sm.sid == null || _server.sm.sid!.isEmpty) { + if (targetServer.sm.sid == null || targetServer.sm.sid!.isEmpty) { logger.i('No session ID available, logging in'); } else { logger.i('Session ID is expired or deleted, logging in'); @@ -132,19 +134,19 @@ class ApiGatewayV6 implements ApiGateway { // 2.login final status = await httpClient( method: 'post', - url: '${_server.address}/api/auth', - body: {'password': await _server.sm.password}, + url: '${targetServer.address}/api/auth', + body: {'password': await targetServer.sm.password}, maxRetries: 0, ); if (status.statusCode == 200) { final statusParsed = Session.fromJson(jsonDecode(status.body)); - await _server.sm.save(statusParsed.session.sid); + await targetServer.sm.save(statusParsed.session.sid); // 3. Get DNS blocking status final enableOrDisable = await httpClient( method: 'get', - url: '${_server.address}/api/dns/blocking', + url: '${targetServer.address}/api/dns/blocking', ); if (enableOrDisable.statusCode == 200) { final enableOrDisableParsed = diff --git a/lib/screens/home/home_appbar.dart b/lib/screens/home/home_appbar.dart index 722a137..e8540e2 100644 --- a/lib/screens/home/home_appbar.dart +++ b/lib/screens/home/home_appbar.dart @@ -99,7 +99,9 @@ class HomeAppBar extends StatelessWidget implements PreferredSizeWidget { final ProcessModal process = ProcessModal(context: context); process.open(AppLocalizations.of(context)!.connecting); - final result = await serversProvider.loadApiGateway(server)?.loginQuery(); + final result = await serversProvider + .loadApiGateway(server) + ?.loginQuery(server: server); process.close(); if (result?.result == APiResponseType.success) { await connectSuccess(result); diff --git a/lib/screens/servers/add_server_fullscreen.dart b/lib/screens/servers/add_server_fullscreen.dart index 7bba81b..7fb100f 100644 --- a/lib/screens/servers/add_server_fullscreen.dart +++ b/lib/screens/servers/add_server_fullscreen.dart @@ -232,11 +232,17 @@ class _AddServerFullscreenState extends State { basicAuthPassword: basicAuthPassword.text, ); serverObj.sm.savePassword(passwordFieldController.text); - final result = - await serversProvider.loadApiGateway(serverObj)?.loginQuery(); + final result = await serversProvider + .loadApiGateway(serverObj) + ?.loginQuery(server: serverObj); if (!mounted) return; if (result?.result == APiResponseType.success) { Navigator.maybePop(context); + showSuccessSnackBar( + context: context, + appConfigProvider: appConfigProvider, + label: AppLocalizations.of(context)!.checkAddress, + ); serversProvider.addServer( Server( address: serverObj.address, @@ -322,8 +328,9 @@ class _AddServerFullscreenState extends State { basicAuthPassword: basicAuthPassword.text, ); serverObj.sm.savePassword(passwordFieldController.text); - final result = - await serversProvider.loadApiGateway(serverObj)?.loginQuery(); + final result = await serversProvider + .loadApiGateway(serverObj) + ?.loginQuery(server: serverObj); if (result?.result == APiResponseType.success) { Server server = Server( address: widget.server!.address, diff --git a/lib/screens/servers/servers_tile_item.dart b/lib/screens/servers/servers_tile_item.dart index 75baeaf..9288dc4 100644 --- a/lib/screens/servers/servers_tile_item.dart +++ b/lib/screens/servers/servers_tile_item.dart @@ -132,7 +132,9 @@ class _ServersTileItemState extends State final ProcessModal process = ProcessModal(context: context); process.open(AppLocalizations.of(context)!.connecting); - final result = await serversProvider.loadApiGateway(server)?.loginQuery(); + final result = await serversProvider + .loadApiGateway(server) + ?.loginQuery(server: server); if (result?.result == APiResponseType.success) { await connectSuccess(result); } else if (mounted) {