batata-client 0.0.2

Rust client for Batata/Nacos service discovery and configuration management
Documentation
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
use std::collections::HashMap;

use prost_types::Any;
use serde::{Deserialize, Serialize};

use crate::api::remote::{Request, RequestTrait, Response, ResponseTrait};
use crate::api::Payload;
use crate::common::CONFIG_MODULE;

/// Configuration request base
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConfigRequest {
    #[serde(flatten)]
    pub request: Request,
    pub data_id: String,
    pub group: String,
    pub tenant: String,
    pub module: String,
}

impl ConfigRequest {
    pub fn new(data_id: &str, group: &str, tenant: &str) -> Self {
        Self {
            request: Request::new(),
            data_id: data_id.to_string(),
            group: group.to_string(),
            tenant: tenant.to_string(),
            module: CONFIG_MODULE.to_string(),
        }
    }
}

impl RequestTrait for ConfigRequest {
    fn headers(&self) -> HashMap<String, String> {
        self.request.headers()
    }

    fn insert_headers(&mut self, headers: HashMap<String, String>) {
        self.request.insert_headers(headers);
    }

    fn request_id(&self) -> String {
        self.request.request_id.clone()
    }
}

// ==================== Config Query ====================

/// Configuration query request
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConfigQueryRequest {
    #[serde(flatten)]
    pub config_request: ConfigRequest,
    pub tag: String,
}

impl ConfigQueryRequest {
    pub fn new(data_id: &str, group: &str, tenant: &str) -> Self {
        Self {
            config_request: ConfigRequest::new(data_id, group, tenant),
            tag: String::new(),
        }
    }

    pub fn with_tag(mut self, tag: &str) -> Self {
        self.tag = tag.to_string();
        self
    }
}

impl RequestTrait for ConfigQueryRequest {
    fn headers(&self) -> HashMap<String, String> {
        self.config_request.headers()
    }

    fn request_type(&self) -> &'static str {
        "ConfigQueryRequest"
    }

    fn insert_headers(&mut self, headers: HashMap<String, String>) {
        self.config_request.insert_headers(headers);
    }

    fn request_id(&self) -> String {
        self.config_request.request_id()
    }
}

/// Configuration query response
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConfigQueryResponse {
    #[serde(flatten)]
    pub response: Response,
    pub content: String,
    pub encrypted_data_key: String,
    pub content_type: String,
    pub md5: String,
    pub last_modified: i64,
    pub is_beta: bool,
    pub tag: String,
}

impl ConfigQueryResponse {
    pub const CONFIG_NOT_FOUND: i32 = 300;
    pub const CONFIG_QUERY_CONFLICT: i32 = 400;
    pub const NO_RIGHT: i32 = 403;
}

impl ResponseTrait for ConfigQueryResponse {
    fn response_type(&self) -> &'static str {
        "ConfigQueryResponse"
    }

    fn set_request_id(&mut self, request_id: String) {
        self.response.request_id = request_id;
    }

    fn error_code(&self) -> i32 {
        self.response.error_code
    }

    fn result_code(&self) -> i32 {
        self.response.result_code
    }

    fn message(&self) -> String {
        self.response.message.clone()
    }
}

// ==================== Config Publish ====================

/// Configuration publish request
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConfigPublishRequest {
    #[serde(flatten)]
    pub config_request: ConfigRequest,
    pub content: String,
    pub cas_md5: String,
    pub addition_map: HashMap<String, String>,
}

impl ConfigPublishRequest {
    pub fn new(data_id: &str, group: &str, tenant: &str, content: &str) -> Self {
        Self {
            config_request: ConfigRequest::new(data_id, group, tenant),
            content: content.to_string(),
            cas_md5: String::new(),
            addition_map: HashMap::new(),
        }
    }

    pub fn with_cas_md5(mut self, md5: &str) -> Self {
        self.cas_md5 = md5.to_string();
        self
    }

    pub fn with_addition(mut self, key: &str, value: &str) -> Self {
        self.addition_map.insert(key.to_string(), value.to_string());
        self
    }

    pub fn with_app_name(self, app_name: &str) -> Self {
        self.with_addition("appName", app_name)
    }

    pub fn with_type(self, config_type: &str) -> Self {
        self.with_addition("type", config_type)
    }
}

impl RequestTrait for ConfigPublishRequest {
    fn headers(&self) -> HashMap<String, String> {
        self.config_request.headers()
    }

    fn request_type(&self) -> &'static str {
        "ConfigPublishRequest"
    }

    fn insert_headers(&mut self, headers: HashMap<String, String>) {
        self.config_request.insert_headers(headers);
    }

    fn request_id(&self) -> String {
        self.config_request.request_id()
    }
}

/// Configuration publish response
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConfigPublishResponse {
    #[serde(flatten)]
    pub response: Response,
}

