Skip to main content

openai_interface/containers/files/
create.rs

1//! Create a container file via `POST /containers/{container_id}/files`.
2//!
3//! > ![warn] This module is untested!
4//! > If you encounter any issues, please report them on the repository.
5
6use url::Url;
7
8use crate::{
9    containers::files::ContainerFile,
10    errors::OapiError,
11    rest::post::{Post, PostNoStream},
12};
13
14/// Attaches a file to a container.
15///
16/// The file must already exist as a [`FileObject`](crate::files::FileObject);
17/// this request only links it into the container filesystem.
18#[derive(Debug, Clone, serde::Serialize)]
19pub struct CreateContainerFileRequest<'a> {
20    /// The ID of the container, e.g. `cntr_...`.
21    #[serde(skip)]
22    pub container_id: &'a str,
23    /// The ID of the existing [`FileObject`](crate::files::FileObject)
24    /// to attach.
25    pub file_id: String,
26}
27
28impl Post for CreateContainerFileRequest<'_> {
29    fn is_streaming(&self) -> bool {
30        false
31    }
32
33    /// Builds the URL for the request.
34    ///
35    /// `base_url` should be like <https://api.openai.com/v1>
36    fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
37        let mut url = Url::parse(base_url.trim_end_matches('/')).map_err(OapiError::UrlError)?;
38        url.path_segments_mut()
39            .map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
40            .push("containers")
41            .push(self.container_id)
42            .push("files");
43        Ok(url.to_string())
44    }
45}
46
47impl PostNoStream for CreateContainerFileRequest<'_> {
48    type Response = ContainerFile;
49}