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
33    fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
34        let mut url =
35            Url::parse(base_url.trim_end_matches('/')).map_err(|e| OapiError::UrlError(e))?;
36        url.path_segments_mut()
37            .map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
38            .push("files");
39
40        let mut has_params = false;
41        {
42            let mut query_pairs = url.query_pairs_mut();
43
44            if let Some(ref purpose) = self.purpose {
45                query_pairs.append_pair("purpose", purpose);
46                has_params = true;
47            }
48
49            if let Some(limit) = self.limit {
50                query_pairs.append_pair("limit", &limit.to_string());
51                has_params = true;
52            }
53
54            if let Some(ref after) = self.after {
55                query_pairs.append_pair("after", after);
56                has_params = true;
57            }
58        }
59
60        // Remove trailing '?' if no parameters were added
61        let url_string = url.to_string();
62        if url_string.ends_with('?') && !has_params {
63            Ok(url_string.trim_end_matches('?').to_string())
64        } else {
65            Ok(url_string)
66        }
67    }
68}
69
70impl GetNoStream for ListFilesRequest {
71    type Response = super::response::ListFilesResponse;
72}