use reqwest::Method;
use serde_json::json;
use crate::client::Client;
use crate::error::OpenAIError;
use crate::request::{MultipartField, RequestOptions};
use crate::types::uploads::{Upload, UploadCreateParams, UploadPart};
#[derive(Debug, Clone)]
pub struct Uploads {
client: Client,
}
impl Uploads {
pub(crate) fn new(client: Client) -> Self {
Self { client }
}
pub async fn create(&self, params: UploadCreateParams) -> Result<Upload, OpenAIError> {
let body = serde_json::to_value(¶ms)?;
self.client
.execute(Method::POST, "/uploads", RequestOptions::json(body))
.await
}
pub async fn add_part(
&self,
upload_id: &str,
data: Vec<u8>,
) -> Result<UploadPart, OpenAIError> {
let fields = vec![MultipartField::File {
name: "data".into(),
filename: "part".into(),
bytes: data,
content_type: None,
}];
self.client
.execute(
Method::POST,
&format!("/uploads/{upload_id}/parts"),
RequestOptions::multipart(fields),
)
.await
}
pub async fn complete(
&self,
upload_id: &str,
part_ids: Vec<String>,
md5: Option<&str>,
) -> Result<Upload, OpenAIError> {
let mut body = json!({ "part_ids": part_ids });
if let Some(md5) = md5 {
body["md5"] = json!(md5);
}
self.client
.execute(
Method::POST,
&format!("/uploads/{upload_id}/complete"),
RequestOptions::json(body),
)
.await
}
pub async fn cancel(&self, upload_id: &str) -> Result<Upload, OpenAIError> {
self.client
.execute(
Method::POST,
&format!("/uploads/{upload_id}/cancel"),
RequestOptions::default(),
)
.await
}
}