-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[pihole] Integration of API v6 #19350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8841ce8
Initiating integration of API v6
clinique d429e35
Adds enable / disable blocking
clinique 4b8b331
Correction of warnings
clinique 2b5f5d8
Finalizing API v6 for translated values of the handler.
clinique 09987e9
Adding missing Relative field.
clinique File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
...ng.pihole/src/main/java/org/openhab/binding/pihole/internal/rest/JettyAdminServiceV6.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| /* | ||
| * Copyright (c) 2010-2025 Contributors to the openHAB project | ||
| * | ||
| * See the NOTICE file(s) distributed with this work for additional | ||
| * information. | ||
| * | ||
| * This program and the accompanying materials are made available under the | ||
| * terms of the Eclipse Public License 2.0 which is available at | ||
| * http://www.eclipse.org/legal/epl-2.0 | ||
| * | ||
| * SPDX-License-Identifier: EPL-2.0 | ||
| */ | ||
| package org.openhab.binding.pihole.internal.rest; | ||
|
|
||
| import static java.util.concurrent.TimeUnit.SECONDS; | ||
|
|
||
| import java.net.URI; | ||
| import java.util.Optional; | ||
|
|
||
| import org.eclipse.jdt.annotation.NonNullByDefault; | ||
| import org.eclipse.jdt.annotation.Nullable; | ||
| import org.eclipse.jetty.client.HttpClient; | ||
| import org.eclipse.jetty.client.util.StringContentProvider; | ||
| import org.eclipse.jetty.http.HttpHeader; | ||
| import org.eclipse.jetty.http.HttpMethod; | ||
| import org.openhab.binding.pihole.internal.PiHoleException; | ||
| import org.openhab.binding.pihole.internal.rest.model.DnsStatistics; | ||
| import org.openhab.binding.pihole.internal.rest.model.v6.Password; | ||
| import org.openhab.binding.pihole.internal.rest.model.v6.SessionAnswer; | ||
| import org.openhab.binding.pihole.internal.rest.model.v6.SessionAnswer.Session; | ||
| import org.openhab.binding.pihole.internal.rest.model.v6.StatAnswer; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import com.google.gson.Gson; | ||
|
|
||
| /** | ||
| * @author Martin Grzeslowski - Initial contribution | ||
magx2 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| @NonNullByDefault | ||
| public class JettyAdminServiceV6 extends AdminService { | ||
| private final Logger logger = LoggerFactory.getLogger(JettyAdminServiceV6.class); | ||
| protected final URI apiUrl; | ||
| private @Nullable Session session; | ||
|
|
||
| public JettyAdminServiceV6(String token, URI baseUrl, HttpClient client, Gson gson) { | ||
| super(token, client, gson); | ||
| apiUrl = baseUrl.resolve("/api"); | ||
| } | ||
|
|
||
| private void getAuth() throws PiHoleException { | ||
| logger.debug("Check if authentication is required"); | ||
| var authUrl = apiUrl.resolve(apiUrl.getPath() + "/auth"); | ||
| var request = client.newRequest(authUrl).timeout(TIMEOUT_SECONDS, SECONDS).method(HttpMethod.POST) | ||
| .header(HttpHeader.ACCEPT, "application/json") | ||
| .content(new StringContentProvider(gson.toJson(new Password(token)))); | ||
| var response = send(request); | ||
| var content = response.getContentAsString(); | ||
| SessionAnswer answer = gson.fromJson(content, SessionAnswer.class); | ||
| logger.debug(answer.session().message()); | ||
| session = answer.session(); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<DnsStatistics> summary() throws PiHoleException { | ||
| logger.debug("Getting summary"); | ||
| getAuth(); | ||
| var url = apiUrl.resolve(apiUrl.getPath() + "/stats/summary"); | ||
| var request = client.newRequest(url).timeout(TIMEOUT_SECONDS, SECONDS).method(HttpMethod.GET) | ||
| .header(HttpHeader.ACCEPT, "application/json").header("sid", session.sid()); | ||
| var response = send(request); | ||
| var content = response.getContentAsString(); | ||
| StatAnswer answer = gson.fromJson(content, StatAnswer.class); | ||
| DnsStatistics translated = new DnsStatistics(answer.gravity().domainsBeingBlocked(), null, null, null, | ||
| answer.queries().uniqueDomains(), answer.queries().forwarded(), answer.queries().cached(), null, null, | ||
| null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, | ||
| null, null); | ||
| return Optional.of(translated); | ||
| } | ||
|
|
||
| @Override | ||
| public void disableBlocking(long seconds) throws PiHoleException { | ||
| logger.debug("Disabling blocking for {} seconds", seconds); | ||
| // var url = baseUrl.resolve("/admin/api.php?disable=%s&auth=%s".formatted(seconds, token)); | ||
| // var request = client.newRequest(url).timeout(TIMEOUT_SECONDS, SECONDS); | ||
| // send(request); | ||
| } | ||
|
|
||
| @Override | ||
| public void enableBlocking() throws PiHoleException { | ||
| logger.debug("Enabling blocking"); | ||
| // var url = baseUrl.resolve("/admin/api.php?enable&auth=%s".formatted(token)); | ||
| // var request = client.newRequest(url).timeout(TIMEOUT_SECONDS, SECONDS); | ||
| // send(request); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.