babelforce-manager-sdk 0.46.0

Rust SDK for the babelforce manager APIs — auth, user & agent management, call reporting, metrics, and task automations.
Documentation
use crate::error::{map_manager_err, ManagerError};
use crate::gen::manager::apis::configuration::Configuration;
use crate::gen::manager::apis::global_automation_api as api;
use crate::gen::manager::models;
use crate::http::collect_all;
use crate::retry::{with_retry, RetryPolicy};
use crate::token::SharedCfg;

/// Global automations (event triggers) — `/api/v2/events/triggers`.
pub struct AutomationsResource {
    pub(crate) cfg: SharedCfg<Configuration>,
    pub(crate) retry: RetryPolicy,
}

impl AutomationsResource {
    /// List all global automations (auto-paginated).
    pub async fn list_all(&self) -> Result<Vec<models::GlobalAutomation>, ManagerError> {
        let cfg = self.cfg.get().await?;
        let cfg = cfg.as_ref();
        collect_all(&self.retry, map_manager_err, |page| async move {
            let r = api::list_global_automations(cfg, Some(page), None).await?;
            Ok((
                r.items,
                r.pagination.pages.unwrap_or(1),
                r.pagination.current.unwrap_or(1),
            ))
        })
        .await
    }

    /// Create a global automation.
    pub async fn create(
        &self,
        body: models::RestCreateGlobalAutomation,
    ) -> Result<models::GlobalAutomationItemResponse, ManagerError> {
        let cfg = self.cfg.get().await?;
        let cfg = cfg.as_ref();
        with_retry(&self.retry, false, || {
            api::create_global_automation(cfg, body.clone())
        })
        .await
        .map_err(map_manager_err)
    }

    /// Get a global automation by id.
    pub async fn get(
        &self,
        id: &str,
    ) -> Result<models::GlobalAutomationItemResponse, ManagerError> {
        let cfg = self.cfg.get().await?;
        let cfg = cfg.as_ref();
        with_retry(&self.retry, true, || api::get_global_automation(cfg, id))
            .await
            .map_err(map_manager_err)
    }

    /// Update a global automation.
    pub async fn update(
        &self,
        id: &str,
        body: models::RestUpdateGlobalAutomation,
    ) -> Result<models::GlobalAutomationItemResponse, ManagerError> {
        let cfg = self.cfg.get().await?;
        let cfg = cfg.as_ref();
        with_retry(&self.retry, false, || {
            api::update_global_automation(cfg, id, body.clone())
        })
        .await
        .map_err(map_manager_err)
    }

    /// Delete a global automation.
    pub async fn delete(&self, id: &str) -> Result<(), ManagerError> {
        let cfg = self.cfg.get().await?;
        let cfg = cfg.as_ref();
        with_retry(&self.retry, false, || {
            api::delete_global_automation(cfg, id)
        })
        .await
        .map_err(map_manager_err)?;
        Ok(())
    }

    /// Clone a global automation; returns the new automation.
    pub async fn clone(
        &self,
        id: &str,
    ) -> Result<models::GlobalAutomationItemResponse, ManagerError> {
        let cfg = self.cfg.get().await?;
        let cfg = cfg.as_ref();
        with_retry(&self.retry, false, || api::clone_event_trigger(cfg, id))
            .await
            .map_err(map_manager_err)
    }

    /// Bulk-update global automations.
    pub async fn bulk_update(
        &self,
        body: models::BulkUpdateRequest,
    ) -> Result<models::ObjectListResponse, ManagerError> {
        let cfg = self.cfg.get().await?;
        let cfg = cfg.as_ref();
        with_retry(&self.retry, false, || {
            api::bulk_update_event_triggers(cfg, body.clone())
        })
        .await
        .map_err(map_manager_err)
    }

    /// Bulk-delete global automations by id.
    pub async fn bulk_delete(
        &self,
        ids: Vec<String>,
    ) -> Result<models::DefaultV2MessageResponse, ManagerError> {
        let cfg = self.cfg.get().await?;
        let cfg = cfg.as_ref();
        let ids = ids
            .iter()
            .map(|id| {
                uuid::Uuid::parse_str(id)
                    .map_err(|e| ManagerError::InvalidArgument(format!("id {id}: {e}")))
            })
            .collect::<Result<Vec<_>, _>>()?;
        let body = models::BulkIdsRequest { ids };
        with_retry(&self.retry, false, || {
            api::bulk_delete_event_triggers(cfg, body.clone())
        })
        .await
        .map_err(map_manager_err)
    }

    /// Dispatch a global automation, optionally simulating a call, and return the result payload.
    pub async fn dispatch(
        &self,
        event_trigger_id: &str,
        timeout: Option<i32>,
        simulate_call: Option<bool>,
        body: std::collections::HashMap<String, serde_json::Value>,
    ) -> Result<std::collections::HashMap<String, serde_json::Value>, ManagerError> {
        let cfg = self.cfg.get().await?;
        let cfg = cfg.as_ref();
        with_retry(&self.retry, false, || {
            api::dispatch_event_trigger(
                cfg,
                event_trigger_id,
                timeout,
                simulate_call,
                Some(body.clone()),
            )
        })
        .await
        .map_err(map_manager_err)
    }
}