openai-interface 0.14.0

A low-level Rust interface for the OpenAI API
Documentation
//! Add an upload part via `POST /uploads/{upload_id}/parts`.
//!
//! > ![warn] This module is untested!
//! > If you encounter any issues, please report them on the repository.
//!
//! Each part can be up to 64 MB except the last, which may be smaller.
//! Parts must be added in order and the combined size must match the
//! declared upload size.

use url::Url;

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

/// Adds a chunk of bytes to an upload session.
#[derive(Debug, Clone, serde::Serialize)]
pub struct AddUploadPartRequest<'a> {
    /// The ID of the upload session, e.g. `upload_...`.
    #[serde(skip)]
    pub upload_id: &'a str,
    /// The chunk of bytes for this part.
    pub data: Vec<u8>,
}

impl Post for AddUploadPartRequest<'_> {
    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("parts");
        Ok(url.to_string())
    }
}

impl PostNoStream for AddUploadPartRequest<'_> {
    type Response = UploadPart;

    /// Sends the part upload POST request using multipart/form-data
    /// format with a `data` file field.
    async fn get_response_string(
        &self,
        client: &reqwest::Client,
        base_url: &str,
        options: &RequestOptions,
    ) -> Result<String, OapiError> {
        let part = reqwest::multipart::Part::bytes(self.data.clone());
        let form = reqwest::multipart::Form::new().part("data", part);

        let url = self.build_url(base_url)?;
        crate::rest::post::post_multipart_json(client, url, form, options).await
    }
}