openai-interface 0.10.0

A low-level Rust interface for the OpenAI API
Documentation
//! Create a container via `POST /containers`.
//!
//! > ![warn] This module is untested!
//! > If you encounter any issues, please report them on the repository.

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

use crate::{
    containers::Container,
    errors::OapiError,
    rest::post::{Post, PostNoStream},
};

/// Creates a container.
#[derive(Debug, Clone, Serialize, Default)]
pub struct CreateContainerRequest {
    /// The name of the container.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// The IDs of files to seed the container with, as raw JSON (each
    /// entry a `{"file_id": ...}` or `{"file_id": ..., "expires_after": ...}`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub file_ids: Option<Vec<serde_json::Value>>,
    /// The expiration policy of the container, as raw JSON
    /// (`{"anchor": "last_active_at", "minutes": N}`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expires_after: Option<serde_json::Value>,
    /// 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 CreateContainerRequest {
    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("containers");
        Ok(url.to_string())
    }
}

impl PostNoStream for CreateContainerRequest {
    type Response = Container;
}