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;
#[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()
}
}
#[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()
}
}
#[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()
}
}
#[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()
}
}
#[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()
}
}
#[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()
}
}
#[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()
}
}
#[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(),
}
}
}
#[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()
}
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConfigContext {
pub group: String,
pub data_id: String,
pub tenant: String,
}
#[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()
}
}
#[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()
}
}
#[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
}
}
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,
}),
}
}
}
#[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,
}
}
pub fn with_data_id(mut self, data_id: &str) -> Self {
self.config_request.data_id = data_id.to_string();
self
}
pub fn with_group(mut self, group: &str) -> Self {
self.config_request.group = group.to_string();
self
}
pub fn with_page(mut self, page_no: i32, page_size: i32) -> Self {
self.page_no = page_no;
self.page_size = page_size;
self
}
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()
}
}
#[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,
}
#[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()
}
}
#[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();
}
}