Skip to main content

openai_interface/containers/
create.rs

1//! Create a container via `POST /containers`.
2//!
3//! > ![warn] This module is untested!
4//! > If you encounter any issues, please report them on the repository.
5
6use serde::{Deserialize, Serialize};
7use url::Url;
8
9use crate::{
10    containers::Container,
11    errors::OapiError,
12    rest::post::{Post, PostNoStream},
13};
14
15crate::request_body! {
16    /// Creates a container.
17    #[derive(Debug, Clone, Serialize, Deserialize, Default)]
18    pub struct CreateContainerRequest {
19        /// The name of the container.
20        #[serde(skip_serializing_if = "Option::is_none")]
21        pub name: Option<String>,
22        /// The IDs of files to seed the container with, as raw JSON (each
23        /// entry a `{"file_id": ...}` or `{"file_id": ..., "expires_after": ...}`).
24        #[serde(skip_serializing_if = "Option::is_none")]
25        pub file_ids: Option<Vec<serde_json::Value>>,
26        /// The expiration policy of the container, as raw JSON
27        /// (`{"anchor": "last_active_at", "minutes": N}`).
28        #[serde(skip_serializing_if = "Option::is_none")]
29        pub expires_after: Option<serde_json::Value>,
30    }
31}
32
33impl Post for CreateContainerRequest {
34    fn is_streaming(&self) -> bool {
35        false
36    }
37
38    /// Builds the URL for the request.
39    ///
40    /// `base_url` should be like <https://api.openai.com/v1>
41    fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
42        let mut url = Url::parse(base_url.trim_end_matches('/')).map_err(OapiError::UrlError)?;
43        url.path_segments_mut()
44            .map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
45            .push("containers");
46        Ok(url.to_string())
47    }
48}
49
50impl PostNoStream for CreateContainerRequest {
51    type Response = Container;
52}