docs(examples): update client example to use rt::main
This commit is contained in:
@@ -7,9 +7,12 @@ use std::env;
|
|||||||
use std::io::{self, Write};
|
use std::io::{self, Write};
|
||||||
|
|
||||||
use hyper::Client;
|
use hyper::Client;
|
||||||
use hyper::rt;
|
|
||||||
|
|
||||||
fn main() {
|
// A simple type alias so as to DRY.
|
||||||
|
type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
|
||||||
|
|
||||||
|
#[hyper::rt::main]
|
||||||
|
async fn main() -> Result<()> {
|
||||||
pretty_env_logger::init();
|
pretty_env_logger::init();
|
||||||
|
|
||||||
// Some simple CLI args requirements...
|
// Some simple CLI args requirements...
|
||||||
@@ -17,7 +20,7 @@ fn main() {
|
|||||||
Some(url) => url,
|
Some(url) => url,
|
||||||
None => {
|
None => {
|
||||||
println!("Usage: client <url>");
|
println!("Usage: client <url>");
|
||||||
return;
|
return Ok(());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -26,26 +29,16 @@ fn main() {
|
|||||||
let url = url.parse::<hyper::Uri>().unwrap();
|
let url = url.parse::<hyper::Uri>().unwrap();
|
||||||
if url.scheme_part().map(|s| s.as_ref()) != Some("http") {
|
if url.scheme_part().map(|s| s.as_ref()) != Some("http") {
|
||||||
println!("This example only works with 'http' URLs.");
|
println!("This example only works with 'http' URLs.");
|
||||||
return;
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run the runtime with the future trying to fetch and print this URL.
|
fetch_url(url).await
|
||||||
//
|
|
||||||
// Note that in more complicated use cases, the runtime should probably
|
|
||||||
// run on its own, and futures should just be spawned into it.
|
|
||||||
rt::run(fetch_url(url));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn fetch_url(url: hyper::Uri) {
|
async fn fetch_url(url: hyper::Uri) -> Result<()> {
|
||||||
let client = Client::new();
|
let client = Client::new();
|
||||||
|
|
||||||
let res = match client.get(url).await {
|
let res = client.get(url).await?;
|
||||||
Ok(res) => res,
|
|
||||||
Err(err) => {
|
|
||||||
eprintln!("Response Error: {}", err);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
println!("Response: {}", res.status());
|
println!("Response: {}", res.status());
|
||||||
println!("Headers: {:#?}\n", res.headers());
|
println!("Headers: {:#?}\n", res.headers());
|
||||||
@@ -53,17 +46,11 @@ async fn fetch_url(url: hyper::Uri) {
|
|||||||
let mut body = res.into_body();
|
let mut body = res.into_body();
|
||||||
|
|
||||||
while let Some(next) = body.next().await {
|
while let Some(next) = body.next().await {
|
||||||
match next {
|
let chunk = next?;
|
||||||
Ok(chunk) => {
|
io::stdout().write_all(&chunk)?;
|
||||||
io::stdout().write_all(&chunk)
|
|
||||||
.expect("example expects stdout is open");
|
|
||||||
},
|
|
||||||
Err(err) => {
|
|
||||||
eprintln!("Body Error: {}", err);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("\n\nDone!");
|
println!("\n\nDone!");
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user