cyfs_lib/router_handler/http/
request.rs

1use super::super::*;
2use cyfs_base::*;
3
4use serde_json::{Map, Value};
5
6#[derive(Debug)]
7pub struct RouterAddHandlerParam {
8    pub filter: Option<String>,
9    pub req_path: Option<String>,
10    pub index: i32,
11
12    pub default_action: RouterHandlerAction,
13
14    pub routine: Option<String>,
15}
16
17pub struct RouterRemoveHandlerParam {
18    pub id: String,
19}
20
21impl JsonCodec<RouterAddHandlerParam> for RouterAddHandlerParam {
22    fn encode_json(&self) -> Map<String, Value> {
23        let mut obj = Map::new();
24
25        JsonCodecHelper::encode_option_string_field(&mut obj, "filter", self.filter.as_ref());
26        JsonCodecHelper::encode_option_string_field(&mut obj, "req_path", self.req_path.as_ref());
27
28        obj.insert("index".to_string(), Value::String(self.index.to_string()));
29        obj.insert(
30            "default_action".to_string(),
31            Value::Object(self.default_action.encode_json()),
32        );
33
34        if self.routine.is_some() {
35            obj.insert(
36                "routine".to_string(),
37                Value::String(self.routine.as_ref().unwrap().clone()),
38            );
39        }
40
41        obj
42    }
43
44    fn decode_json(req_obj: &Map<String, Value>) -> BuckyResult<Self> {
45        let mut filter: Option<String> = None;
46        let mut req_path: Option<String> = None;
47        let mut default_action: Option<RouterHandlerAction> = None;
48        let mut routine: Option<String> = None;
49        let mut index: Option<i32> = None;
50
51        for (k, v) in req_obj {
52            match k.as_str() {
53                "filter" => {
54                    filter = Some(JsonCodecHelper::decode_from_string(&v)?);
55                }
56                "req_path" => {
57                    req_path = Some(JsonCodecHelper::decode_from_string(&v)?);
58                }
59
60                "index" => {
61                    index = Some(JsonCodecHelper::decode_to_int(v)?);
62                }
63                "default_action" => {
64                    /*
65                    支持两种模式
66                    default_action: 'xxxx',
67                    或者
68                    default_action: {
69                        "action": "xxxx",
70                    }
71                    */
72                    if v.is_string() {
73                        default_action = Some(JsonCodecHelper::decode_from_string(v)?);
74                    } else if v.is_object() {
75                        default_action =
76                            Some(RouterHandlerAction::decode_json(v.as_object().unwrap())?);
77                    } else {
78                        let msg = format!("invalid default_action field format: {:?}", v);
79                        warn!("{}", msg);
80                        return Err(BuckyError::new(BuckyErrorCode::InvalidParam, msg));
81                    }
82                }
83
84                "routine" => {
85                    if !v.is_string() {
86                        let msg = format!("invalid routine field: {:?}", v);
87                        warn!("{}", msg);
88
89                        return Err(BuckyError::new(BuckyErrorCode::InvalidParam, msg));
90                    }
91
92                    routine = Some(v.as_str().unwrap().to_owned());
93                }
94
95                u @ _ => {
96                    warn!("unknown router handler field: {}", u);
97                }
98            }
99        }
100
101        if index.is_none() || default_action.is_none() {
102            let msg = format!("router handler request field missing: index/default_action");
103            warn!("{}", msg);
104
105            return Err(BuckyError::new(BuckyErrorCode::InvalidParam, msg));
106        }
107
108        let req = Self {
109            filter,
110            req_path,
111            index: index.unwrap(),
112            default_action: default_action.unwrap(),
113            routine,
114        };
115
116        Ok(req)
117    }
118}
119
120impl JsonCodec<RouterRemoveHandlerParam> for RouterRemoveHandlerParam {
121    fn encode_json(&self) -> Map<String, Value> {
122        let mut obj = Map::new();
123
124        obj.insert("id".to_string(), Value::String(self.id.clone()));
125
126        obj
127    }
128
129    fn decode_json(req_obj: &Map<String, Value>) -> BuckyResult<Self> {
130        let mut id: Option<String> = None;
131
132        for (k, v) in req_obj {
133            match k.as_str() {
134                "id" => {
135                    if !v.is_string() {
136                        let msg = format!("invalid handler id field: {:?}", v);
137                        warn!("{}", msg);
138
139                        return Err(BuckyError::new(BuckyErrorCode::InvalidParam, msg));
140                    }
141
142                    id = Some(v.as_str().unwrap().to_owned())
143                }
144
145                u @ _ => {
146                    warn!("unknown router handler field: {}", u);
147                }
148            }
149        }
150
151        if id.is_none() {
152            let msg = format!("routine handler request missing: id");
153            warn!("{}", msg);
154
155            return Err(BuckyError::new(BuckyErrorCode::InvalidParam, msg));
156        }
157
158        let req = Self { id: id.unwrap() };
159
160        Ok(req)
161    }
162}
163
164/*
165#[derive(Debug)]
166pub struct RouterHandlerResponse {
167    pub err: u32,
168    pub msg: Option<String>,
169}
170
171impl JsonCodec<RouterHandlerResponse> for RouterHandlerResponse {
172    fn encode_json(&self) -> Map<String, Value> {
173        let mut obj = Map::new();
174
175        obj.insert("err".to_owned(), Value::String(self.err.to_string()));
176        if self.msg.is_some() {
177            obj.insert(
178                "msg".to_owned(),
179                Value::String(self.msg.as_ref().unwrap().clone()),
180            );
181        }
182
183        obj
184    }
185
186    fn decode_json(obj: &Map<String, Value>) -> BuckyResult<Self> {
187        let mut err: Option<u32> = None;
188        let mut msg: Option<String> = None;
189
190        for (k, v) in obj {
191            match k.as_str() {
192                "err" => {
193                    let v = v.as_str().unwrap_or("").parse::<u32>().map_err(|e| {
194                        error!("parse err field error: {} {:?}", e, obj);
195
196                        BuckyError::from(BuckyErrorCode::InvalidFormat)
197                    })?;
198
199                    err = Some(v);
200                }
201
202                "msg" => {
203                    let v = v.as_str().unwrap_or("");
204                    msg = Some(v.to_owned());
205                }
206
207                u @ _ => {
208                    error!("unknown handler register response field: {}", u);
209                }
210            }
211        }
212
213        if err.is_none() {
214            error!("err field missing! {:?}", obj);
215            return Err(BuckyError::from(BuckyErrorCode::InvalidFormat));
216        }
217
218        Ok(Self {
219            err: err.unwrap(),
220            msg,
221        })
222    }
223}
224*/