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