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((r.items, r.pagination.pages, r.pagination.current))
23        })
24        .await
25    }
26
27    /// Create a global automation.
28    pub async fn create(
29        &self,
30        body: models::RestCreateGlobalAutomation,
31    ) -> Result<models::GlobalAutomationItemResponse, ManagerError> {
32        let cfg = self.cfg.get().await?;
33        let cfg = cfg.as_ref();
34        with_retry(&self.retry, false, || {
35            api::create_global_automation(cfg, body.clone())
36        })
37        .await
38        .map_err(map_manager_err)
39    }
40
41    /// Get a global automation by id.
42    pub async fn get(
43        &self,
44        id: &str,
45    ) -> Result<models::GlobalAutomationItemResponse, ManagerError> {
46        let cfg = self.cfg.get().await?;
47        let cfg = cfg.as_ref();
48        with_retry(&self.retry, true, || api::get_global_automation(cfg, id))
49            .await
50            .map_err(map_manager_err)
51    }
52
53    /// Update a global automation.
54    pub async fn update(
55        &self,
56        id: &str,
57        body: models::RestUpdateGlobalAutomation,
58    ) -> Result<models::GlobalAutomationItemResponse, ManagerError> {
59        let cfg = self.cfg.get().await?;
60        let cfg = cfg.as_ref();
61        with_retry(&self.retry, false, || {
62            api::update_global_automation(cfg, id, body.clone())
63        })
64        .await
65        .map_err(map_manager_err)
66    }
67
68    /// Delete a global automation.
69    pub async fn delete(&self, id: &str) -> Result<(), ManagerError> {
70        let cfg = self.cfg.get().await?;
71        let cfg = cfg.as_ref();
72        with_retry(&self.retry, false, || {
73            api::delete_global_automation(cfg, id)
74        })
75        .await
76        .map_err(map_manager_err)?;
77        Ok(())
78    }
79
80    /// Clone a global automation; returns the new automation.
81    pub async fn clone(
82        &self,
83        id: &str,
84    ) -> Result<models::GlobalAutomationItemResponse, ManagerError> {
85        let cfg = self.cfg.get().await?;
86        let cfg = cfg.as_ref();
87        with_retry(&self.retry, false, || api::clone_event_trigger(cfg, id))
88            .await
89            .map_err(map_manager_err)
90    }
91
92    /// Bulk-update global automations.
93    pub async fn bulk_update(
94        &self,
95        body: models::BulkUpdateRequest,
96    ) -> Result<models::ObjectListResponse, ManagerError> {
97        let cfg = self.cfg.get().await?;
98        let cfg = cfg.as_ref();
99        with_retry(&self.retry, false, || {
100            api::bulk_update_event_triggers(cfg, body.clone())
101        })
102        .await
103        .map_err(map_manager_err)
104    }
105
106    /// Bulk-delete global automations by id.
107    pub async fn bulk_delete(
108        &self,
109        ids: Vec<String>,
110    ) -> Result<models::DefaultV2MessageResponse, ManagerError> {
111        let cfg = self.cfg.get().await?;
112        let cfg = cfg.as_ref();
113        let ids = ids
114            .iter()
115            .map(|id| {
116                uuid::Uuid::parse_str(id)
117                    .map_err(|e| ManagerError::InvalidArgument(format!("id {id}: {e}")))
118            })
119            .collect::<Result<Vec<_>, _>>()?;
120        let body = models::BulkIdsRequest { ids };
121        with_retry(&self.retry, false, || {
122            api::bulk_delete_event_triggers(cfg, body.clone())
123        })
124        .await
125        .map_err(map_manager_err)
126    }
127
128    /// Dispatch a global automation, optionally simulating a call, and return the result payload.
129    pub async fn dispatch(
130        &self,
131        event_trigger_id: &str,
132        timeout: Option<i32>,
133        simulate_call: Option<bool>,
134        body: std::collections::HashMap<String, serde_json::Value>,
135    ) -> Result<std::collections::HashMap<String, serde_json::Value>, ManagerError> {
136        let cfg = self.cfg.get().await?;
137        let cfg = cfg.as_ref();
138        with_retry(&self.retry, false, || {
139            api::dispatch_event_trigger(
140                cfg,
141                event_trigger_id,
142                timeout,
143                simulate_call,
144                Some(body.clone()),
145            )
146        })
147        .await
148        .map_err(map_manager_err)
149    }
150}