Skip to main content

adguardian/fetch/
fetch_status.rs

1use base64::{engine::general_purpose::STANDARD, Engine as _};
2use reqwest::header::{HeaderValue, AUTHORIZATION, CONTENT_LENGTH};
3use serde::Deserialize;
4
5/// Represents the status response from the AdGuard Home API.
6///
7/// This struct is used to deserialize the JSON response from the
8/// `/control/status` endpoint.
9///
10/// # Example
11///
12/// A `StatusResponse` is typically obtained like this:
13///
14/// ```
15/// let client = reqwest::Client::new();
16/// let hostname = "http://localhost:3000";
17/// let username = "username";
18/// let password = "password";
19/// let status = fetch_adguard_status(&client, &hostname, &username, &password).await?;
20/// println!("AdGuard Status: {:?}", status);
21/// ```
22///
23/// # Fields
24///
25/// * `version` - The version of the AdGuard Home instance.
26/// * `dns_port` - The port number on which the DNS server is running.
27/// * `http_port` - The port number on which the HTTP server is running.
28/// * `protection_enabled` - Whether or not protection is currently enabled.
29/// * `dhcp_available` - Whether or not DHCP is available.
30/// * `running` - Whether or not the AdGuard Home instance is currently running.
31#[derive(Debug, Default, Deserialize, Clone)]
32#[serde(default)]
33pub struct StatusResponse {
34  pub version: String,
35  pub dns_port: u16,
36  pub http_port: u16,
37  pub protection_enabled: bool,
38  pub dhcp_available: bool,
39  pub running: bool,
40}
41
42/// Fetches the current status from the AdGuard Home instance.
43///
44/// This function sends a GET request to the `/control/status` endpoint of the
45/// AdGuard Home API, then deserializes the JSON response into a `StatusResponse`.
46///
47/// # Arguments
48///
49/// * `client` - A reference to the `reqwest::Client`.
50/// * `hostname` - The hostname of the AdGuard Home instance.
51/// * `username` - The username for the AdGuard Home instance.
52/// * `password` - The password for the AdGuard Home instance.
53///
54/// # Returns
55///
56/// A `Result` which is `Ok` if the status was successfully fetched and `Err` otherwise.
57/// The `Ok` variant contains a `StatusResponse`.
58///
59/// # Example
60///
61/// ```
62/// let client = reqwest::Client::new();
63/// let hostname = "http://localhost:80";
64/// let username = "username";
65/// let password = "password";
66/// let status = fetch_adguard_status(&client, &hostname, &username, &password).await?;
67/// println!("AdGuard Status: {:?}", status);
68/// ```
69pub async fn fetch_adguard_status(
70  client: &reqwest::Client,
71  endpoint: &str,
72  username: &str,
73  password: &str,
74) -> Result<StatusResponse, anyhow::Error> {
75  let auth_string = format!("{}:{}", username, password);
76  let auth_header_value = format!("Basic {}", STANDARD.encode(&auth_string));
77  let mut headers = reqwest::header::HeaderMap::new();
78  headers.insert(AUTHORIZATION, auth_header_value.parse()?);
79  headers.insert(CONTENT_LENGTH, HeaderValue::from_static("0"));
80
81  let url = format!("{}/control/status", endpoint);
82  let response = client.get(&url).headers(headers).send().await?;
83  if !response.status().is_success() {
84    return Err(anyhow::anyhow!(
85      "Request failed with status code {}",
86      response.status()
87    ));
88  }
89
90  let data = response.json().await?;
91  Ok(data)
92}