Skip to main content

openai_interface/uploads/
parts.rs

1//! Add an upload part via `POST /uploads/{upload_id}/parts`.
2//!
3//! > ![warn] This module is untested!
4//! > If you encounter any issues, please report them on the repository.
5//!
6//! Each part can be up to 64 MB except the last, which may be smaller.
7//! Parts must be added in order and the combined size must match the
8//! declared upload size.
9
10use url::Url;
11
12use crate::{
13    errors::OapiError,
14    rest::RequestOptions,
15    rest::post::{Post, PostNoStream},
16    uploads::UploadPart,
17};
18
19/// Adds a chunk of bytes to an upload session.
20#[derive(Debug, Clone, serde::Serialize)]
21pub struct AddUploadPartRequest<'a> {
22    /// The ID of the upload session, e.g. `upload_...`.
23    #[serde(skip)]
24    pub upload_id: &'a str,
25    /// The chunk of bytes for this part.
26    pub data: Vec<u8>,
27}
28
29impl Post for AddUploadPartRequest<'_> {
30    fn is_streaming(&self) -> bool {
31        false
32    }
33
34    /// Builds the URL for the request.
35    ///
36    /// `base_url` should be like <https://api.openai.com/v1>
37    fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
38        let mut url = Url::parse(base_url.trim_end_matches('/')).map_err(OapiError::UrlError)?;
39        url.path_segments_mut()
40            .map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
41            .push("uploads")
42            .push(self.upload_id)
43            .push("parts");
44        Ok(url.to_string())
45    }
46}
47
48impl PostNoStream for AddUploadPartRequest<'_> {
49    type Response = UploadPart;
50
51    /// Sends the part upload POST request using multipart/form-data
52    /// format with a `data` file field.
53    async fn get_response_string(
54        &self,
55        client: &reqwest::Client,
56        base_url: &str,
57        options: &RequestOptions,
58    ) -> Result<String, OapiError> {
59        let part = reqwest::multipart::Part::bytes(self.data.clone());
60        let form = reqwest::multipart::Form::new().part("data", part);
61
62        let url = self.build_url(base_url)?;
63        crate::rest::post::post_multipart_json(client, url, form, options).await
64    }
65}