Skip to main content

openai_interface/files/list/
request.rs

1//! File list request functionality
2//!
3//! This module provides functionality for listing files via GET requests,
4//! demonstrating the use of the GET request traits.
5
6use url::Url;
7
8use crate::{
9    errors::OapiError,
10    rest::get::{Get, GetNoStream},
11};
12
13/// Request parameters for listing files
14#[derive(Debug, Clone, Default)]
15pub struct ListFilesRequest {
16    /// A cursor for use in pagination.
17    ///
18    /// `after` is an object ID that defines your place
19    /// in the list. For instance, if you make a list request and receive 100 objects,
20    /// ending with obj_foo, your subsequent call can include after=obj_foo in order to
21    /// fetch the next page of the list.
22    pub after: Option<String>,
23    /// A limit on the number of objects to be returned. Limit can range between 1 and
24    /// 10,000, and the default is 10,000.
25    pub limit: Option<u32>,
26    /// Sort order by the `created_at` timestamp of the objects.
27    pub order: Option<ListOrder>,
28    /// Only return files with the given purpose.
29    pub purpose: Option<String>,
30}
31
32/// Sort order by the `created_at` timestamp of the objects. `asc` for ascending
33/// order and `desc` for descending order.
34#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
35pub enum ListOrder {
36    Asc,
37    #[default]
38    Desc,
39}
40
41impl ListOrder {
42    fn as_str(self) -> &'static str {
43        match self {
44            ListOrder::Asc => "asc",
45            ListOrder::Desc => "desc",
46        }
47    }
48}
49
50impl Get for ListFilesRequest {
51    #[allow(rustdoc::bare_urls)]
52    /// base_url should look like https://api.openai.com/v1
53    fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
54        let mut url = Url::parse(base_url.trim_end_matches('/')).map_err(OapiError::UrlError)?;
55        url.path_segments_mut()
56            .map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
57            .push("files");
58
59        // Only touch the query string when there is something to add:
60        // `query_pairs_mut` marks the query as present, which would leave a
61        // trailing `?` on the URL even without any parameter.
62        if self.purpose.is_some()
63            || self.limit.is_some()
64            || self.after.is_some()
65            || self.order.is_some()
66        {
67            let mut query_pairs = url.query_pairs_mut();
68
69            if let Some(ref purpose) = self.purpose {
70                query_pairs.append_pair("purpose", purpose);
71            }
72
73            if let Some(limit) = self.limit {
74                query_pairs.append_pair("limit", &limit.to_string());
75            }
76
77            if let Some(order) = self.order {
78                query_pairs.append_pair("order", order.as_str());
79            }
80
81            if let Some(ref after) = self.after {
82                query_pairs.append_pair("after", after);
83            }
84        }
85
86        Ok(url.to_string())
87    }
88}
89
90impl GetNoStream for ListFilesRequest {
91    type Response = super::response::ListFilesResponse;
92}