zbus_lib/message/
mod.rs

1use std::collections::HashMap;
2
3use serde::{Deserialize, Serialize};
4use serde_json::Value;
5
6#[derive(Serialize, Deserialize)]
7pub enum Message {
8    Request(Request),
9    Response(Response),
10}
11
12#[derive(Serialize, Deserialize)]
13pub struct Response {
14    url: Option<String>,
15    method: Option<String>,
16    status: u32,
17    headers: HashMap<String, Value>,
18    body: Value,
19}
20
21impl Response {
22    pub fn builder() -> ResponseBuilder {
23        ResponseBuilder {
24            url: Some("".into()),
25            status: 200,
26            method: Some("GET".into()),
27            headers: HashMap::new(),
28            body: Value::Null,
29        }
30    }
31    pub fn url(&self) -> Option<String> {
32        self.url.clone()
33    }
34    pub fn method(&self) -> Option<String> {
35        self.url.clone()
36    }
37    pub fn headers(&self) -> HashMap<String, Value> {
38        self.headers.clone()
39    }
40    pub fn body(&self) -> Value {
41        self.body.clone()
42    }
43    pub fn status(&self) -> u32 {
44        self.status
45    }
46
47    pub fn id(&self) -> Option<&str> {
48        // if let Some(id) = self.headers.get("id") {
49        //     id.as_str()
50        // } else { None }
51        self.headers.get("id").and_then(|id| id.as_str())
52    }
53}
54
55pub struct ResponseBuilder {
56    url: Option<String>,
57    method: Option<String>,
58    status: u32,
59    headers: HashMap<String, Value>,
60    body: Value,
61}
62
63impl ResponseBuilder {
64    pub fn url<S: Into<String>>(&mut self, url: S) -> &mut Self {
65        self.url = Some(url.into());
66        self
67    }
68    pub fn status(&mut self, status: u32) -> &mut Self {
69        self.status = status;
70        self
71    }
72    pub fn method<S: Into<String>>(&mut self, method: S) -> &mut Self {
73        self.method = Some(method.into());
74        self
75    }
76    pub fn headers(&mut self, headers: HashMap<String, Value>) -> &mut Self {
77        self.headers = headers;
78        self
79    }
80    pub fn body(&mut self, body: Value) -> &mut Self {
81        self.body = body;
82        self
83    }
84    pub fn build(&self) -> Response {
85        Response {
86            url: self.url.clone(),
87            status: self.status.clone(),
88            method: self.method.clone(),
89            headers: self.headers.clone(),
90            body: self.body.clone(),
91        }
92    }
93}
94
95#[derive(Serialize, Deserialize)]
96pub struct Request {
97    url: String,
98    method: Option<String>,
99    headers: HashMap<String, Value>,
100    body: Value,
101}
102
103impl Request {
104    pub fn builder() -> RequestBuilder {
105        RequestBuilder {
106            url: "".into(),
107            method: "GET".into(),
108            headers: HashMap::new(),
109            body: Value::Null,
110        }
111    }
112    pub fn id(&self) -> Option<&str> {
113        // if let Some(id) = self.headers.get("id") {
114        //     id.as_str()
115        // } else {
116        //     None
117        // }
118        self.headers.get("id").and_then(|id| id.as_str())
119    }
120    pub fn url(&self) -> &str {
121        &self.url
122    }
123    pub fn method(&self) -> &Option<String> {
124        &self.method
125    }
126    pub fn headers(&self) -> HashMap<String, Value> {
127        self.headers.clone()
128    }
129    pub fn body(&self) -> Value {
130        self.body.clone()
131    }
132}
133
134
135pub struct RequestBuilder {
136    url: String,
137    method: String,
138    headers: HashMap<String, Value>,
139    body: Value,
140}
141
142impl RequestBuilder {
143    pub fn url<S: Into<String>>(&mut self, url: S) -> &mut Self {
144        self.url = url.into();
145        self
146    }
147    pub fn method<S: Into<String>>(&mut self, method: S) -> &mut Self {
148        self.method = method.into();
149        self
150    }
151    pub fn headers(&mut self, headers: HashMap<String, Value>) -> &mut Self {
152        self.headers = headers;
153        self
154    }
155    pub fn cookies(&mut self, cookies: HashMap<String, String>) -> &mut Self {
156        let mut cookieList = Vec::new();
157        for (key, value) in cookies {
158            cookieList.push(format!("{}={}", key, value));
159        }
160        let cookieString = cookieList.iter().map(|cookie| String::from(cookie) + "; ").collect();
161        self.headers.insert("Cookie".into(), Value::String(cookieString));
162        self
163    }
164
165
166    pub fn body(&mut self, body: Value) -> &mut Self {
167        self.body = body;
168        self
169    }
170    pub fn id<S: Into<String>>(&mut self, id: S) -> &mut Self {
171        self.headers.insert("id".into(), Value::String(id.into()));
172        self
173    }
174
175
176    pub fn build(&self) -> Request {
177        Request {
178            url: self.url.clone(),
179            method: Option::from(self.method.clone()),
180            headers: self.headers.clone(),
181            body: self.body.clone(),
182        }
183    }
184}
185
186// Message {
187// url: Some("/corp/person/getById".into()),
188// method: Some("get".into()),
189// status: None,
190// headers: HashMap::new(),
191// body: Some(Value::Array(vec! {Value::String("0002f6eca3fc42c6812d23dd73cbed4f".into())})),
192// }
193
194#[test]
195fn test() {
196    let mut cookies = HashMap::new();
197    let mut cookieList = Vec::new();
198    cookies.insert("id", "asdfsa");
199    cookies.insert("name", "zxj");
200    for (key, value) in cookies {
201        cookieList.push(format!("{}={}", key, value));
202    }
203    let cookieString: String = cookieList.iter().map(|cookie| String::from(cookie) + "; ").collect();
204    println!("{}", cookieString)
205}