Skip to content

Commit b103ac2

Browse files
committed
dev: added more colors for response on http
1 parent 9b13bf2 commit b103ac2

1 file changed

Lines changed: 50 additions & 33 deletions

File tree

src/modules/http/handler.rs

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,64 @@ use reqwest::Method;
44
use serde_json::json;
55
use std::collections::HashMap;
66

7+
const GREEN: &str = "\x1b[32m";
8+
const RED: &str = "\x1b[31m";
9+
const WHITE: &str = "\x1b[37m";
10+
const RESET: &str = "\x1b[0m";
11+
712
pub async fn handle(action: HttpAction) -> Result<(), Box<dyn std::error::Error>> {
8-
match action {
9-
HttpAction::Get { url, headers } => {
10-
let headers = parse_headers(headers);
11-
let response = client::execute_request(Method::GET, &url, None, headers).await?;
12-
client::print_response(response).await?;
13-
}
13+
let (method, url, body, headers) = match action {
14+
HttpAction::Get { url, headers } => (Method::GET, url, None, headers),
1415
HttpAction::Post { url, body, headers } => {
15-
let body = body.map(|b| json!(b));
16-
let headers = parse_headers(headers);
17-
let response = client::execute_request(Method::POST, &url, body, headers).await?;
18-
client::print_response(response).await?;
16+
(Method::POST, url, body.map(|b| json!(b)), headers)
1917
}
2018
HttpAction::Put { url, body, headers } => {
21-
let body = body.map(|b| json!(b));
22-
let headers = parse_headers(headers);
23-
let response = client::execute_request(Method::PUT, &url, body, headers).await?;
24-
client::print_response(response).await?;
19+
(Method::PUT, url, body.map(|b| json!(b)), headers)
2520
}
2621
HttpAction::Patch { url, body, headers } => {
27-
let body = body.map(|b| json!(b));
28-
let headers = parse_headers(headers);
29-
let response = client::execute_request(Method::PATCH, &url, body, headers).await?;
30-
client::print_response(response).await?;
31-
}
32-
HttpAction::Delete { url, headers } => {
33-
let headers = parse_headers(headers);
34-
let response = client::execute_request(Method::DELETE, &url, None, headers).await?;
35-
client::print_response(response).await?;
36-
}
37-
HttpAction::Head { url, headers } => {
38-
let headers = parse_headers(headers);
39-
let response = client::execute_request(Method::HEAD, &url, None, headers).await?;
40-
client::print_response(response).await?;
41-
}
42-
HttpAction::Options { url, headers } => {
43-
let headers = parse_headers(headers);
44-
let response = client::execute_request(Method::OPTIONS, &url, None, headers).await?;
45-
client::print_response(response).await?;
22+
(Method::PATCH, url, body.map(|b| json!(b)), headers)
4623
}
24+
HttpAction::Delete { url, headers } => (Method::DELETE, url, None, headers),
25+
HttpAction::Head { url, headers } => (Method::HEAD, url, None, headers),
26+
HttpAction::Options { url, headers } => (Method::OPTIONS, url, None, headers),
27+
};
28+
29+
let headers_map = parse_headers(headers);
30+
let response = client::execute_request(method, &url, body, headers_map.clone()).await?;
31+
32+
let status_color = if response.status().is_success() {
33+
GREEN
34+
} else {
35+
RED
36+
};
37+
println!("{}Status: {}{}", status_color, response.status(), RESET);
38+
39+
println!("{}Headers:{}\n", RED, RESET);
40+
for (k, v) in response.headers() {
41+
println!("{}{}: {}{}", RED, k, v.to_str().unwrap_or(""), RESET);
4742
}
43+
44+
let content_type = response
45+
.headers()
46+
.get("content-type")
47+
.and_then(|v| v.to_str().ok())
48+
.unwrap_or("");
49+
50+
let body_color = if content_type.contains("text/html")
51+
|| content_type.contains("application/json")
52+
|| content_type.contains("text/plain")
53+
{
54+
WHITE
55+
} else {
56+
RED
57+
};
58+
59+
let body_text = response.text().await?;
60+
println!(
61+
"\n{}Body:{}\n{}{}{}",
62+
RED, RESET, body_color, body_text, RESET
63+
);
64+
4865
Ok(())
4966
}
5067

0 commit comments

Comments
 (0)