-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.rs
144 lines (121 loc) · 3.94 KB
/
client.rs
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
use hyper::{Client, Uri, Request, Body};
use hyper_tls::HttpsConnector;
use tokio::time::timeout;
use std::time::Duration;
use tracing::{info, error, debug};
use tracing_subscriber;
use structopt::StructOpt;
use std::fs::File;
use std::io::Write;
use http::Method;
use serde::Deserialize;
use dotenv::dotenv;
use std::env;
#[derive(StructOpt, Debug)]
#[structopt(name = "http2_client")]
struct Opt {
#[structopt(short, long, default_value = "https://example.com")]
url: String,
#[structopt(short, long, default_value = "10")]
timeout: u64,
#[structopt(short, long, default_value = "GET")]
method: String,
#[structopt(long)]
headers: Vec<String>,
#[structopt(long)]
output: Option<String>,
#[structopt(long)]
body: Option<String>,
#[structopt(short, long)]
verbose: bool,
#[structopt(short, long, default_value = "3")]
retries: u8,
#[structopt(long)]
follow_redirects: bool,
}
#[tokio::main]
async fn main() {
dotenv().ok();
tracing_subscriber::fmt::init();
let opt = Opt::from_args();
if opt.verbose {
tracing_subscriber::fmt().with_max_level(tracing::Level::DEBUG).init();
}
let https = HttpsConnector::new();
let client = Client::builder()
.http2_only(true)
.build::<_, hyper::Body>(https);
let uri = match opt.url.parse::<Uri>() {
Ok(uri) => uri,
Err(e) => {
error!("Failed to parse URI: {}", e);
return;
}
};
let method = match opt.method.to_uppercase().as_str() {
"GET" => Method::GET,
"POST" => Method::POST,
"PUT" => Method::PUT,
"DELETE" => Method::DELETE,
"HEAD" => Method::HEAD,
"OPTIONS" => Method::OPTIONS,
"PATCH" => Method::PATCH,
_ => {
error!("Unsupported HTTP method: {}", opt.method);
return;
}
};
let mut request_builder = Request::builder()
.method(method)
.uri(uri)
.header("User-Agent", "hyper-http2-client/0.1");
for header in opt.headers.iter() {
let parts: Vec<&str> = header.splitn(2, ':').collect();
if parts.len() == 2 {
request_builder = request_builder.header(parts[0].trim(), parts[1].trim());
} else {
error!("Invalid header format: {}", header);
return;
}
}
let body = if let Some(body_str) = &opt.body {
Body::from(body_str.clone())
} else {
Body::empty()
};
let request = request_builder
.body(body)
.expect("Request builder");
let timeout_duration = Duration::from_secs(opt.timeout);
for attempt in 1..=opt.retries {
match timeout(timeout_duration, client.request(request.clone())).await {
Ok(Ok(response)) => {
info!("Response Status: {}", response.status());
for (key, value) in response.headers() {
info!("{}: {:?}", key, value);
}
let body = hyper::body::to_bytes(response.into_body()).await;
match body {
Ok(bytes) => {
let body_str = String::from_utf8_lossy(&bytes);
info!("Response Body: {}", body_str);
if let Some(output) = &opt.output {
let mut file = File::create(output).expect("Unable to create file");
file.write_all(&bytes).expect("Unable to write data");
}
}
Err(e) => {
error!("Failed to read response body: {}", e);
}
}
break;
}
Ok(Err(e)) => {
error!("Request failed: {} (Attempt {}/{})", e, attempt, opt.retries);
}
Err(_) => {
error!("Request timed out (Attempt {}/{})", attempt, opt.retries);
}
}
}
}