watchbird_client 0.1.0

A self-hosted and API-driven uptime monitor
Documentation
use http::Method;
use watchbird_api::v1alpha1::{Alert, AlertId, CreateAlert, ListFilter};

use crate::{ApiClient, Result};

impl ApiClient {
    pub async fn get_alert(&self, id: AlertId) -> Result<Alert> {
        Ok(self
            .request(Method::GET, format!("alert/{}", id).as_str())
            .send()
            .await?
            .error_for_status()?
            .json::<Alert>()
            .await?)
    }

    pub async fn find_alert(&self, name: String) -> Result<Vec<Alert>> {
        Ok(self
            .request(Method::GET, "alerts")
            .query(
                &ListFilter {
                    owner_ref: Some(self.owner_ref.clone()),
                    name: Some(name),
                }
                .to_query(),
            )
            .send()
            .await?
            .error_for_status()?
            .json::<Vec<Alert>>()
            .await?)
    }

    pub async fn list_alerts(&self) -> Result<Vec<Alert>> {
        Ok(self
            .request(Method::GET, "alerts")
            .query(
                &ListFilter {
                    owner_ref: Some(self.owner_ref.clone()),
                    ..Default::default()
                }
                .to_query(),
            )
            .send()
            .await?
            .error_for_status()?
            .json::<Vec<Alert>>()
            .await?)
    }

    pub async fn list_all_alerts(&self) -> Result<Vec<Alert>> {
        Ok(self
            .request(Method::GET, "alerts")
            .send()
            .await?
            .error_for_status()?
            .json::<Vec<Alert>>()
            .await?)
    }

    pub async fn create_alert(&self, alert: &CreateAlert) -> Result<Alert> {
        Ok(self
            .request(Method::POST, "alert")
            .json(alert)
            .send()
            .await?
            .error_for_status()?
            .json::<Alert>()
            .await?)
    }

    pub async fn update_alert(&self, id: AlertId, alert: &CreateAlert) -> Result<Alert> {
        Ok(self
            .request(Method::PUT, &format!("alert/{}", id))
            .json(alert)
            .send()
            .await?
            .error_for_status()?
            .json::<Alert>()
            .await?)
    }

    pub async fn delete_alert(&self, id: AlertId) -> Result<()> {
        self.request(Method::DELETE, &format!("alert/{}", id))
            .send()
            .await?
            .error_for_status()?;
        Ok(())
    }
}