Skip to main content

openai_interface/uploads/
complete.rs

1//! Complete an upload via `POST /uploads/{upload_id}/complete`.
2//!
3//! > ![warn] This module is untested!
4//! > If you encounter any issues, please report them on the repository.
5//!
6//! On success the returned [`Upload`] carries the ID of the finished
7//! [`FileObject`](crate::files::FileObject) in its `file_id` field.
8
9use serde::Serialize;
10use url::Url;
11
12use crate::{
13    errors::OapiError,
14    rest::post::{Post, PostNoStream},
15    uploads::Upload,
16};
17
18/// Completes an upload session.
19#[derive(Debug, Clone, Serialize)]
20pub struct CompleteUploadRequest<'a> {
21    /// The ID of the upload session, e.g. `upload_...`.
22    #[serde(skip)]
23    pub upload_id: &'a str,
24    /// The ordered part IDs returned by the parts uploads.
25    pub part_ids: Vec<String>,
26    /// The optional MD5 hash of the file for integrity verification.
27    #[serde(skip_serializing_if = "Option::is_none")]
28    pub md5: Option<String>,
29    /// Additional JSON properties flattened into the request body, for
30    /// fields not covered by the typed struct.
31    #[serde(flatten, skip_serializing_if = "Option::is_none")]
32    pub extra_body: Option<serde_json::Map<String, serde_json::Value>>,
33}
34
35impl Post for CompleteUploadRequest<'_> {
36    fn is_streaming(&self) -> bool {
37        false
38    }
39
40    /// Builds the URL for the request.
41    ///
42    /// `base_url` should be like <https://api.openai.com/v1>
43    fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
44        let mut url = Url::parse(base_url.trim_end_matches('/')).map_err(OapiError::UrlError)?;
45        url.path_segments_mut()
46            .map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
47            .push("uploads")
48            .push(self.upload_id)
49            .push("complete");
50        Ok(url.to_string())
51    }
52}
53
54impl PostNoStream for CompleteUploadRequest<'_> {
55    type Response = Upload;
56}