impl ResponseTrait for ConfigPublishResponse {
    fn response_type(&self) -> &'static str {
        "ConfigPublishResponse"
    }

    fn set_request_id(&mut self, request_id: String) {
        self.response.request_id = request_id;
    }

    fn error_code(&self) -> i32 {
        self.response.error_code
    }

    fn result_code(&self) -> i32 {
        self.response.result_code
    }

    fn message(&self) -> String {
        self.response.message.clone()
    }
}

// ==================== Config Remove ====================

/// Configuration remove request
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConfigRemoveRequest {
    #[serde(flatten)]
    pub config_request: ConfigRequest,
    pub tag: String,
}

impl ConfigRemoveRequest {
    pub fn new(data_id: &str, group: &str, tenant: &str) -> Self {
        Self {
            config_request: ConfigRequest::new(data_id, group, tenant),
            tag: String::new(),
        }
    }
}

impl RequestTrait for ConfigRemoveRequest {
    fn headers(&self) -> HashMap<String, String> {
        self.config_request.headers()
    }

    fn request_type(&self) -> &'static str {
        "ConfigRemoveRequest"
    }

    fn insert_headers(&mut self, headers: HashMap<String, String>) {
        self.config_request.insert_headers(headers);
    }

    fn request_id(&self) -> String {
        self.config_request.request_id()
    }
}

/// Configuration remove response
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConfigRemoveResponse {
    #[serde(flatten)]
    pub response: Response,
}

impl ResponseTrait for ConfigRemoveResponse {
    fn response_type(&self) -> &'static str {
        "ConfigRemoveResponse"
    }

    fn set_request_id(&mut self, request_id: String) {
        self.response.request_id = request_id;
    }

    fn error_code(&self) -> i32 {
        self.response.error_code
    }

    fn result_code(&self) -> i32 {
        self.response.result_code
    }

    fn message(&self) -> String {
        self.response.message.clone()
    }
}

// ==================== Config Listen ====================

/// Configuration listen context
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConfigListenContext {
    pub group: String,
    pub md5: String,
    pub data_id: String,
    pub tenant: String,
}

impl ConfigListenContext {
    pub fn new(data_id: &str, group: &str, tenant: &str, md5: &str) -> Self {
        Self {
            data_id: data_id.to_string(),
            group: group.to_string(),
            tenant: tenant.to_string(),
            md5: md5.to_string(),
        }
    }
}

/// Configuration batch listen request
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConfigBatchListenRequest {
    #[serde(flatten)]
    pub config_request: ConfigRequest,
    pub listen: bool,
    pub config_listen_contexts: Vec<ConfigListenContext>,
}

impl ConfigBatchListenRequest {
    pub fn new(listen: bool) -> Self {
        Self {
            config_request: ConfigRequest::new("", "", ""),
            listen,
            config_listen_contexts: Vec::new(),
        }
    }

    pub fn add_context(mut self, context: ConfigListenContext) -> Self {
        self.config_listen_contexts.push(context);
        self
    }
}

impl RequestTrait for ConfigBatchListenRequest {
    fn headers(&self) -> HashMap<String, String> {
        self.config_request.headers()
    }

    fn request_type(&self) -> &'static str {
        "ConfigBatchListenRequest"
    }

    fn insert_headers(&mut self, headers: HashMap<String, String>) {
        self.config_request.insert_headers(headers);
    }

    fn request_id(&self) -> String {
        self.config_request.request_id()
    }
}

/// Changed configuration context
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConfigContext {
    pub group: String,
    pub data_id: String,
    pub tenant: String,
}

/// Configuration change batch listen response
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConfigChangeBatchListenResponse {
    #[serde(flatten)]
    pub response: Response,
    pub changed_configs: Vec<ConfigContext>,
}

impl ResponseTrait for ConfigChangeBatchListenResponse {
    fn response_type(&self) -> &'static str {
        "ConfigChangeBatchListenResponse"
    }

    fn set_request_id(&mut self, request_id: String) {
        self.response.request_id = request_id;
    }

    fn error_code(&self) -> i32 {
        self.response.error_code
    }

    fn result_code(&self) -> i32 {
        self.response.result_code
    }

    fn message(&self) -> String {
        self.response.message.clone()
    }
}

// ==================== Config Change Notify ====================

/// Configuration change notify request (from server)
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConfigChangeNotifyRequest {
    #[serde(flatten)]
    pub request: Request,
    pub data_id: String,
    pub group: String,
    pub tenant: String,
    pub module: String,
}

impl RequestTrait for ConfigChangeNotifyRequest {
    fn headers(&self) -> HashMap<String, String> {
        self.request.headers()
    }

    fn request_type(&self) -> &'static str {
        "ConfigChangeNotifyRequest"
    }

    fn insert_headers(&mut self, headers: HashMap<String, String>) {
        self.request.insert_headers(headers);
    }

    fn request_id(&self) -> String {
        self.request.request_id()
    }
}

