use std::sync::Arc;
use crate::error::{map_manager_err, ManagerError};
use crate::gen::manager::apis::configuration::Configuration;
use crate::gen::manager::apis::file_api;
use crate::gen::manager::models;
use crate::http::{collect_all, fetch_page, Page};
use crate::retry::{with_retry, RetryPolicy};
pub struct FilesResource {
pub(crate) cfg: Arc<Configuration>,
pub(crate) retry: RetryPolicy,
}
impl FilesResource {
pub async fn list_all(&self) -> Result<Vec<models::StoredFile>, ManagerError> {
collect_all(&self.retry, map_manager_err, |page| async move {
let r = file_api::list_files(
self.cfg.as_ref(),
Some(page),
None,
None,
None,
None,
None,
None,
None,
)
.await?;
Ok((r.items, r.pagination.pages, r.pagination.current))
})
.await
}
pub async fn list_page(
&self,
page: i32,
per_page: Option<i32>,
file_type: Option<models::StorageType>,
) -> Result<Page<models::StoredFile>, ManagerError> {
fetch_page(&self.retry, map_manager_err, || async move {
let r = file_api::list_files(
self.cfg.as_ref(),
Some(page),
per_page,
None,
None,
file_type,
None,
None,
None,
)
.await?;
Ok((
r.items,
r.pagination.pages,
r.pagination.current,
Some(r.pagination.total),
))
})
.await
}
pub async fn list_by_type(
&self,
file_type: models::StorageType,
) -> Result<Vec<models::StoredFile>, ManagerError> {
let r = with_retry(&self.retry, true, || {
file_api::list_files_by_type(self.cfg.as_ref(), file_type)
})
.await
.map_err(map_manager_err)?;
Ok(r.items)
}
pub async fn backups(&self) -> Result<Vec<models::StoredFile>, ManagerError> {
let r = with_retry(&self.retry, true, || {
file_api::list_backup_files(self.cfg.as_ref())
})
.await
.map_err(map_manager_err)?;
Ok(r.items)
}
pub async fn recordings(&self) -> Result<Vec<models::StoredFile>, ManagerError> {
let r = with_retry(&self.retry, true, || {
file_api::list_recording_files(self.cfg.as_ref())
})
.await
.map_err(map_manager_err)?;
Ok(r.items)
}
pub async fn prompts(&self) -> Result<Vec<models::StoredFile>, ManagerError> {
let r = with_retry(&self.retry, true, || {
file_api::list_prompt_files(self.cfg.as_ref())
})
.await
.map_err(map_manager_err)?;
Ok(r.items)
}
pub async fn get(&self, id: &str) -> Result<models::StoredFileItemResponse, ManagerError> {
with_retry(&self.retry, true, || {
file_api::get_file(self.cfg.as_ref(), id)
})
.await
.map_err(map_manager_err)
}
pub async fn delete(&self, id: &str) -> Result<models::DefaultV2MessageResponse, ManagerError> {
with_retry(&self.retry, false, || {
file_api::delete_file(self.cfg.as_ref(), id)
})
.await
.map_err(map_manager_err)
}
pub async fn download(&self, id: &str) -> Result<(), ManagerError> {
with_retry(&self.retry, true, || {
file_api::download_file(self.cfg.as_ref(), id)
})
.await
.map_err(map_manager_err)?;
Ok(())
}
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!("id {id:?}: {e}")))
})
.collect::<Result<Vec<_>, _>>()?;
let body = models::FileBulkDeleteRequest { ids };
with_retry(&self.retry, false, || {
file_api::bulk_delete_files(self.cfg.as_ref(), body.clone())
})
.await
.map_err(map_manager_err)
}
pub async fn bulk_download(&self, ids: Vec<String>) -> Result<Vec<u8>, ManagerError> {
let ids_joined = ids.join(",");
let resp = file_api::get_bulk_file_download(self.cfg.as_ref(), &ids_joined)
.await
.map_err(map_manager_err)?;
let bytes = resp
.bytes()
.await
.map_err(|e| ManagerError::Network(e.to_string()))?
.to_vec();
Ok(bytes)
}
pub async fn bulk_download_post(&self, ids: Vec<String>) -> Result<Vec<u8>, ManagerError> {
let ids = ids
.iter()
.map(|id| {
uuid::Uuid::parse_str(id)
.map_err(|e| ManagerError::InvalidArgument(format!("id {id:?}: {e}")))
})
.collect::<Result<Vec<_>, _>>()?;
let body = models::FileBulkDownloadRequest { ids };
let resp = file_api::post_bulk_file_download(self.cfg.as_ref(), body)
.await
.map_err(map_manager_err)?;
let bytes = resp
.bytes()
.await
.map_err(|e| ManagerError::Network(e.to_string()))?
.to_vec();
Ok(bytes)
}
}