use reqwest::Method;
use crate::error::Result;
use crate::http::{push_opt, Envelope, HttpClient, Query};
use crate::models::{
Automation, AutomationCreated, AutomationDeleted, AutomationRun, AutomationStats,
AutomationToggled, AutomationTriggered, CreateAutomation, Page, UpdateAutomation,
};
#[derive(Debug, Clone)]
pub struct Automations<'a> {
pub(crate) http: &'a HttpClient,
}
impl Automations<'_> {
pub async fn list(&self) -> Result<Vec<Automation>> {
let body: Envelope<Vec<Automation>> = self
.http
.send::<_, ()>(Method::GET, "/automations", None, None)
.await?;
Ok(body.data)
}
pub async fn get(&self, id: &str) -> Result<Automation> {
let body: Envelope<Automation> = self
.http
.send::<_, ()>(Method::GET, &format!("/automations/{id}"), None, None)
.await?;
Ok(body.data)
}
pub async fn create(&self, automation: &CreateAutomation) -> Result<AutomationCreated> {
let body: Envelope<AutomationCreated> = self
.http
.send(Method::POST, "/automations", None, Some(automation))
.await?;
Ok(body.data)
}
pub async fn update(&self, id: &str, changes: &UpdateAutomation) -> Result<Automation> {
let body: Envelope<Automation> = self
.http
.send(
Method::PUT,
&format!("/automations/{id}"),
None,
Some(changes),
)
.await?;
Ok(body.data)
}
pub async fn delete(&self, id: &str) -> Result<AutomationDeleted> {
let body: Envelope<AutomationDeleted> = self
.http
.send::<_, ()>(Method::DELETE, &format!("/automations/{id}"), None, None)
.await?;
Ok(body.data)
}
pub async fn toggle(&self, id: &str) -> Result<AutomationToggled> {
let body: Envelope<AutomationToggled> = self
.http
.send::<_, ()>(
Method::POST,
&format!("/automations/{id}/toggle"),
None,
None,
)
.await?;
Ok(body.data)
}
pub async fn runs(
&self,
id: &str,
page: Option<u32>,
per_page: Option<u32>,
) -> Result<Page<AutomationRun>> {
let mut query: Query = Vec::new();
push_opt(&mut query, "page", page);
push_opt(&mut query, "per_page", per_page);
self.http
.send::<_, ()>(
Method::GET,
&format!("/automations/{id}/runs"),
Some(query),
None,
)
.await
}
pub async fn get_run(&self, id: &str, run_id: i64) -> Result<AutomationRun> {
let body: Envelope<AutomationRun> = self
.http
.send::<_, ()>(
Method::GET,
&format!("/automations/{id}/runs/{run_id}"),
None,
None,
)
.await?;
Ok(body.data)
}
pub async fn stats(&self) -> Result<AutomationStats> {
let body: Envelope<AutomationStats> = self
.http
.send::<_, ()>(Method::GET, "/automations/stats", None, None)
.await?;
Ok(body.data)
}
pub async fn trigger(
&self,
id: &str,
payload: Option<&serde_json::Value>,
) -> Result<AutomationTriggered> {
let empty = serde_json::json!({});
let body: Envelope<AutomationTriggered> = self
.http
.send(
Method::POST,
&format!("/automations/{id}/trigger"),
None,
Some(payload.unwrap_or(&empty)),
)
.await?;
Ok(body.data)
}
}