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

/// Logs — the request audit log (`/api/v2/audit/request`) and the live log (`/api/v2/logs`).
pub struct LogsResource {
    pub(crate) cfg: Arc<Configuration>,
    pub(crate) retry: RetryPolicy,
}

impl LogsResource {
    /// Collect every audit-log entry across pages.
    pub async fn audit_all(&self) -> Result<Vec<models::AuditLog>, ManagerError> {
        collect_all(&self.retry, map_manager_err, |page| async move {
            let r = api::list_audit_logs(
                self.cfg.as_ref(),
                None,
                None,
                None,
                Some(page),
                None,
                None,
                None,
                None,
            )
            .await?;
            Ok((r.items, r.pagination.pages, r.pagination.current))
        })
        .await
    }

    /// Read the live log.
    pub async fn live(&self) -> Result<Vec<models::LiveLog>, ManagerError> {
        let resp = with_retry(&self.retry, true, || {
            api::list_live_logs(self.cfg.as_ref(), None)
        })
        .await
        .map_err(map_manager_err)?;
        Ok(resp.items)
    }

    /// Enable live logging.
    pub async fn enable_live(&self) -> Result<models::DefaultV2MessageResponse, ManagerError> {
        with_retry(&self.retry, false, || {
            api::enable_live_logging(self.cfg.as_ref())
        })
        .await
        .map_err(map_manager_err)
    }

    /// Disable live logging.
    pub async fn disable_live(&self) -> Result<models::DefaultV2MessageResponse, ManagerError> {
        with_retry(&self.retry, false, || {
            api::disable_live_logging(self.cfg.as_ref())
        })
        .await
        .map_err(map_manager_err)
    }

    /// Write a single entry to the live log.
    pub async fn write(
        &self,
        body: models::WriteLogRequest,
    ) -> Result<models::GenericItemResponse, ManagerError> {
        with_retry(&self.retry, false, || {
            api::write_log(self.cfg.as_ref(), body.clone())
        })
        .await
        .map_err(map_manager_err)
    }
}