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::dashboard_api;
use crate::gen::manager::models;
use crate::http::{collect_all, fetch_page, Page};
use crate::retry::{with_retry, RetryPolicy};

/// Reporting dashboards — `/api/v2/dashboards`, with per-dashboard user management.
pub struct DashboardsResource {
    pub(crate) cfg: Arc<Configuration>,
    pub(crate) retry: RetryPolicy,
}

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

    /// List one page of dashboards.
    pub async fn list_page(
        &self,
        page: i32,
        per_page: Option<i32>,
    ) -> Result<Page<models::Dashboard>, ManagerError> {
        fetch_page(&self.retry, map_manager_err, || async move {
            let r = dashboard_api::list_dashboards(
                self.cfg.as_ref(),
                Some(page),
                per_page,
                None,
                None,
                None,
                None,
            )
            .await?;
            Ok((
                r.items,
                r.pagination.pages,
                r.pagination.current,
                Some(r.pagination.total),
            ))
        })
        .await
    }

    /// Create a dashboard.
    pub async fn create(
        &self,
        body: models::DashboardCreateBody,
    ) -> Result<models::DashboardItemResponse, ManagerError> {
        with_retry(&self.retry, false, || {
            dashboard_api::create_dashboard(self.cfg.as_ref(), body.clone())
        })
        .await
        .map_err(map_manager_err)
    }

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

    /// Update a dashboard.
    pub async fn update(
        &self,
        id: &str,
        body: models::DashboardUpdateBody,
    ) -> Result<models::DashboardItemResponse, ManagerError> {
        with_retry(&self.retry, false, || {
            dashboard_api::update_dashboard(self.cfg.as_ref(), id, body.clone())
        })
        .await
        .map_err(map_manager_err)
    }

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

    /// List the users allowed to access a dashboard.
    pub async fn list_users(
        &self,
        id: &str,
    ) -> Result<models::DashboardUsersResponse, ManagerError> {
        with_retry(&self.retry, true, || {
            dashboard_api::list_dashboard_users(self.cfg.as_ref(), id)
        })
        .await
        .map_err(map_manager_err)
    }

    /// Add an existing reporter user (by email) to a dashboard's allowed users.
    pub async fn add_user(
        &self,
        id: &str,
        email: &str,
    ) -> Result<models::DefaultV2MessageResponse, ManagerError> {
        let body = models::DashboardUserAddRequest {
            email: email.to_string(),
        };
        with_retry(&self.retry, false, || {
            dashboard_api::add_dashboard_user(self.cfg.as_ref(), id, body.clone())
        })
        .await
        .map_err(map_manager_err)
    }

    /// Remove a user from a dashboard's allowed users.
    pub async fn remove_user(
        &self,
        id: &str,
        user_id: &str,
    ) -> Result<models::DefaultV2MessageResponse, ManagerError> {
        with_retry(&self.retry, false, || {
            dashboard_api::remove_dashboard_user(self.cfg.as_ref(), id, user_id)
        })
        .await
        .map_err(map_manager_err)
    }
}