Skip to main content

babelforce_manager_sdk/resources/
integrations.rs

1use crate::error::{map_manager_err, ManagerError};
2use crate::gen::manager::apis::action_api;
3use crate::gen::manager::apis::configuration::Configuration;
4use crate::gen::manager::apis::integration_api as api;
5use crate::gen::manager::models;
6use crate::http::{collect_all, fetch_page, Page};
7use crate::retry::{with_retry, RetryPolicy};
8use crate::token::SharedCfg;
9
10/// Integrations — `/api/v2/integrations`.
11pub struct IntegrationsResource {
12    pub(crate) cfg: SharedCfg<Configuration>,
13    pub(crate) retry: RetryPolicy,
14}
15
16impl IntegrationsResource {
17    /// List all configured integrations (auto-paginated).
18    pub async fn list_all(&self) -> Result<Vec<models::Integration>, ManagerError> {
19        let cfg = self.cfg.get().await?;
20        let cfg = cfg.as_ref();
21        collect_all(&self.retry, map_manager_err, |page| async move {
22            let r = api::list_integrations(cfg, Some(page), None).await?;
23            Ok((
24                r.items,
25                r.pagination.pages.unwrap_or(1),
26                r.pagination.current.unwrap_or(1),
27            ))
28        })
29        .await
30    }
31
32    /// List one page of configured integrations.
33    pub async fn list_page(
34        &self,
35        page: i32,
36        per_page: Option<i32>,
37    ) -> Result<Page<models::Integration>, ManagerError> {
38        let cfg = self.cfg.get().await?;
39        let cfg = cfg.as_ref();
40        fetch_page(&self.retry, map_manager_err, || async move {
41            let r = api::list_integrations(cfg, Some(page), per_page).await?;
42            Ok((
43                r.items,
44                r.pagination.pages.unwrap_or(1),
45                r.pagination.current.unwrap_or(1),
46                r.pagination.total,
47            ))
48        })
49        .await
50    }
51
52    /// Create an integration.
53    pub async fn create(
54        &self,
55        body: models::IntegrationCreateRequest,
56    ) -> Result<models::IntegrationItemResponse, ManagerError> {
57        let cfg = self.cfg.get().await?;
58        let cfg = cfg.as_ref();
59        with_retry(&self.retry, false, || {
60            api::create_integration(cfg, body.clone())
61        })
62        .await
63        .map_err(map_manager_err)
64    }
65
66    /// Get an integration by id.
67    pub async fn get(&self, id: &str) -> Result<models::IntegrationItemResponse, ManagerError> {
68        let cfg = self.cfg.get().await?;
69        let cfg = cfg.as_ref();
70        with_retry(&self.retry, true, || api::get_integration(cfg, id))
71            .await
72            .map_err(map_manager_err)
73    }
74
75    /// Update an integration.
76    pub async fn update(
77        &self,
78        id: &str,
79        body: models::IntegrationUpdateRequest,
80    ) -> Result<models::IntegrationItemResponse, ManagerError> {
81        let cfg = self.cfg.get().await?;
82        let cfg = cfg.as_ref();
83        with_retry(&self.retry, false, || {
84            api::update_integration(cfg, id, body.clone())
85        })
86        .await
87        .map_err(map_manager_err)
88    }
89
90    /// Delete an integration.
91    pub async fn delete(&self, id: &str) -> Result<(), ManagerError> {
92        let cfg = self.cfg.get().await?;
93        let cfg = cfg.as_ref();
94        with_retry(&self.retry, false, || api::delete_integration(cfg, id))
95            .await
96            .map_err(map_manager_err)?;
97        Ok(())
98    }
99
100    /// List the integration providers available to this account.
101    pub async fn available(
102        &self,
103    ) -> Result<models::IntegrationListAvailableIntegrationsResponse, ManagerError> {
104        let cfg = self.cfg.get().await?;
105        let cfg = cfg.as_ref();
106        with_retry(&self.retry, true, || api::list_available_integrations(cfg))
107            .await
108            .map_err(map_manager_err)
109    }
110
111    /// Associate an integration action with an object.
112    pub async fn add_association(
113        &self,
114        integration_id: &str,
115        association_id: &str,
116        action_name: &str,
117    ) -> Result<models::IntegrationAddAssociationResponse, ManagerError> {
118        let cfg = self.cfg.get().await?;
119        let cfg = cfg.as_ref();
120        with_retry(&self.retry, false, || {
121            api::add_integration_association(cfg, integration_id, association_id, action_name)
122        })
123        .await
124        .map_err(map_manager_err)
125    }
126
127    /// Remove an integration action association.
128    pub async fn remove_association(
129        &self,
130        integration_id: &str,
131        association_id: &str,
132        action_name: &str,
133    ) -> Result<models::IntegrationRemoveAssociationResponse, ManagerError> {
134        let cfg = self.cfg.get().await?;
135        let cfg = cfg.as_ref();
136        with_retry(&self.retry, false, || {
137            api::delete_integration_association(cfg, integration_id, association_id, action_name)
138        })
139        .await
140        .map_err(map_manager_err)
141    }
142
143    /// Get a provider's logo at a given size.
144    pub async fn provider_logo(
145        &self,
146        provider_name: models::IntegrationProvider,
147        size: &str,
148    ) -> Result<models::GetIntegrationProviderLogoResponse, ManagerError> {
149        let cfg = self.cfg.get().await?;
150        let cfg = cfg.as_ref();
151        with_retry(&self.retry, true, || {
152            api::get_integration_provider_logo(cfg, provider_name, size)
153        })
154        .await
155        .map_err(map_manager_err)
156    }
157
158    /// List the session variables a provider's actions expose.
159    pub async fn provider_session_variables(
160        &self,
161        provider: &str,
162    ) -> Result<models::IntegrationSessionVariableItemsResponse, ManagerError> {
163        let cfg = self.cfg.get().await?;
164        let cfg = cfg.as_ref();
165        with_retry(&self.retry, true, || {
166            api::list_provider_session_variables(cfg, provider)
167        })
168        .await
169        .map_err(map_manager_err)
170    }
171
172    /// Dispatch an integration action.
173    pub async fn dispatch_action(
174        &self,
175        integration_id: &str,
176        action: &str,
177        body: models::IntegrationDispatchActionRequest,
178    ) -> Result<models::IntegrationDispatchActionResponse, ManagerError> {
179        let cfg = self.cfg.get().await?;
180        let cfg = cfg.as_ref();
181        with_retry(&self.retry, false, || {
182            api::dispatch_action(cfg, integration_id, action, None, None, Some(body.clone()))
183        })
184        .await
185        .map_err(map_manager_err)
186    }
187
188    /// List the variables a single provider action exposes.
189    pub async fn action_variables(
190        &self,
191        provider: models::IntegrationProvider,
192        action_name: &str,
193    ) -> Result<models::VariableDefinitionItemsResponse, ManagerError> {
194        let cfg = self.cfg.get().await?;
195        let cfg = cfg.as_ref();
196        with_retry(&self.retry, true, || {
197            api::list_single_action_session_variables(cfg, provider, action_name)
198        })
199        .await
200        .map_err(map_manager_err)
201    }
202
203    /// List the available integration providers.
204    pub async fn providers(&self) -> Result<models::IntegrationObjectListResponse, ManagerError> {
205        let cfg = self.cfg.get().await?;
206        let cfg = cfg.as_ref();
207        with_retry(&self.retry, true, || api::list_integration_providers(cfg))
208            .await
209            .map_err(map_manager_err)
210    }
211
212    /// Get a provider's config template.
213    pub async fn provider_template(
214        &self,
215        provider: &str,
216    ) -> Result<std::collections::HashMap<String, serde_json::Value>, ManagerError> {
217        let cfg = self.cfg.get().await?;
218        let cfg = cfg.as_ref();
219        with_retry(&self.retry, true, || {
220            api::get_integration_provider_template(cfg, provider)
221        })
222        .await
223        .map_err(map_manager_err)
224    }
225
226    /// Get the config template for a given integration type and provider.
227    pub async fn template(
228        &self,
229        r#type: &str,
230        provider: &str,
231    ) -> Result<std::collections::HashMap<String, serde_json::Value>, ManagerError> {
232        let cfg = self.cfg.get().await?;
233        let cfg = cfg.as_ref();
234        with_retry(&self.retry, true, || {
235            api::get_integration_template(cfg, r#type, provider)
236        })
237        .await
238        .map_err(map_manager_err)
239    }
240
241    /// Authorize an integration.
242    pub async fn authorize(
243        &self,
244        id: &str,
245        body: std::collections::HashMap<String, serde_json::Value>,
246    ) -> Result<models::IntegrationItemResponse, ManagerError> {
247        let cfg = self.cfg.get().await?;
248        let cfg = cfg.as_ref();
249        with_retry(&self.retry, false, || {
250            api::authorize_integration(cfg, id, Some(body.clone()))
251        })
252        .await
253        .map_err(map_manager_err)
254    }
255
256    /// Clone an integration.
257    pub async fn clone(&self, id: &str) -> Result<models::IntegrationItemResponse, ManagerError> {
258        let cfg = self.cfg.get().await?;
259        let cfg = cfg.as_ref();
260        with_retry(&self.retry, false, || api::clone_integration(cfg, id))
261            .await
262            .map_err(map_manager_err)
263    }
264
265    /// Run the integrate step for an integration.
266    pub async fn integrate(
267        &self,
268        id: &str,
269        body: std::collections::HashMap<String, serde_json::Value>,
270    ) -> Result<models::IntegrationItemResponse, ManagerError> {
271        let cfg = self.cfg.get().await?;
272        let cfg = cfg.as_ref();
273        with_retry(&self.retry, false, || {
274            api::integrate_integration(cfg, id, Some(body.clone()))
275        })
276        .await
277        .map_err(map_manager_err)
278    }
279
280    /// Update several integrations in one request.
281    pub async fn bulk_update(
282        &self,
283        body: models::BulkUpdateIntegrationsRequest,
284    ) -> Result<models::DefaultV2MessageResponse, ManagerError> {
285        let cfg = self.cfg.get().await?;
286        let cfg = cfg.as_ref();
287        with_retry(&self.retry, false, || {
288            api::bulk_update_integrations(cfg, body.clone())
289        })
290        .await
291        .map_err(map_manager_err)
292    }
293
294    /// Delete several integrations in one request, given their ids.
295    pub async fn bulk_delete(
296        &self,
297        ids: Vec<String>,
298    ) -> Result<models::DefaultV2MessageResponse, ManagerError> {
299        let cfg = self.cfg.get().await?;
300        let cfg = cfg.as_ref();
301        for id in &ids {
302            uuid::Uuid::parse_str(id)
303                .map_err(|e| ManagerError::InvalidArgument(format!("id {id}: {e}")))?;
304        }
305        let body = models::IntegrationBulkIdsRequest { ids };
306        with_retry(&self.retry, false, || {
307            api::bulk_delete_integrations(cfg, body.clone())
308        })
309        .await
310        .map_err(map_manager_err)
311    }
312
313    /// List the actions exposed by integrations of a given type.
314    pub async fn type_actions(
315        &self,
316        r#type: &str,
317    ) -> Result<models::IntegrationObjectListResponse, ManagerError> {
318        let cfg = self.cfg.get().await?;
319        let cfg = cfg.as_ref();
320        with_retry(&self.retry, true, || {
321            api::list_integration_type_actions(cfg, r#type)
322        })
323        .await
324        .map_err(map_manager_err)
325    }
326
327    /// Dispatch an action against an integration of a given type.
328    pub async fn dispatch_type_action(
329        &self,
330        r#type: &str,
331        id: &str,
332        action: &str,
333        body: std::collections::HashMap<String, serde_json::Value>,
334    ) -> Result<std::collections::HashMap<String, serde_json::Value>, ManagerError> {
335        let cfg = self.cfg.get().await?;
336        let cfg = cfg.as_ref();
337        with_retry(&self.retry, false, || {
338            api::dispatch_integration_type_action(cfg, r#type, id, action, Some(body.clone()))
339        })
340        .await
341        .map_err(map_manager_err)
342    }
343
344    /// Proxy a GET request through an integration's API.
345    pub async fn api_proxy_get(&self, integration_id: &str, uri: &str) -> Result<(), ManagerError> {
346        let cfg = self.cfg.get().await?;
347        let cfg = cfg.as_ref();
348        with_retry(&self.retry, true, || {
349            api::integration_api_proxy_get(cfg, integration_id, uri)
350        })
351        .await
352        .map_err(map_manager_err)
353    }
354
355    /// Proxy a POST request through an integration's API.
356    pub async fn api_proxy_post(
357        &self,
358        integration_id: &str,
359        uri: &str,
360        body: std::collections::HashMap<String, serde_json::Value>,
361    ) -> Result<(), ManagerError> {
362        let cfg = self.cfg.get().await?;
363        let cfg = cfg.as_ref();
364        with_retry(&self.retry, false, || {
365            api::integration_api_proxy_post(cfg, integration_id, uri, Some(body.clone()))
366        })
367        .await
368        .map_err(map_manager_err)
369    }
370
371    /// List the tokens issued for an integration.
372    pub async fn list_tokens(
373        &self,
374        id: &str,
375    ) -> Result<models::IntegrationTokenListResponse, ManagerError> {
376        let cfg = self.cfg.get().await?;
377        let cfg = cfg.as_ref();
378        with_retry(&self.retry, true, || api::list_integration_tokens(cfg, id))
379            .await
380            .map_err(map_manager_err)
381    }
382
383    /// Get a single integration token.
384    pub async fn get_token(
385        &self,
386        id: &str,
387        token_id: &str,
388    ) -> Result<models::IntegrationTokenItemResponse, ManagerError> {
389        let cfg = self.cfg.get().await?;
390        let cfg = cfg.as_ref();
391        with_retry(&self.retry, true, || {
392            api::get_integration_token(cfg, id, token_id)
393        })
394        .await
395        .map_err(map_manager_err)
396    }
397
398    /// Delete an integration token.
399    pub async fn delete_token(
400        &self,
401        id: &str,
402        token_id: &str,
403    ) -> Result<models::DefaultV2MessageResponse, ManagerError> {
404        let cfg = self.cfg.get().await?;
405        let cfg = cfg.as_ref();
406        with_retry(&self.retry, false, || {
407            api::delete_integration_token(cfg, id, token_id)
408        })
409        .await
410        .map_err(map_manager_err)
411    }
412
413    /// Refresh an integration token.
414    pub async fn refresh_token(
415        &self,
416        id: &str,
417        token_id: &str,
418    ) -> Result<models::IntegrationTokenItemResponse, ManagerError> {
419        let cfg = self.cfg.get().await?;
420        let cfg = cfg.as_ref();
421        with_retry(&self.retry, false, || {
422            api::refresh_integration_token(cfg, id, token_id)
423        })
424        .await
425        .map_err(map_manager_err)
426    }
427
428    /// List the available trigger/automation actions (catalog), optionally filtered by type.
429    pub async fn list_actions(
430        &self,
431        r#type: Option<&str>,
432    ) -> Result<models::ObjectListResponse, ManagerError> {
433        let cfg = self.cfg.get().await?;
434        let cfg = cfg.as_ref();
435        with_retry(&self.retry, true, || action_api::list_actions(cfg, r#type))
436            .await
437            .map_err(map_manager_err)
438    }
439
440    /// List an action's input parameters.
441    pub async fn list_action_params(
442        &self,
443        provider_name: &str,
444        provider_action_name: &str,
445    ) -> Result<models::ObjectListResponse, ManagerError> {
446        let cfg = self.cfg.get().await?;
447        let cfg = cfg.as_ref();
448        with_retry(&self.retry, true, || {
449            action_api::list_action_params(cfg, provider_name, provider_action_name)
450        })
451        .await
452        .map_err(map_manager_err)
453    }
454
455    /// Execute a call/queue action directly.
456    pub async fn execute_action(
457        &self,
458        action_type: &str,
459        action_name: &str,
460        body: Option<std::collections::HashMap<String, serde_json::Value>>,
461    ) -> Result<models::DefaultV2MessageResponse, ManagerError> {
462        let cfg = self.cfg.get().await?;
463        let cfg = cfg.as_ref();
464        with_retry(&self.retry, false, || {
465            action_api::execute_action(cfg, action_type, action_name, body.clone())
466        })
467        .await
468        .map_err(map_manager_err)
469    }
470
471    /// Dispatch an integration action via GET.
472    pub async fn dispatch_action_get(
473        &self,
474        integration_id: &str,
475        action: &str,
476        call_id: Option<&str>,
477        session_id: Option<&str>,
478    ) -> Result<models::IntegrationDispatchActionResponse, ManagerError> {
479        let cfg = self.cfg.get().await?;
480        let cfg = cfg.as_ref();
481        with_retry(&self.retry, true, || {
482            api::dispatch_action_get(cfg, integration_id, action, call_id, session_id)
483        })
484        .await
485        .map_err(map_manager_err)
486    }
487}