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

/// Phonebook entries — `/api/v2/phonebook`.
pub struct PhonebookResource {
    pub(crate) cfg: Arc<Configuration>,
    pub(crate) retry: RetryPolicy,
}

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

    /// Create a phonebook entry.
    pub async fn create(
        &self,
        body: models::RestCreatePhonebookEntry,
    ) -> Result<models::PhonebookEntryItemResponse, ManagerError> {
        with_retry(&self.retry, false, || {
            api::create_phonebook_entry(self.cfg.as_ref(), body.clone())
        })
        .await
        .map_err(map_manager_err)
    }

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

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

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

    /// Download all phonebook entries as CSV text.
    pub async fn download(&self) -> Result<String, ManagerError> {
        with_retry(&self.retry, true, || {
            manager_api::download_phonebook_entries(self.cfg.as_ref())
        })
        .await
        .map_err(map_manager_err)
    }

    /// Bulk-upload phonebook entries from a CSV file. `overwrite` replaces existing entries.
    pub async fn upload(&self, file: PathBuf, overwrite: bool) -> Result<(), ManagerError> {
        with_retry(&self.retry, false, || {
            manager_api::upload_phonebook_entries(self.cfg.as_ref(), file.clone(), Some(overwrite))
        })
        .await
        .map_err(map_manager_err)?;
        Ok(())
    }

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