MCPcopy Create free account
hub / github.com/bytecodealliance/wstd / main

Function main

examples/complex_http_client.rs:47–136  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

45
46#[wstd::main]
47async fn main() -> Result<()> {
48 let args = Args::parse();
49
50 // Create and configure the `Client`
51
52 let mut client = Client::new();
53
54 if let Some(connect_timeout) = args.connect_timeout {
55 client.set_connect_timeout(*connect_timeout);
56 }
57 if let Some(first_byte_timeout) = args.first_byte_timeout {
58 client.set_first_byte_timeout(*first_byte_timeout);
59 }
60 if let Some(between_bytes_timeout) = args.between_bytes_timeout {
61 client.set_between_bytes_timeout(*between_bytes_timeout);
62 }
63
64 // Create and configure the request.
65
66 let mut request = Request::builder();
67
68 request = request.uri(args.url).method(args.method);
69
70 for header in args.headers {
71 let mut parts = header.splitn(2, ": ");
72 let key = parts.next().unwrap();
73 let value = parts
74 .next()
75 .ok_or_else(|| anyhow!("headers must be formatted like \"key: value\""))?;
76 request = request.header(key, value);
77 }
78 let mut trailers = HeaderMap::new();
79 for trailer in args.trailers {
80 let mut parts = trailer.splitn(2, ": ");
81 let key = parts.next().unwrap();
82 let value = parts
83 .next()
84 .ok_or_else(|| anyhow!("trailers must be formatted like \"key: value\""))?;
85 trailers.insert(HeaderName::from_str(key)?, HeaderValue::from_str(value)?);
86 }
87
88 let body = if args.body {
89 Body::from_try_stream(wstd::io::stdin().into_inner().into_stream()).into_boxed_body()
90 } else {
91 Body::empty().into_boxed_body()
92 };
93 let t = trailers.clone();
94 let body = body.with_trailers(async move { if t.is_empty() { None } else { Some(Ok(t)) } });
95 let request = request.body(Body::from_http_body(body))?;
96
97 // Send the request.
98 eprintln!("> {} / {:?}", request.method(), request.version());
99 for (key, value) in request.headers().iter() {
100 let value = String::from_utf8_lossy(value.as_bytes());
101 eprintln!("> {key}: {value}");
102 }
103
104 let response = client.send(request).await?;

Callers

nothing calls this directly

Calls 12

stdinFunction · 0.85
emptyFunction · 0.85
stdoutFunction · 0.85
set_connect_timeoutMethod · 0.80
into_boxed_bodyMethod · 0.80
into_streamMethod · 0.80
nextMethod · 0.45
into_innerMethod · 0.45
sendMethod · 0.45
write_allMethod · 0.45

Tested by

no test coverage detected