nullnet_libdatastore/client.rs
1use crate::datastore::store_service_client::StoreServiceClient;
2#[allow(clippy::wildcard_imports)]
3use crate::datastore::*;
4use crate::utils::{authorize_request, validate_response_and_convert_to_reponse_data};
5use crate::{DatastoreConfig, ResponseData};
6use nullnet_liberror::{Error, ErrorHandler, Location, location};
7use tonic::transport::Channel;
8
9/// A client for interacting with the datastore service.
10#[derive(Debug, Clone)]
11pub struct DatastoreClient {
12 /// Datastore gRPC endpoint
13 client: StoreServiceClient<Channel>,
14}
15
16impl DatastoreClient {
17 /// Creates a new instance of `DatastoreClient`.
18 ///
19 /// # Arguments
20 /// * `config` - The configuration settings for connecting to the datastore.
21 #[allow(clippy::missing_errors_doc)]
22 pub async fn new(config: DatastoreConfig) -> Result<Self, Error> {
23 let client = config.connect().await?;
24 Ok(Self { client })
25 }
26
27 /// Logs in to the datastore service with the provided request.
28 ///
29 /// # Arguments
30 /// * `request` - The login request containing the necessary credentials.
31 ///
32 /// # Returns
33 /// * `Ok(LoginResponse)` - The response received after a successful login.
34 /// * `Err(Error)` - If the login fails or if an error occurs during the process.
35 #[allow(clippy::missing_errors_doc)]
36 pub async fn login(&mut self, request: LoginRequest) -> Result<LoginResponse, Error> {
37 let response = self.client.login(request).await.handle_err(location!())?;
38
39 Ok(response.into_inner())
40 }
41
42 /// Creates multiple records in the datastore with the provided request.
43 ///
44 /// # Arguments
45 /// * `request` - The batch create request containing the records to be created.
46 /// * `token` - The authorization token to authorize the request.
47 ///
48 /// # Returns
49 /// * `Ok(ResponseData)` - The response data containing the result of the operation.
50 /// * `Err(Error)` - If the operation fails or if an error occurs during the process.
51 #[allow(clippy::missing_errors_doc)]
52 pub async fn batch_create(
53 &mut self,
54 request: BatchCreateRequest,
55 token: &str,
56 ) -> Result<ResponseData, Error> {
57 let request = authorize_request(request, token)?;
58
59 let response = self
60 .client
61 .batch_create(request)
62 .await
63 .handle_err(location!())?;
64
65 validate_response_and_convert_to_reponse_data(response.get_ref())
66 }
67
68 /// Creates a single record in the datastore with the provided request.
69 ///
70 /// # Arguments
71 /// * `request` - The create request containing the record to be created.
72 /// * `token` - The authorization token to authorize the request.
73 ///
74 /// # Returns
75 /// * `Ok(ResponseData)` - The response data containing the result of the operation.
76 /// * `Err(Error)` - If the operation fails or if an error occurs during the process.
77 #[allow(clippy::missing_errors_doc)]
78 pub async fn create(
79 &mut self,
80 request: CreateRequest,
81 token: &str,
82 ) -> Result<ResponseData, Error> {
83 let request = authorize_request(request, token)?;
84
85 let response = self.client.create(request).await.handle_err(location!())?;
86
87 validate_response_and_convert_to_reponse_data(response.get_ref())
88 }
89
90 /// Deletes a record from the datastore with the provided request.
91 ///
92 /// # Arguments
93 /// * `request` - The delete request containing the identifier of the record to be deleted.
94 /// * `token` - The authorization token to authorize the request.
95 ///
96 /// # Returns
97 /// * `Ok(ResponseData)` - The response data containing the result of the operation.
98 /// * `Err(Error)` - If the operation fails or if an error occurs during the process.
99 #[allow(clippy::missing_errors_doc)]
100 pub async fn delete(
101 &mut self,
102 request: DeleteRequest,
103 token: &str,
104 ) -> Result<ResponseData, Error> {
105 let request = authorize_request(request, token)?;
106
107 let response = self.client.delete(request).await.handle_err(location!())?;
108
109 validate_response_and_convert_to_reponse_data(response.get_ref())
110 }
111
112 /// Deletes multiple records from the datastore with the provided request.
113 ///
114 /// # Arguments
115 /// * `request` - The batch delete request containing the identifiers of the records to be deleted.
116 /// * `token` - The authorization token to authorize the request.
117 ///
118 /// # Returns
119 /// * `Ok(ResponseData)` - The response data containing the result of the operation.
120 /// * `Err(Error)` - If the operation fails or if an error occurs during the process.
121 #[allow(clippy::missing_errors_doc)]
122 pub async fn batch_delete(
123 &mut self,
124 request: BatchDeleteRequest,
125 token: &str,
126 ) -> Result<ResponseData, Error> {
127 let request = authorize_request(request, token)?;
128
129 let response = self
130 .client
131 .batch_delete(request)
132 .await
133 .handle_err(location!())?;
134
135 validate_response_and_convert_to_reponse_data(response.get_ref())
136 }
137
138 /// Updates a record in the datastore with the provided request.
139 ///
140 /// # Arguments
141 /// * `request` - The update request containing the record's updated data.
142 /// * `token` - The authorization token to authorize the request.
143 ///
144 /// # Returns
145 /// * `Ok(ResponseData)` - The response data containing the result of the operation.
146 /// * `Err(Error)` - If the operation fails or if an error occurs during the process.
147 #[allow(clippy::missing_errors_doc)]
148 pub async fn update(
149 &mut self,
150 request: UpdateRequest,
151 token: &str,
152 ) -> Result<ResponseData, Error> {
153 let request = authorize_request(request, token)?;
154
155 let response = self.client.update(request).await.handle_err(location!())?;
156
157 validate_response_and_convert_to_reponse_data(response.get_ref())
158 }
159
160 /// Updates multiple records in the datastore with the provided request.
161 ///
162 /// # Arguments
163 /// * `request` - The batch update request containing the updated data for multiple records.
164 /// * `token` - The authorization token to authorize the request.
165 ///
166 /// # Returns
167 /// * `Ok(ResponseData)` - The response data containing the result of the operation.
168 /// * `Err(Error)` - If the operation fails or if an error occurs during the process.
169 #[allow(clippy::missing_errors_doc)]
170 pub async fn batch_update(
171 &mut self,
172 request: BatchUpdateRequest,
173 token: &str,
174 ) -> Result<ResponseData, Error> {
175 let request = authorize_request(request, token)?;
176
177 let response = self
178 .client
179 .batch_update(request)
180 .await
181 .handle_err(location!())?;
182
183 validate_response_and_convert_to_reponse_data(response.get_ref())
184 }
185
186 /// Retrieves records from the datastore based on the specified filter.
187 ///
188 /// # Arguments
189 /// * `request` - The request containing the filter criteria.
190 /// * `token` - The authorization token to authorize the request.
191 ///
192 /// # Returns
193 /// * `Ok(ResponseData)` - The response data containing the records that match the filter.
194 /// * `Err(Error)` - If the operation fails or if an error occurs during the process.
195 #[allow(clippy::missing_errors_doc)]
196 pub async fn get_by_filter(
197 &mut self,
198 request: GetByFilterRequest,
199 token: &str,
200 ) -> Result<ResponseData, Error> {
201 let request = authorize_request(request, token)?;
202
203 let response = self
204 .client
205 .get_by_filter(request)
206 .await
207 .handle_err(location!())?;
208
209 validate_response_and_convert_to_reponse_data(response.get_ref())
210 }
211
212 /// Performs aggregation on records in the datastore based on the provided request.
213 ///
214 /// # Arguments
215 /// * `request` - The aggregation request specifying the criteria for aggregation.
216 /// * `token` - The authorization token to authorize the request.
217 ///
218 /// # Returns
219 /// * `Ok(ResponseData)` - The response data containing the result of the aggregation.
220 /// * `Err(Error)` - If the operation fails or if an error occurs during the process.
221 #[allow(clippy::missing_errors_doc)]
222 pub async fn aggregate(
223 &mut self,
224 request: AggregateRequest,
225 token: &str,
226 ) -> Result<ResponseData, Error> {
227 let request = authorize_request(request, token)?;
228
229 let response = self
230 .client
231 .aggregate(request)
232 .await
233 .handle_err(location!())?;
234
235 validate_response_and_convert_to_reponse_data(response.get_ref())
236 }
237
238 /// Retrieves a record from the datastore by its identifier.
239 ///
240 /// # Arguments
241 /// * `request` - The request containing the identifier of the record to be retrieved.
242 /// * `token` - The authorization token to authorize the request.
243 ///
244 /// # Returns
245 /// * `Ok(ResponseData)` - The response data containing the requested record.
246 /// * `Err(Error)` - If the operation fails or if an error occurs during the process.
247 #[allow(clippy::missing_errors_doc)]
248 pub async fn get_by_id(
249 &mut self,
250 request: GetByIdRequest,
251 token: &str,
252 ) -> Result<ResponseData, Error> {
253 let request = authorize_request(request, token)?;
254
255 let response = self
256 .client
257 .get_by_id(request)
258 .await
259 .handle_err(location!())?;
260
261 validate_response_and_convert_to_reponse_data(response.get_ref())
262 }
263}