adguardian/fetch/
fetch_status.rs

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