babelforce-manager-sdk 0.42.1

Rust SDK for the babelforce manager APIs — auth, user & agent management, call reporting, metrics, and task automations.
Documentation
use std::sync::Arc;

use crate::error::{map_manager_err, ManagerError};
use crate::gen::manager::apis::configuration::Configuration;
use crate::gen::manager::apis::{manager_api, trigger_api as api};
use crate::gen::manager::models;
use crate::http::collect_all;
use crate::retry::{with_retry, RetryPolicy};

/// Workflow triggers — `/api/v2/triggers`.
pub struct TriggersResource {
    pub(crate) cfg: Arc<Configuration>,
    pub(crate) retry: RetryPolicy,
}

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

    /// Create a trigger.
    pub async fn create(
        &self,
        body: models::RestCreateTrigger,
    ) -> Result<models::TriggerItemResponse, ManagerError> {
        with_retry(&self.retry, false, || {
            api::create_trigger(self.cfg.as_ref(), body.clone())
        })
        .await
        .map_err(map_manager_err)
    }

    /// Get a trigger by id.
    pub async fn get(&self, id: &str) -> Result<models::TriggerItemResponse, ManagerError> {
        with_retry(&self.retry, true, || {
            api::get_trigger(self.cfg.as_ref(), id)
        })
        .await
        .map_err(map_manager_err)
    }

    /// Update a trigger.
    pub async fn update(
        &self,
        id: &str,
        body: models::RestUpdateTrigger,
    ) -> Result<models::TriggerItemResponse, ManagerError> {
        with_retry(&self.retry, false, || {
            api::update_trigger(self.cfg.as_ref(), id, body.clone())
        })
        .await
        .map_err(map_manager_err)
    }

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

    /// Clone a trigger; returns the new trigger.
    pub async fn clone(&self, id: &str) -> Result<models::TriggerItemResponse, ManagerError> {
        with_retry(&self.retry, false, || {
            api::clone_trigger(self.cfg.as_ref(), id)
        })
        .await
        .map_err(map_manager_err)
    }

    /// Test trigger conditions against a sample payload. `test_mode` runs without side effects.
    pub async fn test(
        &self,
        body: models::TestTriggersRequest,
        test_mode: bool,
    ) -> Result<models::TestTriggersResponse, ManagerError> {
        with_retry(&self.retry, false, || {
            manager_api::test_triggers(self.cfg.as_ref(), test_mode, Some(body.clone()))
        })
        .await
        .map_err(map_manager_err)
    }

    /// Apply a bulk action (e.g. enable/disable/delete) to triggers by id.
    pub async fn bulk_action(
        &self,
        action: &str,
        ids: Vec<String>,
    ) -> Result<models::BulkActionResponse, ManagerError> {
        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_trigger_action(self.cfg.as_ref(), action, body.clone())
        })
        .await
        .map_err(map_manager_err)
    }

    /// List the available trigger expressions.
    pub async fn expressions(&self) -> Result<models::ObjectListResponse, ManagerError> {
        with_retry(&self.retry, true, || {
            api::list_trigger_expressions(self.cfg.as_ref())
        })
        .await
        .map_err(map_manager_err)
    }

    /// List the available trigger operators.
    pub async fn operators(&self) -> Result<models::ObjectListResponse, ManagerError> {
        with_retry(&self.retry, true, || {
            api::list_trigger_operators(self.cfg.as_ref())
        })
        .await
        .map_err(map_manager_err)
    }

    /// List the conditions configured on a trigger.
    pub async fn conditions(
        &self,
        id: &str,
    ) -> Result<models::TriggerConditionListResponse, ManagerError> {
        with_retry(&self.retry, true, || {
            api::list_trigger_conditions(self.cfg.as_ref(), id)
        })
        .await
        .map_err(map_manager_err)
    }

    /// Replace the conditions on a trigger.
    pub async fn set_conditions(
        &self,
        id: &str,
        body: models::TriggerConditionsRequest,
    ) -> Result<models::TriggerItemResponse, ManagerError> {
        with_retry(&self.retry, false, || {
            api::set_trigger_conditions(self.cfg.as_ref(), id, body.clone())
        })
        .await
        .map_err(map_manager_err)
    }

    /// List where a trigger is used.
    pub async fn uses(&self, id: &str) -> Result<models::TriggerUsesResponse, ManagerError> {
        with_retry(&self.retry, true, || {
            api::get_trigger_uses(self.cfg.as_ref(), id)
        })
        .await
        .map_err(map_manager_err)
    }
}