use crate::client::ZaiClient;
pub type ByteStream = Vec<u8>;
pub struct FileContentRequest {
file_id: String,
}
impl FileContentRequest {
pub fn new(file_id: impl Into<String>) -> Self {
Self {
file_id: file_id.into(),
}
}
pub async fn send_via(&self, client: &ZaiClient) -> crate::ZaiResult<ByteStream> {
Ok(self.fetch_bytes_via(client).await?.to_vec())
}
async fn fetch_bytes_via(&self, client: &ZaiClient) -> crate::ZaiResult<bytes::Bytes> {
crate::client::validation::require_non_blank(&self.file_id, "file_id")?;
let route = crate::client::routes::FILES_GET_CONTENT;
let url = client.endpoints().resolve_route(route, &[&self.file_id])?;
client.send_empty_bytes(route.method(), url).await
}
pub async fn send_to_via<P: AsRef<std::path::Path>>(
&self,
client: &ZaiClient,
path: P,
) -> crate::ZaiResult<usize> {
let bytes = self.fetch_bytes_via(client).await?;
let p = path.as_ref();
if let Some(parent) = p.parent()
&& !parent.as_os_str().is_empty()
{
tokio::fs::create_dir_all(parent).await?;
}
let length = bytes.len();
crate::client::transport::download::atomic_download(p, bytes).await?;
Ok(length)
}
}