Skip to main content

babelforce_manager_sdk/resources/
automations.rs

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