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
use crate::cookie::Cookie;
use crate::Result;
use http::HeaderMap;
use reqwest::{Method, StatusCode};
use serde::de::DeserializeOwned;
use serde_json::{to_string_pretty, Value};

pub struct Response {
	request_method: Method,
	request_url: String,

	status: StatusCode,
	header_map: HeaderMap,

	client_cookies: Vec<Cookie>,

	/// Cookies from the response
	cookies: Vec<Cookie>,
	body: Body,
}

enum Body {
	Json(Value),
	Text(String),
	Other,
}

impl Response {
	pub(crate) async fn from_reqwest_response(
		request_method: Method,
		request_url: String,
		client_cookies: Vec<Cookie>,
		mut res: reqwest::Response,
	) -> Result<Response> {
		let status = res.status();

		// Cookies from response
		let cookies: Vec<Cookie> = res.cookies().map(Cookie::from).collect();

		// Move the headers into a new HeaderMap
		let headers = res.headers_mut().drain().filter_map(|(n, v)| n.map(|n| (n, v)));
		let header_map = HeaderMap::from_iter(headers);

		// Capture the body
		let ct = header_map.get("content-type").and_then(|v| v.to_str().ok());
		let body = if let Some(ct) = ct {
			if ct == "application/json" {
				Body::Json(res.json::<Value>().await?)
			} else if ct.starts_with("text/") {
				Body::Text(res.text().await?)
			} else {
				Body::Other
			}
		} else {
			Body::Other
		};

		Ok(Response {
			client_cookies,
			request_method,
			request_url,
			status,
			header_map,
			cookies,
			body,
		})
	}
}

impl Response {
	// region:    --- Print Methods
	pub async fn print(&self) -> Result<()> {
		self.inner_print(true).await
	}

	pub async fn print_no_body(&self) -> Result<()> {
		self.inner_print(false).await
	}

	/// NOTE: For now, does not need to be async, but keeping the option of using async for later.
	async fn inner_print(&self, body: bool) -> Result<()> {
		println!();
		println!("=== Response for {} {}", self.request_method, self.request_url);
		println!("=> {:<15}: {}", "Status", self.status);
		// Print the response headers.
		println!("=> {:<15}:", "Headers");
		for (n, v) in self.header_map.iter() {
			println!("   {n}: {v:?}");
		}

		// Print the cookie_store
		if !self.cookies.is_empty() {
			println!("=> {:<15}:", "Response Cookies");
			for c in self.cookies.iter() {
				println!("   {}: {}", c.name, c.value);
			}
		}

		// Print the cookie_store
		if !self.client_cookies.is_empty() {
			println!("=> {:<15}:", "Client Cookies");
			for c in self.client_cookies.iter() {
				println!("   {}: {}", c.name, c.value);
			}
		}

		if body {
			// Print the body (json pretty print if json type)
			println!("=> {:<15}:", "Response Body");
			match &self.body {
				Body::Json(val) => println!("{}", to_string_pretty(val)?),
				Body::Text(val) => println!("{val}"),
				_ => (),
			}
		}

		println!("===\n");
		Ok(())
	}
	// endregion: --- Print Methods

	// region:    --- Headers
	pub fn header_all(&self, name: &str) -> Vec<String> {
		self.header_map
			.get_all(name)
			.iter()
			.filter_map(|v| v.to_str().map(|v| v.to_string()).ok())
			.collect()
	}

	pub fn header(&self, name: &str) -> Option<String> {
		self.header_map.get(name).and_then(|v| v.to_str().map(|v| v.to_string()).ok())
	}
	// endregion: --- Headers

	// region:    --- Response Cookie
	/// Return the cookie that has been set for this http response.
	pub fn res_cookie(&self, name: &str) -> Option<&Cookie> {
		self.cookies.iter().find(|c| c.name == name)
	}

	/// Return the cookie value that has been set for this http response.
	pub fn res_cookie_value(&self, name: &str) -> Option<String> {
		self.cookies.iter().find(|c| c.name == name).map(|c| c.value.clone())
	}
	// endregion: --- Response Cookie

	// region:    --- Client Cookies
	/// Return the client httpc-test Cookie for a given name.
	/// Note: The response.client_cookies are the captured client cookies
	///       at the time of the response.
	pub fn client_cookie(&self, name: &str) -> Option<&Cookie> {
		self.client_cookies.iter().find(|c| c.name == name)
	}

	/// Return the client cookie value as String for a given name.
	/// Note: The response.client_cookies are the captured client cookies
	///       at the time of the response.
	pub fn client_cookie_value(&self, name: &str) -> Option<String> {
		self.client_cookies.iter().find(|c| c.name == name).map(|c| c.value.clone())
	}
	// endregion: --- Client Cookies

	// region:    --- Body
	pub fn json_body(&self) -> Result<Value> {
		match &self.body {
			Body::Json(val) => Ok(val.clone()),
			_ => Err(crate::Error::Static("No json body")),
		}
	}

	pub fn text_body(&self) -> Result<String> {
		match &self.body {
			Body::Text(val) => Ok(val.clone()),
			_ => Err(crate::Error::Static("No text body")),
		}
	}

	pub fn json_body_as<T>(&self) -> Result<T>
	where
		T: DeserializeOwned,
	{
		self.json_body()
			.and_then(|val| serde_json::from_value::<T>(val).map_err(|e| crate::Error::Generic(e.to_string())))
	}
	// endregion: --- Body
}