use crate::error::{map_manager_err, ManagerError};
use crate::gen::manager::apis::configuration::Configuration;
use crate::gen::manager::apis::global_automation_api as api;
use crate::gen::manager::models;
use crate::http::collect_all;
use crate::retry::{with_retry, RetryPolicy};
use crate::token::SharedCfg;
pub struct AutomationsResource {
pub(crate) cfg: SharedCfg<Configuration>,
pub(crate) retry: RetryPolicy,
}
impl AutomationsResource {
pub async fn list_all(&self) -> Result<Vec<models::GlobalAutomation>, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
collect_all(&self.retry, map_manager_err, |page| async move {
let r = api::list_global_automations(cfg, Some(page), None).await?;
Ok((r.items, r.pagination.pages, r.pagination.current))
})
.await
}
pub async fn create(
&self,
body: models::RestCreateGlobalAutomation,
) -> Result<models::GlobalAutomationItemResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, false, || {
api::create_global_automation(cfg, body.clone())
})
.await
.map_err(map_manager_err)
}
pub async fn get(
&self,
id: &str,
) -> Result<models::GlobalAutomationItemResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, true, || api::get_global_automation(cfg, id))
.await
.map_err(map_manager_err)
}
pub async fn update(
&self,
id: &str,
body: models::RestUpdateGlobalAutomation,
) -> Result<models::GlobalAutomationItemResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, false, || {
api::update_global_automation(cfg, id, body.clone())
})
.await
.map_err(map_manager_err)
}
pub async fn delete(&self, id: &str) -> Result<(), ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, false, || {
api::delete_global_automation(cfg, id)
})
.await
.map_err(map_manager_err)?;
Ok(())
}
pub async fn clone(
&self,
id: &str,
) -> Result<models::GlobalAutomationItemResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, false, || api::clone_event_trigger(cfg, id))
.await
.map_err(map_manager_err)
}
pub async fn bulk_update(
&self,
body: models::BulkUpdateRequest,
) -> Result<models::ObjectListResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, false, || {
api::bulk_update_event_triggers(cfg, body.clone())
})
.await
.map_err(map_manager_err)
}
pub async fn bulk_delete(
&self,
ids: Vec<String>,
) -> Result<models::DefaultV2MessageResponse, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
let ids = ids
.iter()
.map(|id| {
uuid::Uuid::parse_str(id)
.map_err(|e| ManagerError::InvalidArgument(format!("id {id}: {e}")))
})
.collect::<Result<Vec<_>, _>>()?;
let body = models::BulkIdsRequest { ids };
with_retry(&self.retry, false, || {
api::bulk_delete_event_triggers(cfg, body.clone())
})
.await
.map_err(map_manager_err)
}
pub async fn dispatch(
&self,
event_trigger_id: &str,
timeout: Option<i32>,
simulate_call: Option<bool>,
body: std::collections::HashMap<String, serde_json::Value>,
) -> Result<std::collections::HashMap<String, serde_json::Value>, ManagerError> {
let cfg = self.cfg.get().await?;
let cfg = cfg.as_ref();
with_retry(&self.retry, false, || {
api::dispatch_event_trigger(
cfg,
event_trigger_id,
timeout,
simulate_call,
Some(body.clone()),
)
})
.await
.map_err(map_manager_err)
}
}