use crate::client::ZaiClient;
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<Vec<u8>> {
let route = crate::client::routes::FILES_GET_CONTENT;
let url = client.endpoints().resolve_route(route, &[&self.file_id])?;
let bytes = client.send_empty_bytes(route.method(), url).await?;
Ok(bytes.to_vec())
}
pub async fn send_to_via<P: AsRef<std::path::Path>>(
&self,
client: &ZaiClient,
path: P,
) -> crate::ZaiResult<usize> {
let bytes = self.send_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 mut f = tokio::fs::File::create(p).await?;
tokio::io::AsyncWriteExt::write_all(&mut f, &bytes).await?;
Ok(bytes.len())
}
}