Skip to main content

openai_interface/containers/files/
list.rs

1//! List the files of a container via
2//! `GET /containers/{container_id}/files`.
3//!
4//! > ![warn] This module is untested!
5//! > If you encounter any issues, please report them on the repository.
6
7use url::Url;
8
9use crate::{
10    containers::files::ContainerFile,
11    errors::OapiError,
12    pagination::PaginationQuery,
13    rest::get::{Get, GetNoStream},
14};
15
16/// Lists the files of a container.
17#[derive(Debug, Clone, Default)]
18pub struct ListContainerFilesRequest<'a> {
19    /// The ID of the container, e.g. `cntr_...`.
20    pub container_id: &'a str,
21    /// The standard pagination parameters (`before`, `after`, `limit`,
22    /// `order`).
23    pub pagination: PaginationQuery<'a>,
24    /// Additional query parameters appended verbatim to the URL.
25    pub extra_query: Option<std::collections::HashMap<String, String>>,
26}
27
28impl Get for ListContainerFilesRequest<'_> {
29    /// Builds the URL for the request.
30    ///
31    /// `base_url` should be like <https://api.openai.com/v1>
32    fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
33        let mut url = Url::parse(base_url.trim_end_matches('/')).map_err(OapiError::UrlError)?;
34        url.path_segments_mut()
35            .map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
36            .push("containers")
37            .push(self.container_id)
38            .push("files");
39
40        let mut touched = false;
41        {
42            let mut pairs = url.query_pairs_mut();
43            if self.pagination.any_set() {
44                self.pagination.append_to(&mut pairs);
45                touched = true;
46            }
47            if let Some(extra_query) = &self.extra_query {
48                for (key, value) in extra_query {
49                    pairs.append_pair(key, value);
50                }
51                touched = true;
52            }
53        }
54        if !touched {
55            url.set_query(None);
56        }
57
58        Ok(url.to_string())
59    }
60}
61
62impl GetNoStream for ListContainerFilesRequest<'_> {
63    type Response = ListContainerFilesResponse;
64}
65
66/// The response of listing a container's files.
67#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
68pub struct ListContainerFilesResponse {
69    /// The container files on this page.
70    #[serde(default)]
71    pub data: Vec<ContainerFile>,
72    /// Whether more files exist after this page.
73    #[serde(default)]
74    pub has_more: Option<bool>,
75    /// The ID of the first file on the page, for cursor pagination.
76    #[serde(default)]
77    pub first_id: Option<String>,
78    /// The ID of the last file on the page, for cursor pagination.
79    #[serde(default)]
80    pub last_id: Option<String>,
81    /// The object type (`list`), if the provider sends it.
82    #[serde(default)]
83    pub object: Option<String>,
84}
85
86crate::impl_from_str!(ListContainerFilesResponse);