Skip to content

Commit

Permalink
Fix save v5
Browse files Browse the repository at this point in the history
  • Loading branch information
tsutsu3 committed Jan 7, 2025
1 parent 8d5ac13 commit 7215ae0
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lib/gateways/api_gateway_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ abstract interface class ApiGateway {
///
/// ### Returns
/// - A [LoginQueryResponse] object containing the result of the login query.
Future<LoginQueryResponse> loginQuery();
Future<LoginQueryResponse> loginQuery({Server? server});

/// Fetches real-time status information from a Pi-hole server.
///
Expand Down
18 changes: 10 additions & 8 deletions lib/gateways/v5/api_gateway_v5.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<LoginQueryResponse> loginQuery() async {
Future<LoginQueryResponse> 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) {
Expand All @@ -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) {
Expand Down
18 changes: 10 additions & 8 deletions lib/gateways/v6/api_gateway_v6.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,16 @@ class ApiGatewayV6 implements ApiGateway {

/// Handles the login process to a Pi-hole server using its API.
@override
Future<LoginQueryResponse> loginQuery() async {
Future<LoginQueryResponse> 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) {
Expand All @@ -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');
Expand All @@ -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 =
Expand Down
4 changes: 3 additions & 1 deletion lib/screens/home/home_appbar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 11 additions & 4 deletions lib/screens/servers/add_server_fullscreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,17 @@ class _AddServerFullscreenState extends State<AddServerFullscreen> {
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,
Expand Down Expand Up @@ -322,8 +328,9 @@ class _AddServerFullscreenState extends State<AddServerFullscreen> {
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,
Expand Down
4 changes: 3 additions & 1 deletion lib/screens/servers/servers_tile_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ class _ServersTileItemState extends State<ServersTileItem>
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) {
Expand Down

0 comments on commit 7215ae0

Please sign in to comment.