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
use std::collections::HashMap;

use serde::{Deserialize, Serialize};
use serde_json::Value;

#[derive(Serialize, Deserialize)]
pub enum Message {
    Request(Request),
    Response(Response),
}

#[derive(Serialize, Deserialize)]
pub struct Response {
    url: Option<String>,
    method: Option<String>,
    status: u32,
    headers: HashMap<String, Value>,
    body: Value,
}

impl Response {
    pub fn builder() -> ResponseBuilder {
        ResponseBuilder {
            url: Some("".into()),
            status: 200,
            method: Some("GET".into()),
            headers: HashMap::new(),
            body: Value::Null,
        }
    }
    pub fn url(&self) -> Option<String> {
        self.url.clone()
    }
    pub fn method(&self) -> Option<String> {
        self.url.clone()
    }
    pub fn headers(&self) -> HashMap<String, Value> {
        self.headers.clone()
    }
    pub fn body(&self) -> Value {
        self.body.clone()
    }
    pub fn status(&self) -> u32 {
        self.status
    }

    pub fn id(&self) -> Option<&str> {
        // if let Some(id) = self.headers.get("id") {
        //     id.as_str()
        // } else { None }
        self.headers.get("id").and_then(|id| id.as_str())
    }
}

pub struct ResponseBuilder {
    url: Option<String>,
    method: Option<String>,
    status: u32,
    headers: HashMap<String, Value>,
    body: Value,
}

impl ResponseBuilder {
    pub fn url<S: Into<String>>(&mut self, url: S) -> &mut Self {
        self.url = Some(url.into());
        self
    }
    pub fn status(&mut self, status: u32) -> &mut Self {
        self.status = status;
        self
    }
    pub fn method<S: Into<String>>(&mut self, method: S) -> &mut Self {
        self.method = Some(method.into());
        self
    }
    pub fn headers(&mut self, headers: HashMap<String, Value>) -> &mut Self {
        self.headers = headers;
        self
    }
    pub fn body(&mut self, body: Value) -> &mut Self {
        self.body = body;
        self
    }
    pub fn build(&self) -> Response {
        Response {
            url: self.url.clone(),
            status: self.status.clone(),
            method: self.method.clone(),
            headers: self.headers.clone(),
            body: self.body.clone(),
        }
    }
}

#[derive(Serialize, Deserialize)]
pub struct Request {
    url: String,
    method: Option<String>,
    headers: HashMap<String, Value>,
    body: Value,
}

impl Request {
    pub fn builder() -> RequestBuilder {
        RequestBuilder {
            url: "".into(),
            method: "GET".into(),
            headers: HashMap::new(),
            body: Value::Null,
        }
    }
    pub fn id(&self) -> Option<&str> {
        // if let Some(id) = self.headers.get("id") {
        //     id.as_str()
        // } else {
        //     None
        // }
        self.headers.get("id").and_then(|id| id.as_str())
    }
    pub fn url(&self) -> &str {
        &self.url
    }
    pub fn method(&self) -> &Option<String> {
        &self.method
    }
    pub fn headers(&self) -> HashMap<String, Value> {
        self.headers.clone()
    }
    pub fn body(&self) -> Value {
        self.body.clone()
    }
}


pub struct RequestBuilder {
    url: String,
    method: String,
    headers: HashMap<String, Value>,
    body: Value,
}

impl RequestBuilder {
    pub fn url<S: Into<String>>(&mut self, url: S) -> &mut Self {
        self.url = url.into();
        self
    }
    pub fn method<S: Into<String>>(&mut self, method: S) -> &mut Self {
        self.method = method.into();
        self
    }
    pub fn headers(&mut self, headers: HashMap<String, Value>) -> &mut Self {
        self.headers = headers;
        self
    }
    pub fn cookies(&mut self, cookies: HashMap<String, String>) -> &mut Self {
        let mut cookieList = Vec::new();
        for (key, value) in cookies {
            cookieList.push(format!("{}={}", key, value));
        }
        let cookieString = cookieList.iter().map(|cookie| String::from(cookie) + "; ").collect();
        self.headers.insert("Cookie".into(), Value::String(cookieString));
        self
    }


    pub fn body(&mut self, body: Value) -> &mut Self {
        self.body = body;
        self
    }
    pub fn id<S: Into<String>>(&mut self, id: S) -> &mut Self {
        self.headers.insert("id".into(), Value::String(id.into()));
        self
    }


    pub fn build(&self) -> Request {
        Request {
            url: self.url.clone(),
            method: Option::from(self.method.clone()),
            headers: self.headers.clone(),
            body: self.body.clone(),
        }
    }
}

// Message {
// url: Some("/corp/person/getById".into()),
// method: Some("get".into()),
// status: None,
// headers: HashMap::new(),
// body: Some(Value::Array(vec! {Value::String("0002f6eca3fc42c6812d23dd73cbed4f".into())})),
// }

#[test]
fn test() {
    let mut cookies = HashMap::new();
    let mut cookieList = Vec::new();
    cookies.insert("id", "asdfsa");
    cookies.insert("name", "zxj");
    for (key, value) in cookies {
        cookieList.push(format!("{}={}", key, value));
    }
    let cookieString: String = cookieList.iter().map(|cookie| String::from(cookie) + "; ").collect();
    println!("{}", cookieString)
}