Skip to main content

babelforce_manager_sdk/resources/
triggers.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::{manager_api, trigger_api as api};
6use crate::gen::manager::models;
7use crate::http::collect_all;
8use crate::retry::{with_retry, RetryPolicy};
9
10/// Workflow triggers — `/api/v2/triggers`.
11pub struct TriggersResource {
12    pub(crate) cfg: Arc<Configuration>,
13    pub(crate) retry: RetryPolicy,
14}
15
16impl TriggersResource {
17    /// List all triggers (auto-paginated).
18    pub async fn list_all(&self) -> Result<Vec<models::Trigger>, ManagerError> {
19        collect_all(&self.retry, map_manager_err, |page| async move {
20            let r = api::list_triggers(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 trigger.
27    pub async fn create(
28        &self,
29        body: models::RestCreateTrigger,
30    ) -> Result<models::TriggerItemResponse, ManagerError> {
31        with_retry(&self.retry, false, || {
32            api::create_trigger(self.cfg.as_ref(), body.clone())
33        })
34        .await
35        .map_err(map_manager_err)
36    }
37
38    /// Get a trigger by id.
39    pub async fn get(&self, id: &str) -> Result<models::TriggerItemResponse, ManagerError> {
40        with_retry(&self.retry, true, || {
41            api::get_trigger(self.cfg.as_ref(), id)
42        })
43        .await
44        .map_err(map_manager_err)
45    }
46
47    /// Update a trigger.
48    pub async fn update(
49        &self,
50        id: &str,
51        body: models::RestUpdateTrigger,
52    ) -> Result<models::TriggerItemResponse, ManagerError> {
53        with_retry(&self.retry, false, || {
54            api::update_trigger(self.cfg.as_ref(), id, body.clone())
55        })
56        .await
57        .map_err(map_manager_err)
58    }
59
60    /// Delete a trigger.
61    pub async fn delete(&self, id: &str) -> Result<(), ManagerError> {
62        with_retry(&self.retry, false, || {
63            api::delete_trigger(self.cfg.as_ref(), id)
64        })
65        .await
66        .map_err(map_manager_err)?;
67        Ok(())
68    }
69
70    /// Clone a trigger; returns the new trigger.
71    pub async fn clone(&self, id: &str) -> Result<models::TriggerItemResponse, ManagerError> {
72        with_retry(&self.retry, false, || {
73            api::clone_trigger(self.cfg.as_ref(), id)
74        })
75        .await
76        .map_err(map_manager_err)
77    }
78
79    /// Test trigger conditions against a sample payload. `test_mode` runs without side effects.
80    pub async fn test(
81        &self,
82        body: models::TestTriggersRequest,
83        test_mode: bool,
84    ) -> Result<models::TestTriggersResponse, ManagerError> {
85        with_retry(&self.retry, false, || {
86            manager_api::test_triggers(self.cfg.as_ref(), test_mode, Some(body.clone()))
87        })
88        .await
89        .map_err(map_manager_err)
90    }
91
92    /// Apply a bulk action (e.g. enable/disable/delete) to triggers by id.
93    pub async fn bulk_action(
94        &self,
95        action: &str,
96        ids: Vec<String>,
97    ) -> Result<models::BulkActionResponse, ManagerError> {
98        let ids = ids
99            .iter()
100            .map(|id| {
101                uuid::Uuid::parse_str(id)
102                    .map_err(|e| ManagerError::InvalidArgument(format!("id {id}: {e}")))
103            })
104            .collect::<Result<Vec<_>, _>>()?;
105        let body = models::BulkIdsRequest { ids };
106        with_retry(&self.retry, false, || {
107            api::bulk_trigger_action(self.cfg.as_ref(), action, body.clone())
108        })
109        .await
110        .map_err(map_manager_err)
111    }
112
113    /// List the available trigger expressions.
114    pub async fn expressions(&self) -> Result<models::ObjectListResponse, ManagerError> {
115        with_retry(&self.retry, true, || {
116            api::list_trigger_expressions(self.cfg.as_ref())
117        })
118        .await
119        .map_err(map_manager_err)
120    }
121
122    /// List the available trigger operators.
123    pub async fn operators(&self) -> Result<models::ObjectListResponse, ManagerError> {
124        with_retry(&self.retry, true, || {
125            api::list_trigger_operators(self.cfg.as_ref())
126        })
127        .await
128        .map_err(map_manager_err)
129    }
130
131    /// List the conditions configured on a trigger.
132    pub async fn conditions(
133        &self,
134        id: &str,
135    ) -> Result<models::TriggerConditionListResponse, ManagerError> {
136        with_retry(&self.retry, true, || {
137            api::list_trigger_conditions(self.cfg.as_ref(), id)
138        })
139        .await
140        .map_err(map_manager_err)
141    }
142
143    /// Replace the conditions on a trigger.
144    pub async fn set_conditions(
145        &self,
146        id: &str,
147        body: models::TriggerConditionsRequest,
148    ) -> Result<models::TriggerItemResponse, ManagerError> {
149        with_retry(&self.retry, false, || {
150            api::set_trigger_conditions(self.cfg.as_ref(), id, body.clone())
151        })
152        .await
153        .map_err(map_manager_err)
154    }
155
156    /// List where a trigger is used.
157    pub async fn uses(&self, id: &str) -> Result<models::TriggerUsesResponse, ManagerError> {
158        with_retry(&self.retry, true, || {
159            api::get_trigger_uses(self.cfg.as_ref(), id)
160        })
161        .await
162        .map_err(map_manager_err)
163    }
164}