Skip to main content

context69_sdk/client/groups/
documents.rs

1use context69_contracts::{
2    BatchGetDocumentsRequest, BatchGetDocumentsResponse, CreateMetadataIndexRequest, DocumentKey,
3    DocumentLookupQuery, DocumentQueryRequest, DocumentQueryResponse, DocumentResponse,
4    MetadataIndexResponse, UpdateMetadataIndexRequest,
5};
6use reqwest::Method;
7use uuid::Uuid;
8
9use super::super::Context69Client;
10use crate::{Error, client::transport::group_path};
11
12pub struct GroupDocumentsApi<'a> {
13    client: &'a Context69Client,
14    group_path: String,
15}
16impl<'a> GroupDocumentsApi<'a> {
17    pub(crate) fn new(client: &'a Context69Client, group_path: String) -> Self {
18        Self { client, group_path }
19    }
20    pub async fn query(
21        &self,
22        request: &DocumentQueryRequest,
23    ) -> Result<DocumentQueryResponse, Error> {
24        let path = group_path(&self.group_path, "/documents/query");
25        self.client
26            .execute_json(
27                self.client
28                    .authorized_request(Method::POST, &path)
29                    .await?
30                    .json(request),
31            )
32            .await
33    }
34    pub async fn get(&self, key: &DocumentKey) -> Result<DocumentResponse, Error> {
35        self.get_localized(key, None).await
36    }
37
38    pub async fn get_localized(
39        &self,
40        key: &DocumentKey,
41        locale: Option<&str>,
42    ) -> Result<DocumentResponse, Error> {
43        let path = group_path(&self.group_path, "/documents/by-external-id");
44        let query = DocumentLookupQuery {
45            source_key: key.source_key.clone(),
46            external_id: key.external_id.clone(),
47            locale: locale.map(str::to_string),
48        };
49        self.client
50            .execute_json(
51                self.client
52                    .authorized_request(Method::GET, &path)
53                    .await?
54                    .query(&query),
55            )
56            .await
57    }
58    pub async fn batch_get(
59        &self,
60        request: &BatchGetDocumentsRequest,
61    ) -> Result<BatchGetDocumentsResponse, Error> {
62        let path = group_path(&self.group_path, "/documents/batch-get");
63        self.client
64            .execute_json(
65                self.client
66                    .authorized_request(Method::POST, &path)
67                    .await?
68                    .json(request),
69            )
70            .await
71    }
72    pub async fn delete(&self, key: &DocumentKey) -> Result<(), Error> {
73        let path = group_path(&self.group_path, "/documents/by-external-id");
74        self.client
75            .execute_empty(
76                self.client
77                    .authorized_request(Method::DELETE, &path)
78                    .await?
79                    .query(key),
80            )
81            .await
82    }
83}
84
85pub struct GroupMetadataIndexesApi<'a> {
86    client: &'a Context69Client,
87    group_path: String,
88    source_key: String,
89}
90impl<'a> GroupMetadataIndexesApi<'a> {
91    pub(crate) fn new(client: &'a Context69Client, group_path: String, source_key: String) -> Self {
92        Self {
93            client,
94            group_path,
95            source_key,
96        }
97    }
98    pub async fn list(&self) -> Result<Vec<MetadataIndexResponse>, Error> {
99        let path = group_path(&self.group_path, "/metadata-indexes");
100        self.client
101            .execute_json(
102                self.client
103                    .authorized_request(Method::GET, &path)
104                    .await?
105                    .query(&[("source_key", &self.source_key)]),
106            )
107            .await
108    }
109    pub async fn create(
110        &self,
111        request: &CreateMetadataIndexRequest,
112    ) -> Result<MetadataIndexResponse, Error> {
113        let path = group_path(&self.group_path, "/metadata-indexes");
114        self.client
115            .execute_json(
116                self.client
117                    .authorized_request(Method::POST, &path)
118                    .await?
119                    .query(&[("source_key", &self.source_key)])
120                    .json(request),
121            )
122            .await
123    }
124    pub async fn update(
125        &self,
126        index_id: Uuid,
127        request: &UpdateMetadataIndexRequest,
128    ) -> Result<MetadataIndexResponse, Error> {
129        let path = group_path(&self.group_path, &format!("/metadata-indexes/{index_id}"));
130        self.client
131            .execute_json(
132                self.client
133                    .authorized_request(Method::PUT, &path)
134                    .await?
135                    .json(request),
136            )
137            .await
138    }
139    pub async fn retry(&self, index_id: Uuid) -> Result<MetadataIndexResponse, Error> {
140        let path = group_path(
141            &self.group_path,
142            &format!("/metadata-indexes/{index_id}/retry"),
143        );
144        self.client
145            .execute_json(self.client.authorized_request(Method::POST, &path).await?)
146            .await
147    }
148    pub async fn delete(&self, index_id: Uuid) -> Result<(), Error> {
149        let path = group_path(&self.group_path, &format!("/metadata-indexes/{index_id}"));
150        self.client
151            .execute_empty(
152                self.client
153                    .authorized_request(Method::DELETE, &path)
154                    .await?,
155            )
156            .await
157    }
158}