Skip to main content

openai_interface/batches/
list.rs

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