Skip to main content

babelforce_manager_sdk/resources/
automations.rs

1use crate::error::{map_manager_err, ManagerError};
2use crate::gen::manager::apis::configuration::Configuration;
3use crate::gen::manager::apis::global_automation_api as api;
4use crate::gen::manager::models;
5use crate::http::collect_all;
6use crate::retry::{with_retry, RetryPolicy};
7use crate::token::SharedCfg;
8
9/// Global automations (event triggers) — `/api/v2/events/triggers`.
10pub struct AutomationsResource {
11    pub(crate) cfg: SharedCfg<Configuration>,
12    pub(crate) retry: RetryPolicy,
13}
14
15impl AutomationsResource {
16    /// List all global automations (auto-paginated).
17    pub async fn list_all(&self) -> Result<Vec<models::GlobalAutomation>, ManagerError> {
18        let cfg = self.cfg.get().await?;
19        let cfg = cfg.as_ref();
20        collect_all(&self.retry, map_manager_err, |page| async move {
21            let r = api::list_global_automations(cfg, Some(page), None).await?;
22            Ok((
23                r.items,
24                r.pagination.pages.unwrap_or(1),
25                r.pagination.current.unwrap_or(1),
26            ))
27        })
28        .await
29    }
30
31    /// Create a global automation.
32    pub async fn create(
33        &self,
34        body: models::RestCreateGlobalAutomation,
35    ) -> Result<models::GlobalAutomationItemResponse, ManagerError> {
36        let cfg = self.cfg.get().await?;
37        let cfg = cfg.as_ref();
38        with_retry(&self.retry, false, || {
39            api::create_global_automation(cfg, body.clone())
40        })
41        .await
42        .map_err(map_manager_err)
43    }
44
45    /// Get a global automation by id.
46    pub async fn get(
47        &self,
48        id: &str,
49    ) -> Result<models::GlobalAutomationItemResponse, ManagerError> {
50        let cfg = self.cfg.get().await?;
51        let cfg = cfg.as_ref();
52        with_retry(&self.retry, true, || api::get_global_automation(cfg, id))
53            .await
54            .map_err(map_manager_err)
55    }
56
57    /// Update a global automation.
58    pub async fn update(
59        &self,
60        id: &str,
61        body: models::RestUpdateGlobalAutomation,
62    ) -> Result<models::GlobalAutomationItemResponse, ManagerError> {
63        let cfg = self.cfg.get().await?;
64        let cfg = cfg.as_ref();
65        with_retry(&self.retry, false, || {
66            api::update_global_automation(cfg, id, body.clone())
67        })
68        .await
69        .map_err(map_manager_err)
70    }
71
72    /// Delete a global automation.
73    pub async fn delete(&self, id: &str) -> Result<(), ManagerError> {
74        let cfg = self.cfg.get().await?;
75        let cfg = cfg.as_ref();
76        with_retry(&self.retry, false, || {
77            api::delete_global_automation(cfg, id)
78        })
79        .await
80        .map_err(map_manager_err)?;
81        Ok(())
82    }
83
84    /// Clone a global automation; returns the new automation.
85    pub async fn clone(
86        &self,
87        id: &str,
88    ) -> Result<models::GlobalAutomationItemResponse, ManagerError> {
89        let cfg = self.cfg.get().await?;
90        let cfg = cfg.as_ref();
91        with_retry(&self.retry, false, || api::clone_event_trigger(cfg, id))
92            .await
93            .map_err(map_manager_err)
94    }
95
96    /// Bulk-update global automations.
97    pub async fn bulk_update(
98        &self,
99        body: models::BulkUpdateRequest,
100    ) -> Result<models::ObjectListResponse, ManagerError> {
101        let cfg = self.cfg.get().await?;
102        let cfg = cfg.as_ref();
103        with_retry(&self.retry, false, || {
104            api::bulk_update_event_triggers(cfg, body.clone())
105        })
106        .await
107        .map_err(map_manager_err)
108    }
109
110    /// Bulk-delete global automations by id.
111    pub async fn bulk_delete(
112        &self,
113        ids: Vec<String>,
114    ) -> Result<models::DefaultV2MessageResponse, ManagerError> {
115        let cfg = self.cfg.get().await?;
116        let cfg = cfg.as_ref();
117        let ids = ids
118            .iter()
119            .map(|id| {
120                uuid::Uuid::parse_str(id)
121                    .map_err(|e| ManagerError::InvalidArgument(format!("id {id}: {e}")))
122            })
123            .collect::<Result<Vec<_>, _>>()?;
124        let body = models::BulkIdsRequest { ids };
125        with_retry(&self.retry, false, || {
126            api::bulk_delete_event_triggers(cfg, body.clone())
127        })
128        .await
129        .map_err(map_manager_err)
130    }
131
132    /// Dispatch a global automation, optionally simulating a call, and return the result payload.
133    pub async fn dispatch(
134        &self,
135        event_trigger_id: &str,
136        timeout: Option<i32>,
137        simulate_call: Option<bool>,
138        body: std::collections::HashMap<String, serde_json::Value>,
139    ) -> Result<std::collections::HashMap<String, serde_json::Value>, ManagerError> {
140        let cfg = self.cfg.get().await?;
141        let cfg = cfg.as_ref();
142        with_retry(&self.retry, false, || {
143            api::dispatch_event_trigger(
144                cfg,
145                event_trigger_id,
146                timeout,
147                simulate_call,
148                Some(body.clone()),
149            )
150        })
151        .await
152        .map_err(map_manager_err)
153    }
154}