babelforce-manager-sdk 0.44.0

Rust SDK for the babelforce manager APIs — auth, user & agent management, call reporting, metrics, and task automations.
Documentation
use crate::error::{map_manager_err, ManagerError};
use crate::gen::manager::apis::configuration::Configuration;
use crate::gen::manager::apis::{manager_api, prompt_api as api};
use crate::gen::manager::models;
use crate::http::collect_all;
use crate::retry::{with_retry, RetryPolicy};
use crate::token::SharedCfg;
use std::path::PathBuf;

/// Audio prompts — `/api/v2/prompts`.
pub struct PromptsResource {
    pub(crate) cfg: SharedCfg<Configuration>,
    pub(crate) retry: RetryPolicy,
}

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

    /// List the objects that use a prompt by id.
    pub async fn uses(&self, id: &str) -> Result<models::ObjectListResponse, ManagerError> {
        let cfg = self.cfg.get().await?;
        let cfg = cfg.as_ref();
        with_retry(&self.retry, true, || manager_api::get_prompt_uses(cfg, id))
            .await
            .map_err(map_manager_err)
    }

    /// Get a prompt by id.
    pub async fn get(&self, id: &str) -> Result<models::PromptItemResponse, ManagerError> {
        let cfg = self.cfg.get().await?;
        let cfg = cfg.as_ref();
        with_retry(&self.retry, true, || api::get_prompt(cfg, id))
            .await
            .map_err(map_manager_err)
    }

    /// Upload an audio prompt from a file, optionally overriding its filename and label.
    pub async fn upload(
        &self,
        file: PathBuf,
        filename: Option<&str>,
        label: Option<&str>,
    ) -> Result<models::PromptItemResponse, ManagerError> {
        let cfg = self.cfg.get().await?;
        let cfg = cfg.as_ref();
        with_retry(&self.retry, false, || {
            manager_api::upload_prompt(cfg, file.clone(), filename, label)
        })
        .await
        .map_err(map_manager_err)
    }

    /// Update a prompt's metadata.
    pub async fn update(
        &self,
        id: &str,
        body: models::RestUpdatePrompt,
    ) -> Result<models::PromptItemResponse, ManagerError> {
        let cfg = self.cfg.get().await?;
        let cfg = cfg.as_ref();
        with_retry(&self.retry, false, || {
            api::update_prompt(cfg, id, body.clone())
        })
        .await
        .map_err(map_manager_err)
    }

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