Skip to main content

context69_sdk/client/groups/
documents.rs

1use context69_contracts::{
2    BatchGetDocumentsRequest, BatchGetDocumentsResponse, CreateMetadataIndexRequest, DocumentKey,
3    DocumentQueryRequest, DocumentQueryResponse, DocumentResponse, MetadataIndexResponse,
4    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        let path = group_path(&self.group_path, "/documents/by-external-id");
36        self.client
37            .execute_json(
38                self.client
39                    .authorized_request(Method::GET, &path)
40                    .await?
41                    .query(key),
42            )
43            .await
44    }
45    pub async fn batch_get(
46        &self,
47        request: &BatchGetDocumentsRequest,
48    ) -> Result<BatchGetDocumentsResponse, Error> {
49        let path = group_path(&self.group_path, "/documents/batch-get");
50        self.client
51            .execute_json(
52                self.client
53                    .authorized_request(Method::POST, &path)
54                    .await?
55                    .json(request),
56            )
57            .await
58    }
59    pub async fn delete(&self, key: &DocumentKey) -> Result<(), Error> {
60        let path = group_path(&self.group_path, "/documents/by-external-id");
61        self.client
62            .execute_empty(
63                self.client
64                    .authorized_request(Method::DELETE, &path)
65                    .await?
66                    .query(key),
67            )
68            .await
69    }
70}
71
72pub struct GroupMetadataIndexesApi<'a> {
73    client: &'a Context69Client,
74    group_path: String,
75    source_key: String,
76}
77impl<'a> GroupMetadataIndexesApi<'a> {
78    pub(crate) fn new(client: &'a Context69Client, group_path: String, source_key: String) -> Self {
79        Self {
80            client,
81            group_path,
82            source_key,
83        }
84    }
85    pub async fn list(&self) -> Result<Vec<MetadataIndexResponse>, Error> {
86        let path = group_path(&self.group_path, "/metadata-indexes");
87        self.client
88            .execute_json(
89                self.client
90                    .authorized_request(Method::GET, &path)
91                    .await?
92                    .query(&[("source_key", &self.source_key)]),
93            )
94            .await
95    }
96    pub async fn create(
97        &self,
98        request: &CreateMetadataIndexRequest,
99    ) -> Result<MetadataIndexResponse, Error> {
100        let path = group_path(&self.group_path, "/metadata-indexes");
101        self.client
102            .execute_json(
103                self.client
104                    .authorized_request(Method::POST, &path)
105                    .await?
106                    .query(&[("source_key", &self.source_key)])
107                    .json(request),
108            )
109            .await
110    }
111    pub async fn update(
112        &self,
113        index_id: Uuid,
114        request: &UpdateMetadataIndexRequest,
115    ) -> Result<MetadataIndexResponse, Error> {
116        let path = group_path(&self.group_path, &format!("/metadata-indexes/{index_id}"));
117        self.client
118            .execute_json(
119                self.client
120                    .authorized_request(Method::PUT, &path)
121                    .await?
122                    .json(request),
123            )
124            .await
125    }
126    pub async fn retry(&self, index_id: Uuid) -> Result<MetadataIndexResponse, Error> {
127        let path = group_path(
128            &self.group_path,
129            &format!("/metadata-indexes/{index_id}/retry"),
130        );
131        self.client
132            .execute_json(self.client.authorized_request(Method::POST, &path).await?)
133            .await
134    }
135    pub async fn delete(&self, index_id: Uuid) -> Result<(), Error> {
136        let path = group_path(&self.group_path, &format!("/metadata-indexes/{index_id}"));
137        self.client
138            .execute_empty(
139                self.client
140                    .authorized_request(Method::DELETE, &path)
141                    .await?,
142            )
143            .await
144    }
145}