watchbird_client 0.1.0

A self-hosted and API-driven uptime monitor
Documentation
use http::Method;
use watchbird_api::v1alpha1::{CreateWatch, ListFilter, Watch, WatchId};

use crate::{ApiClient, Result};

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

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

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

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

    pub async fn create_watch(&self, watch: &CreateWatch) -> Result<Watch> {
        Ok(self
            .request(Method::POST, "watch")
            .json(watch)
            .send()
            .await?
            .error_for_status()?
            .json::<Watch>()
            .await?)
    }

    pub async fn update_watch(&self, id: WatchId, watch: &CreateWatch) -> Result<Watch> {
        Ok(self
            .request(Method::PUT, &format!("watch/{}", id))
            .json(watch)
            .send()
            .await?
            .error_for_status()?
            .json::<Watch>()
            .await?)
    }

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