babelforce-manager-sdk 0.42.3

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::{conversation_api as api, conversations_api as sub};
use crate::gen::manager::models;
use crate::http::collect_all;
use crate::retry::{with_retry, RetryPolicy};

/// Conversations — `/api/v2/conversations`, including their events and session variables.
pub struct ConversationsResource {
    pub(crate) cfg: Arc<Configuration>,
    pub(crate) retry: RetryPolicy,
}

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

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

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

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

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

    /// List a conversation's events.
    pub async fn events(
        &self,
        conversation_id: &str,
    ) -> Result<Vec<models::ConversationEvent>, ManagerError> {
        let resp = with_retry(&self.retry, true, || {
            sub::list_conversation_events(self.cfg.as_ref(), conversation_id)
        })
        .await
        .map_err(map_manager_err)?;
        Ok(resp.items)
    }

    /// Add an event to a conversation.
    pub async fn add_event(
        &self,
        conversation_id: &str,
        body: models::ConversationEventRequest,
    ) -> Result<models::ConversationEventItemResponse, ManagerError> {
        with_retry(&self.retry, false, || {
            sub::add_conversation_event(self.cfg.as_ref(), conversation_id, body.clone())
        })
        .await
        .map_err(map_manager_err)
    }

    /// Open a conversation.
    pub async fn open(
        &self,
        conversation_id: &str,
    ) -> Result<models::ConversationItemResponse, ManagerError> {
        with_retry(&self.retry, false, || {
            sub::open_conversation(self.cfg.as_ref(), conversation_id)
        })
        .await
        .map_err(map_manager_err)
    }

    /// Close a conversation.
    pub async fn close(
        &self,
        conversation_id: &str,
    ) -> Result<models::ConversationItemResponse, ManagerError> {
        with_retry(&self.retry, false, || {
            sub::close_conversation(self.cfg.as_ref(), conversation_id)
        })
        .await
        .map_err(map_manager_err)
    }

    /// Get a conversation's first event.
    pub async fn first_event(
        &self,
        conversation_id: &str,
    ) -> Result<models::ConversationEventItemResponse, ManagerError> {
        with_retry(&self.retry, true, || {
            sub::get_first_conversation_event(self.cfg.as_ref(), conversation_id)
        })
        .await
        .map_err(map_manager_err)
    }

    /// Get a conversation's latest event.
    pub async fn latest_event(
        &self,
        conversation_id: &str,
    ) -> Result<models::ConversationEventItemResponse, ManagerError> {
        with_retry(&self.retry, true, || {
            sub::get_latest_conversation_event(self.cfg.as_ref(), conversation_id)
        })
        .await
        .map_err(map_manager_err)
    }

    /// List events across all conversations (auto-paginated).
    pub async fn all_events(&self) -> Result<Vec<models::ConversationEvent>, ManagerError> {
        collect_all(&self.retry, map_manager_err, |page| async move {
            let r = sub::list_all_conversation_events(self.cfg.as_ref(), Some(page), None).await?;
            Ok((r.items, r.pagination.pages, r.pagination.current))
        })
        .await
    }

    /// Get a conversation's session variables.
    pub async fn get_session(
        &self,
        conversation_id: &str,
    ) -> Result<models::ConversationSessionVariablesItemResponse, ManagerError> {
        with_retry(&self.retry, true, || {
            sub::get_conversation_session(self.cfg.as_ref(), conversation_id)
        })
        .await
        .map_err(map_manager_err)
    }

    /// Update a conversation's session variables.
    pub async fn update_session(
        &self,
        conversation_id: &str,
        variables: std::collections::HashMap<String, serde_json::Value>,
    ) -> Result<models::ConversationSessionVariablesItemResponse, ManagerError> {
        with_retry(&self.retry, false, || {
            sub::update_conversation_session(
                self.cfg.as_ref(),
                conversation_id,
                Some(variables.clone()),
            )
        })
        .await
        .map_err(map_manager_err)
    }
}