Skip to main content

hivehub_internal_sdk/
vectorizer.rs

1use crate::{
2    client::HiveHubCloudClient,
3    error::HiveHubCloudError,
4    models::{
5        CollectionValidation, CreateCollectionRequest, CreateCollectionResponse, QuotaCheckRequest,
6        QuotaCheckResponse, UpdateUsageRequest, UpdateUsageResponse, UserCollectionsResponse,
7    },
8};
9use reqwest::Method;
10use uuid::Uuid;
11
12/// Vectorizer service client
13#[derive(Debug, Clone)]
14pub struct VectorizerClient<'a> {
15    client: &'a HiveHubCloudClient,
16}
17
18impl<'a> VectorizerClient<'a> {
19    pub(crate) fn new(client: &'a HiveHubCloudClient) -> Self {
20        Self { client }
21    }
22
23    /// Get all collections for a user
24    ///
25    /// # Example
26    /// ```no_run
27    /// # use hivehub_internal_sdk::HiveHubCloudClient;
28    /// # use uuid::Uuid;
29    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
30    /// let client = HiveHubCloudClient::new("api_key".to_string(), "http://localhost:12000".to_string())?;
31    /// let user_id = Uuid::new_v4();
32    /// let collections = client.vectorizer().get_user_collections(&user_id).await?;
33    /// println!("User has {} collections", collections.total_collections);
34    /// # Ok(())
35    /// # }
36    /// ```
37    pub async fn get_user_collections(
38        &self,
39        user_id: &Uuid,
40    ) -> Result<UserCollectionsResponse, HiveHubCloudError> {
41        let path = format!("/api/internal/vectorizer/users/{user_id}/collections");
42        self.client.request(Method::GET, &path).await
43    }
44
45    /// Validate collection ownership and quota
46    ///
47    /// # Example
48    /// ```no_run
49    /// # use hivehub_internal_sdk::HiveHubCloudClient;
50    /// # use uuid::Uuid;
51    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
52    /// let client = HiveHubCloudClient::new("api_key".to_string(), "http://localhost:12000".to_string())?;
53    /// let collection_id = Uuid::new_v4();
54    /// let user_id = Uuid::new_v4();
55    /// let validation = client.vectorizer().validate_collection(&collection_id, &user_id).await?;
56    /// if validation.valid && validation.quota.can_insert {
57    ///     println!("Collection is valid and has quota available");
58    /// }
59    /// # Ok(())
60    /// # }
61    /// ```
62    pub async fn validate_collection(
63        &self,
64        collection_id: &Uuid,
65        user_id: &Uuid,
66    ) -> Result<CollectionValidation, HiveHubCloudError> {
67        let path = format!(
68            "/api/internal/vectorizer/collections/{collection_id}/validate?user_id={user_id}"
69        );
70        self.client.request(Method::GET, &path).await
71    }
72
73    /// Request collection creation (validates quota)
74    ///
75    /// # Example
76    /// ```no_run
77    /// # use hivehub_internal_sdk::{HiveHubCloudClient, models::CreateCollectionRequest};
78    /// # use uuid::Uuid;
79    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
80    /// let client = HiveHubCloudClient::new("api_key".to_string(), "http://localhost:12000".to_string())?;
81    /// let user_id = Uuid::new_v4();
82    /// let request = CreateCollectionRequest {
83    ///     collection_name: "my-collection".to_string(),
84    ///     project_id: Uuid::new_v4(),
85    ///     dimension: 384,
86    /// };
87    /// let result = client.vectorizer().create_collection(&user_id, &request).await?;
88    /// println!("Collection created: {}", result.collection_id);
89    /// # Ok(())
90    /// # }
91    /// ```
92    pub async fn create_collection(
93        &self,
94        user_id: &Uuid,
95        request: &CreateCollectionRequest,
96    ) -> Result<CreateCollectionResponse, HiveHubCloudError> {
97        let path = format!("/api/internal/vectorizer/users/{user_id}/collections");
98        self.client
99            .request_with_body(Method::POST, &path, request)
100            .await
101    }
102
103    /// Update vector count and storage usage
104    ///
105    /// # Example
106    /// ```no_run
107    /// # use hivehub_internal_sdk::{HiveHubCloudClient, models::UpdateUsageRequest};
108    /// # use uuid::Uuid;
109    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
110    /// let client = HiveHubCloudClient::new("api_key".to_string(), "http://localhost:12000".to_string())?;
111    /// let collection_id = Uuid::new_v4();
112    /// let request = UpdateUsageRequest {
113    ///     vector_count: 5500,
114    ///     storage_bytes: 2_097_152,
115    /// };
116    /// client.vectorizer().update_usage(&collection_id, &request).await?;
117    /// # Ok(())
118    /// # }
119    /// ```
120    pub async fn update_usage(
121        &self,
122        collection_id: &Uuid,
123        request: &UpdateUsageRequest,
124    ) -> Result<UpdateUsageResponse, HiveHubCloudError> {
125        let path = format!("/api/internal/vectorizer/collections/{collection_id}/usage");
126        self.client
127            .request_with_body(Method::PUT, &path, request)
128            .await
129    }
130
131    /// Check quota for vectorizer operation
132    pub async fn check_quota(
133        &self,
134        request: &QuotaCheckRequest,
135    ) -> Result<QuotaCheckResponse, HiveHubCloudError> {
136        self.client
137            .request_with_body(Method::POST, "/api/internal/vectorizer/quota", request)
138            .await
139    }
140}
141
142#[cfg(test)]
143mod tests {
144    use super::*;
145
146    #[tokio::test]
147    async fn test_vectorizer_client_creation() {
148        let client =
149            HiveHubCloudClient::new("test_key".to_string(), "http://localhost:12000".to_string())
150                .unwrap();
151
152        let vectorizer = client.vectorizer();
153        assert!(std::mem::size_of_val(&vectorizer) > 0);
154    }
155
156    #[test]
157    fn test_vectorizer_paths() {
158        let user_id = Uuid::new_v4();
159        let collection_id = Uuid::new_v4();
160
161        let collections_path = format!("/api/internal/vectorizer/users/{user_id}/collections");
162        assert!(collections_path.contains(&user_id.to_string()));
163
164        let validate_path = format!(
165            "/api/internal/vectorizer/collections/{collection_id}/validate?user_id={user_id}"
166        );
167        assert!(validate_path.contains(&collection_id.to_string()));
168        assert!(validate_path.contains(&user_id.to_string()));
169    }
170}