plausible_rs/api/
health.rs

1use crate::{Error, Plausible};
2use reqwest::{RequestBuilder, StatusCode};
3use serde::{Deserialize, Serialize};
4
5impl Plausible {
6    /// Monitor the status of the Plausible Analytics API.
7    ///
8    /// # Errors
9    ///
10    /// Will return `Err` if an error occurred while creating/sending the request,
11    /// if it failed to decode the response's bytes, if the response's status code was not a
12    /// success, or if it failed to serialize the response bytes into `HealthResponse`.
13    pub async fn health(&self) -> Result<HealthResponse, Error> {
14        // create request
15        let request: RequestBuilder = self.client.get(format!("{}/api/health", self.base_url));
16
17        // send request, get response
18        let response = request.send().await?;
19
20        // parse status code and returned bytes
21        let status_code: StatusCode = response.status();
22        let bytes = response.bytes().await?;
23
24        // check if failure
25        if !status_code.is_success() {
26            return Err(Error::RequestFailed { bytes, status_code });
27        }
28
29        // success
30        let response: HealthResponse = serde_json::from_slice(&bytes)?;
31        Ok(response)
32    }
33}
34
35/// Response returned from 'GET /api/health' API.
36///
37/// If the individual status is healthy, expect the string value to equal `"ok"`.
38#[derive(Debug, Clone, Serialize, Deserialize)]
39pub struct HealthResponse {
40    pub clickhouse: String,
41    pub postgres: String,
42}