rs_puff/
namespace.rs

1use reqwest::Method;
2
3use crate::{
4    Client, Error, Result,
5    params::{MultiQueryParams, QueryParams, WriteParams},
6    responses::{
7        DeleteAllResponse, HintCacheWarmResponse, MultiQueryResponse, NamespaceMetadata,
8        QueryResponse, SchemaResponse, WriteResponse,
9    },
10};
11
12pub struct Namespace<'a> {
13    client: &'a Client,
14    name: String,
15}
16
17impl<'a> Namespace<'a> {
18    pub(crate) fn new(client: &'a Client, name: String) -> Self {
19        Self { client, name }
20    }
21
22    pub fn name(&self) -> &str {
23        &self.name
24    }
25
26    fn v1_path(&self, suffix: &str) -> String {
27        format!("/v1/namespaces/{}{}", self.name, suffix)
28    }
29
30    fn v2_path(&self, suffix: &str) -> String {
31        format!("/v2/namespaces/{}{}", self.name, suffix)
32    }
33
34    pub async fn write(&self, params: WriteParams) -> Result<WriteResponse> {
35        self.client
36            .request(Method::POST, &self.v2_path(""), Some(&params))
37            .await
38    }
39
40    pub async fn query(&self, params: QueryParams) -> Result<QueryResponse> {
41        self.client
42            .request(Method::POST, &self.v2_path("/query"), Some(&params))
43            .await
44    }
45
46    pub async fn multi_query(&self, params: MultiQueryParams) -> Result<MultiQueryResponse> {
47        self.client
48            .request(Method::POST, &self.v2_path("/query"), Some(&params))
49            .await
50    }
51
52    pub async fn delete_all(&self) -> Result<DeleteAllResponse> {
53        self.client
54            .request_no_body(Method::DELETE, &self.v2_path(""))
55            .await
56    }
57
58    pub async fn metadata(&self) -> Result<NamespaceMetadata> {
59        self.client
60            .request_no_body(Method::GET, &self.v1_path("/metadata"))
61            .await
62    }
63
64    pub async fn schema(&self) -> Result<SchemaResponse> {
65        self.client
66            .request_no_body(Method::GET, &self.v1_path("/schema"))
67            .await
68    }
69
70    pub async fn hint_cache_warm(&self) -> Result<HintCacheWarmResponse> {
71        self.client
72            .request_no_body(Method::GET, &self.v1_path("/hint_cache_warm"))
73            .await
74    }
75
76    /// Check if the namespace exists.
77    ///
78    /// Returns `true` if the namespace exists, `false` if it does not (404 error).
79    /// Other errors are propagated.
80    pub async fn exists(&self) -> Result<bool> {
81        match self.metadata().await {
82            Ok(_) => Ok(true),
83            Err(Error::Api { status: 404, .. }) => Ok(false),
84            Err(e) => Err(e),
85        }
86    }
87}