babelforce_manager_sdk/resources/
prompts.rs1use std::path::PathBuf;
2use std::sync::Arc;
3
4use crate::error::{map_manager_err, ManagerError};
5use crate::gen::manager::apis::configuration::Configuration;
6use crate::gen::manager::apis::{manager_api, prompt_api as api};
7use crate::gen::manager::models;
8use crate::http::collect_all;
9use crate::retry::{with_retry, RetryPolicy};
10
11pub struct PromptsResource {
13 pub(crate) cfg: Arc<Configuration>,
14 pub(crate) retry: RetryPolicy,
15}
16
17impl PromptsResource {
18 pub async fn list_all(&self) -> Result<Vec<models::Prompt>, ManagerError> {
20 collect_all(&self.retry, map_manager_err, |page| async move {
21 let r = api::list_prompts(self.cfg.as_ref(), Some(page), None).await?;
22 Ok((r.items, r.pagination.pages, r.pagination.current))
23 })
24 .await
25 }
26
27 pub async fn uses(&self, id: &str) -> Result<models::ObjectListResponse, ManagerError> {
29 with_retry(&self.retry, true, || {
30 manager_api::get_prompt_uses(self.cfg.as_ref(), id)
31 })
32 .await
33 .map_err(map_manager_err)
34 }
35
36 pub async fn get(&self, id: &str) -> Result<models::PromptItemResponse, ManagerError> {
38 with_retry(&self.retry, true, || api::get_prompt(self.cfg.as_ref(), id))
39 .await
40 .map_err(map_manager_err)
41 }
42
43 pub async fn upload(
45 &self,
46 file: PathBuf,
47 filename: Option<&str>,
48 label: Option<&str>,
49 ) -> Result<models::PromptItemResponse, ManagerError> {
50 with_retry(&self.retry, false, || {
51 manager_api::upload_prompt(self.cfg.as_ref(), file.clone(), filename, label)
52 })
53 .await
54 .map_err(map_manager_err)
55 }
56
57 pub async fn update(
59 &self,
60 id: &str,
61 body: models::RestUpdatePrompt,
62 ) -> Result<models::PromptItemResponse, ManagerError> {
63 with_retry(&self.retry, false, || {
64 api::update_prompt(self.cfg.as_ref(), id, body.clone())
65 })
66 .await
67 .map_err(map_manager_err)
68 }
69
70 pub async fn delete(&self, id: &str) -> Result<(), ManagerError> {
72 with_retry(&self.retry, false, || {
73 api::delete_prompt(self.cfg.as_ref(), id)
74 })
75 .await
76 .map_err(map_manager_err)?;
77 Ok(())
78 }
79}