Skip to main content

openai_interface/vector_stores/
list.rs

1//! List vector stores via `GET /vector_stores`.
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    errors::OapiError,
10    pagination::{PaginationOrder, PaginationQuery},
11    rest::get::{Get, GetNoStream},
12    vector_stores::VectorStore,
13};
14
15/// Lists vector stores.
16#[derive(Debug, Clone, Default)]
17pub struct ListVectorStoresRequest<'a> {
18    /// The standard pagination parameters (`before`, `after`, `limit`,
19    /// `order`).
20    pub pagination: PaginationQuery<'a>,
21    /// The metadata filter, as raw JSON (e.g. `{"key": "value"}`).
22    pub filter: Option<serde_json::Value>,
23    /// Additional query parameters appended verbatim to the URL.
24    pub extra_query: Option<std::collections::HashMap<String, String>>,
25}
26
27impl Get for ListVectorStoresRequest<'_> {
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("vector_stores");
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(filter) = &self.filter {
45                let serialized = serde_json::to_string(filter)
46                    .map_err(|e| OapiError::ResponseError(e.to_string()))?;
47                pairs.append_pair("filter", &serialized);
48                touched = true;
49            }
50            if let Some(extra_query) = &self.extra_query {
51                for (key, value) in extra_query {
52                    pairs.append_pair(key, value);
53                }
54                touched = true;
55            }
56        }
57        if !touched {
58            url.set_query(None);
59        }
60
61        Ok(url.to_string())
62    }
63}
64
65impl GetNoStream for ListVectorStoresRequest<'_> {
66    type Response = ListVectorStoresResponse;
67}
68
69/// The response of listing vector stores.
70#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
71pub struct ListVectorStoresResponse {
72    /// The vector stores on this page.
73    #[serde(default)]
74    pub data: Vec<VectorStore>,
75    /// Whether more vector stores exist after this page.
76    #[serde(default)]
77    pub has_more: Option<bool>,
78    /// The ID of the first store on the page, for cursor pagination.
79    #[serde(default)]
80    pub first_id: Option<String>,
81    /// The ID of the last store on the page, for cursor pagination.
82    #[serde(default)]
83    pub last_id: Option<String>,
84    /// The object type (`list`), if the provider sends it.
85    #[serde(default)]
86    pub object: Option<String>,
87}
88
89crate::impl_from_str!(ListVectorStoresResponse);
90
91// `PaginationOrder` is used inside `pagination.append_to`; this alias
92// import keeps it reachable for callers of this module.
93#[allow(unused_imports)]
94use PaginationOrder as _;