use std::path::PathBuf;
use super::{request::FileUploadPurpose, response::FileUploadResponse};
use crate::client::ZaiClient;
pub struct FileUploadRequest {
purpose: FileUploadPurpose,
file_path: PathBuf,
file_name: Option<String>,
content_type: Option<String>,
}
impl FileUploadRequest {
pub fn new(purpose: FileUploadPurpose, file_path: impl Into<PathBuf>) -> Self {
Self {
purpose,
file_path: file_path.into(),
file_name: None,
content_type: None,
}
}
pub fn with_file_name(mut self, name: impl Into<String>) -> Self {
self.file_name = Some(name.into());
self
}
pub fn with_content_type(mut self, ct: impl Into<String>) -> Self {
self.content_type = Some(ct.into());
self
}
pub async fn send_via(&self, client: &ZaiClient) -> crate::ZaiResult<FileUploadResponse> {
let route = crate::client::routes::FILES_UPLOAD;
let url = client.endpoints().resolve_route(route, &[])?;
let mut file_part =
crate::client::transport::multipart::FilePart::from_path(&self.file_path)?;
if let Some(file_name) = &self.file_name {
file_part = file_part.with_filename(file_name)?;
}
if let Some(content_type) = &self.content_type {
file_part = file_part.with_content_type(content_type)?;
}
let factory = crate::client::transport::multipart::MultipartBodyFactory::new()
.field("purpose", self.purpose.as_str())?
.file_named("file", file_part)?;
client
.send_multipart::<FileUploadResponse>(route.method(), url, &factory)
.await
}
}