/// Configuration change notify response
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConfigChangeNotifyResponse {
    #[serde(flatten)]
    pub response: Response,
}

impl ConfigChangeNotifyResponse {
    pub fn new() -> Self {
        Self {
            response: Response::new(),
        }
    }
}

impl ResponseTrait for ConfigChangeNotifyResponse {
    fn response_type(&self) -> &'static str {
        "ConfigChangeNotifyResponse"
    }

    fn set_request_id(&mut self, request_id: String) {
        self.response.request_id = request_id;
    }

    fn error_code(&self) -> i32 {
        self.response.error_code
    }

    fn result_code(&self) -> i32 {
        self.response.result_code
    }
}

/// Helper function to convert response to payload
impl ConfigChangeNotifyResponse {
    pub fn to_payload(&self, request_id: &str) -> Payload {
        let mut headers = HashMap::new();
        headers.insert("requestId".to_string(), request_id.to_string());

        let body = serde_json::to_vec(self).unwrap_or_default();

        Payload {
            metadata: Some(crate::api::Metadata {
                r#type: "ConfigChangeNotifyResponse".to_string(),
                client_ip: String::new(),
                headers,
            }),
            body: Some(Any {
                type_url: String::new(),
                value: body,
            }),
        }
    }
}

// ==================== Config Search ====================

/// Configuration search request
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConfigSearchRequest {
    #[serde(flatten)]
    pub config_request: ConfigRequest,
    pub search: String,
    pub page_no: i32,
    pub page_size: i32,
}

impl ConfigSearchRequest {
    pub fn new(tenant: &str) -> Self {
        Self {
            config_request: ConfigRequest::new("", "", tenant),
            search: "blur".to_string(),
            page_no: 1,
            page_size: 100,
        }
    }

    /// Set data_id pattern for search
    pub fn with_data_id(mut self, data_id: &str) -> Self {
        self.config_request.data_id = data_id.to_string();
        self
    }

    /// Set group pattern for search
    pub fn with_group(mut self, group: &str) -> Self {
        self.config_request.group = group.to_string();
        self
    }

    /// Set pagination
    pub fn with_page(mut self, page_no: i32, page_size: i32) -> Self {
        self.page_no = page_no;
        self.page_size = page_size;
        self
    }

    /// Set search type ("blur" or "accurate")
    pub fn with_search_type(mut self, search: &str) -> Self {
        self.search = search.to_string();
        self
    }
}

impl RequestTrait for ConfigSearchRequest {
    fn headers(&self) -> HashMap<String, String> {
        self.config_request.headers()
    }

    fn request_type(&self) -> &'static str {
        "ConfigSearchRequest"
    }

    fn insert_headers(&mut self, headers: HashMap<String, String>) {
        self.config_request.insert_headers(headers);
    }

    fn request_id(&self) -> String {
        self.config_request.request_id()
    }
}

/// Configuration search item
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConfigSearchItem {
    pub id: i64,
    pub data_id: String,
    pub group: String,
    pub tenant: String,
    pub content: String,
    pub md5: String,
    pub r#type: String,
    pub app_name: String,
    pub encrypted_data_key: String,
}

/// Configuration search response
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConfigSearchResponse {
    #[serde(flatten)]
    pub response: Response,
    pub total_count: i32,
    pub page_number: i32,
    pub pages_available: i32,
    pub page_items: Vec<ConfigSearchItem>,
}

impl ResponseTrait for ConfigSearchResponse {
    fn response_type(&self) -> &'static str {
        "ConfigSearchResponse"
    }

    fn set_request_id(&mut self, request_id: String) {
        self.response.request_id = request_id;
    }

    fn error_code(&self) -> i32 {
        self.response.error_code
    }

    fn result_code(&self) -> i32 {
        self.response.result_code
    }

    fn message(&self) -> String {
        self.response.message.clone()
    }
}

// ==================== Config Info ====================

/// Configuration information for caching
#[derive(Clone, Debug, Default)]
pub struct ConfigInfo {
    pub data_id: String,
    pub group: String,
    pub tenant: String,
    pub content: String,
    pub md5: String,
    pub last_modified: i64,
    pub content_type: String,
}

impl ConfigInfo {
    pub fn new(data_id: &str, group: &str, tenant: &str) -> Self {
        Self {
            data_id: data_id.to_string(),
            group: group.to_string(),
            tenant: tenant.to_string(),
            ..Default::default()
        }
    }

    pub fn key(&self) -> String {
        crate::common::build_config_key(&self.data_id, &self.group, &self.tenant)
    }

    pub fn update_content(&mut self, content: &str) {
        self.content = content.to_string();
        self.md5 = crate::common::md5_hash(content);
        self.last_modified = crate::common::current_time_millis();
    }
}