Skip to main content

async_openai/
uploads.rs

1use crate::{
2    config::Config,
3    error::OpenAIError,
4    types::uploads::{
5        AddUploadPartRequest, CompleteUploadRequest, CreateUploadRequest, Upload, UploadPart,
6    },
7    Client, RequestOptions,
8};
9
10/// Allows you to upload large files in multiple parts.
11pub struct Uploads<'c, C: Config> {
12    client: &'c Client<C>,
13    pub(crate) request_options: RequestOptions,
14}
15
16impl<'c, C: Config> Uploads<'c, C> {
17    pub fn new(client: &'c Client<C>) -> Self {
18        Self {
19            client,
20            request_options: RequestOptions::new(),
21        }
22    }
23
24    /// Creates an intermediate [Upload](https://platform.openai.com/docs/api-reference/uploads/object) object that
25    /// you can add [Parts](https://platform.openai.com/docs/api-reference/uploads/part-object) to. Currently,
26    /// an Upload can accept at most 8 GB in total and expires after an hour after you create it.
27    ///            
28    /// Once you complete the Upload, we will create a [File](https://platform.openai.com/docs/api-reference/files/object)
29    /// object that contains all the parts you uploaded. This File is usable in the rest of our platform as a regular File object.
30    ///            
31    /// For certain `purpose` values, the correct `mime_type` must be specified. Please refer to documentation for the
32    /// [supported MIME types for your use case](https://platform.openai.com/docs/guides/tools-file-search#supported-files)
33    ///
34    /// For guidance on the proper filename extensions for each purpose, please follow the documentation on
35    /// [creating a File](https://platform.openai.com/docs/api-reference/files/create).
36    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
37    pub async fn create(&self, request: CreateUploadRequest) -> Result<Upload, OpenAIError> {
38        self.client
39            .post("/uploads", request, &self.request_options)
40            .await
41    }
42
43    /// Adds a [Part](https://platform.openai.com/docs/api-reference/uploads/part-object) to an
44    /// [Upload](https://platform.openai.com/docs/api-reference/uploads/object) object.
45    /// A Part represents a chunk of bytes from the file you are trying to upload.
46    ///
47    /// Each Part can be at most 64 MB, and you can add Parts until you hit the Upload maximum of 8 GB.
48    ///
49    /// It is possible to add multiple Parts in parallel. You can decide the intended order of the Parts
50    /// when you [complete the Upload](https://platform.openai.com/docs/api-reference/uploads/complete).
51    #[crate::byot(
52        T0 = std::fmt::Display,
53        T1 = Clone,
54        R = serde::de::DeserializeOwned,
55        where_clause =  "reqwest::multipart::Form: crate::traits::AsyncTryFrom<T1, Error = OpenAIError>, T1: crate::traits::MaybeSend + 'static")]
56    pub async fn add_part(
57        &self,
58        upload_id: &str,
59        request: AddUploadPartRequest,
60    ) -> Result<UploadPart, OpenAIError> {
61        self.client
62            .post_form(
63                &format!("/uploads/{upload_id}/parts"),
64                request,
65                &self.request_options,
66            )
67            .await
68    }
69
70    /// Completes the [Upload](https://platform.openai.com/docs/api-reference/uploads/object).
71    ///
72    /// Within the returned Upload object, there is a nested [File](https://platform.openai.com/docs/api-reference/files/object)
73    /// object that is ready to use in the rest of the platform.
74    ///
75    /// You can specify the order of the Parts by passing in an ordered list of the Part IDs.
76    ///
77    /// The number of bytes uploaded upon completion must match the number of bytes initially specified
78    /// when creating the Upload object. No Parts may be added after an Upload is completed.
79
80    #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)]
81    pub async fn complete(
82        &self,
83        upload_id: &str,
84        request: CompleteUploadRequest,
85    ) -> Result<Upload, OpenAIError> {
86        self.client
87            .post(
88                &format!("/uploads/{upload_id}/complete"),
89                request,
90                &self.request_options,
91            )
92            .await
93    }
94
95    /// Cancels the Upload. No Parts may be added after an Upload is cancelled.
96    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
97    pub async fn cancel(&self, upload_id: &str) -> Result<Upload, OpenAIError> {
98        self.client
99            .post(
100                &format!("/uploads/{upload_id}/cancel"),
101                serde_json::json!({}),
102                &self.request_options,
103            )
104            .await
105    }
106}