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::rest::get::{Get, GetNoStream};
9
10/// Request parameters for listing files
11#[derive(Debug, Clone, Default)]
12pub struct ListFilesRequest {
13    /// A cursor for use in pagination.
14    ///
15    /// `after` is an object ID that defines your place
16    /// in the list. For instance, if you make a list request and receive 100 objects,
17    /// ending with obj_foo, your subsequent call can include after=obj_foo in order to
18    /// fetch the next page of the list.
19    pub after: Option<String>,
20    /// A limit on the number of objects to be returned. Limit can range between 1 and
21    /// 10,000, and the default is 10,000.
22    pub limit: Option<u32>,
23    /// Only return files with the given purpose.
24    pub purpose: Option<String>,
25}
26
27impl Get for ListFilesRequest {
28    /// base_url should look like https://api.openai.com/v1/ (must end with '/')
29    fn build_url(&self, base_url: &str) -> String {
30        let mut url = Url::parse(base_url)
31            .expect("Failed to parse base URL")
32            .join("files")
33            .expect("Failed to join URL");
34
35        let mut has_params = false;
36        {
37            let mut query_pairs = url.query_pairs_mut();
38
39            if let Some(ref purpose) = self.purpose {
40                query_pairs.append_pair("purpose", purpose);
41                has_params = true;
42            }
43
44            if let Some(limit) = self.limit {
45                query_pairs.append_pair("limit", &limit.to_string());
46                has_params = true;
47            }
48
49            if let Some(ref after) = self.after {
50                query_pairs.append_pair("after", after);
51                has_params = true;
52            }
53        }
54
55        // Remove trailing '?' if no parameters were added
56        let url_string = url.to_string();
57        if url_string.ends_with('?') && !has_params {
58            url_string.trim_end_matches('?').to_string()
59        } else {
60            url_string
61        }
62    }
63}
64
65impl GetNoStream for ListFilesRequest {
66    type Response = super::response::ListFilesResponse;
67}