openai-interface 0.11.0

A low-level Rust interface for the OpenAI API
Documentation
//! Complete an upload via `POST /uploads/{upload_id}/complete`.
//!
//! > ![warn] This module is untested!
//! > If you encounter any issues, please report them on the repository.
//!
//! On success the returned [`Upload`] carries the ID of the finished
//! [`FileObject`](crate::files::FileObject) in its `file_id` field.

use serde::Serialize;
use url::Url;

use crate::{
    errors::OapiError,
    rest::post::{Post, PostNoStream},
    uploads::Upload,
};

/// Completes an upload session.
#[derive(Debug, Clone, Serialize)]
pub struct CompleteUploadRequest<'a> {
    /// The ID of the upload session, e.g. `upload_...`.
    #[serde(skip)]
    pub upload_id: &'a str,
    /// The ordered part IDs returned by the parts uploads.
    pub part_ids: Vec<String>,
    /// The optional MD5 hash of the file for integrity verification.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub md5: Option<String>,
    /// Additional JSON properties flattened into the request body, for
    /// fields not covered by the typed struct.
    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    pub extra_body: Option<serde_json::Map<String, serde_json::Value>>,
}

impl Post for CompleteUploadRequest<'_> {
    fn is_streaming(&self) -> bool {
        false
    }

    /// Builds the URL for the request.
    ///
    /// `base_url` should be like <https://api.openai.com/v1>
    fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
        let mut url = Url::parse(base_url.trim_end_matches('/')).map_err(OapiError::UrlError)?;
        url.path_segments_mut()
            .map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
            .push("uploads")
            .push(self.upload_id)
            .push("complete");
        Ok(url.to_string())
    }
}

impl PostNoStream for CompleteUploadRequest<'_> {
    type Response = Upload;
}