zeroentropy_community/resources/
collections.rs

1use crate::client::Client;
2use crate::error::Result;
3use crate::types::{CollectionListResponse, CollectionResponse};
4use serde::Serialize;
5
6/// Collections resource for managing document collections
7pub struct Collections<'a> {
8    client: &'a Client,
9}
10
11impl<'a> Collections<'a> {
12    pub(crate) fn new(client: &'a Client) -> Self {
13        Self { client }
14    }
15
16    /// Add a new collection
17    ///
18    /// # Arguments
19    /// * `collection_name` - Name of the collection to create
20    ///
21    /// # Example
22    /// ```no_run
23    /// # use zeroentropy_community::Client;
24    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
25    /// let client = Client::from_env()?;
26    /// client.collections().add("my_collection").await?;
27    /// # Ok(())
28    /// # }
29    /// ```
30    pub async fn add(&self, collection_name: impl Into<String>) -> Result<CollectionResponse> {
31        #[derive(Serialize)]
32        struct Request {
33            collection_name: String,
34        }
35
36        let body = Request {
37            collection_name: collection_name.into(),
38        };
39
40        self.client.post("/collections/add-collection", &body).await
41    }
42
43    /// Delete a collection
44    ///
45    /// # Arguments
46    /// * `collection_name` - Name of the collection to delete
47    ///
48    /// # Example
49    /// ```no_run
50    /// # use zeroentropy_community::Client;
51    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
52    /// let client = Client::from_env()?;
53    /// client.collections().delete("my_collection").await?;
54    /// # Ok(())
55    /// # }
56    /// ```
57    pub async fn delete(&self, collection_name: impl Into<String>) -> Result<CollectionResponse> {
58        #[derive(Serialize)]
59        struct Request {
60            collection_name: String,
61        }
62
63        let body = Request {
64            collection_name: collection_name.into(),
65        };
66
67        self.client.post("/collections/delete-collection", &body).await
68    }
69
70    /// Get list of all collections
71    ///
72    /// # Example
73    /// ```no_run
74    /// # use zeroentropy_community::Client;
75    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
76    /// let client = Client::from_env()?;
77    /// let response = client.collections().get_list().await?;
78    /// for collection in response.collections {
79    ///     println!("Collection: {}", collection);
80    /// }
81    /// # Ok(())
82    /// # }
83    /// ```
84    pub async fn get_list(&self) -> Result<CollectionListResponse> {
85        self.client.post("/collections/get-collection-list", &serde_json::json!({})).await
86    }
87}