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    /// Only return files with the given purpose.
27    pub purpose: Option<String>,
28}
29
30impl Get for ListFilesRequest {
31    #[allow(rustdoc::bare_urls)]
32    /// base_url should look like https://api.openai.com/v1/ (must ends with '/')
33    fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
34        let mut url = Url::parse(base_url)
35            .expect("Failed to parse base URL")
36            .join("files")
37            .expect("Failed to join URL");
38
39        let mut has_params = false;
40        {
41            let mut query_pairs = url.query_pairs_mut();
42
43            if let Some(ref purpose) = self.purpose {
44                query_pairs.append_pair("purpose", purpose);
45                has_params = true;
46            }
47
48            if let Some(limit) = self.limit {
49                query_pairs.append_pair("limit", &limit.to_string());
50                has_params = true;
51            }
52
53            if let Some(ref after) = self.after {
54                query_pairs.append_pair("after", after);
55                has_params = true;
56            }
57        }
58
59        // Remove trailing '?' if no parameters were added
60        let url_string = url.to_string();
61        if url_string.ends_with('?') && !has_params {
62            Ok(url_string.trim_end_matches('?').to_string())
63        } else {
64            Ok(url_string)
65        }
66    }
67}
68
69impl GetNoStream for ListFilesRequest {
70    type Response = super::response::ListFilesResponse;
71}