Skip to main content

babelforce_manager_sdk/resources/
integrations.rs

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