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

/// Calendars — `/api/v2/calendars`.
pub struct CalendarsResource {
    pub(crate) cfg: Arc<Configuration>,
    pub(crate) retry: RetryPolicy,
}

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

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

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

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

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

    /// Get the dates configured on a calendar.
    pub async fn get_dates(
        &self,
        id: &str,
    ) -> Result<models::CalendarDateListResponse, ManagerError> {
        with_retry(&self.retry, true, || {
            manager_api::get_calender_dates(self.cfg.as_ref(), id)
        })
        .await
        .map_err(map_manager_err)
    }

    /// Add a date to a calendar.
    pub async fn add_date(
        &self,
        id: &str,
        body: models::CalendarDateBody,
    ) -> Result<models::CalendarDateItemResponse, ManagerError> {
        with_retry(&self.retry, false, || {
            manager_api::add_calendar_date(self.cfg.as_ref(), id, body.clone())
        })
        .await
        .map_err(map_manager_err)
    }

    /// Get a single date from a calendar.
    pub async fn get_date(
        &self,
        id: &str,
        date_id: &str,
    ) -> Result<models::CalendarDateItemResponse, ManagerError> {
        with_retry(&self.retry, true, || {
            api::get_calendar_date(self.cfg.as_ref(), id, date_id)
        })
        .await
        .map_err(map_manager_err)
    }

    /// Update a date on a calendar.
    pub async fn update_date(
        &self,
        id: &str,
        date_id: &str,
        body: models::CalendarDateBody,
    ) -> Result<models::CalendarDateItemResponse, ManagerError> {
        with_retry(&self.retry, false, || {
            api::update_calendar_date(self.cfg.as_ref(), id, date_id, body.clone())
        })
        .await
        .map_err(map_manager_err)
    }

    /// Remove a date from a calendar.
    pub async fn remove_date(
        &self,
        id: &str,
        date_id: &str,
    ) -> Result<models::DefaultV2MessageResponse, ManagerError> {
        with_retry(&self.retry, false, || {
            api::remove_calendar_date(self.cfg.as_ref(), id, date_id)
        })
        .await
        .map_err(map_manager_err)
    }

    /// Test whether a date is a holiday across the account's calendars.
    pub async fn test_date(
        &self,
        date: Option<&str>,
    ) -> Result<models::GenericItemResponse, ManagerError> {
        with_retry(&self.retry, true, || {
            api::test_calendar_date(self.cfg.as_ref(), date)
        })
        .await
        .map_err(map_manager_err)
    }

    /// Bulk-delete calendars 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, || {
            api::bulk_delete_calendars(self.cfg.as_ref(), body.clone())
        })
        .await
        .map_err(map_manager_err)
    }

    /// Bulk-update calendars.
    pub async fn bulk_update(
        &self,
        body: models::CalendarBulkUpdateRequest,
    ) -> Result<models::CalendarListResponse, ManagerError> {
        with_retry(&self.retry, false, || {
            api::bulk_update_calendars(self.cfg.as_ref(), body.clone())
        })
        .await
        .map_err(map_manager_err)
    }
}