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