doc(client): show spawning a client future, clarify lazy in client example

This commit is contained in:
Sean McArthur
2018-06-18 17:02:57 -07:00
parent 482a5f589e
commit 61f31b5a4a
2 changed files with 72 additions and 61 deletions

View File

@@ -28,32 +28,38 @@ fn main() {
return;
}
rt::run(rt::lazy(move || {
let client = Client::new();
client
// Fetch the url...
.get(url)
// And then, if we get a response back...
.and_then(|res| {
println!("Response: {}", res.status());
println!("Headers: {:#?}", res.headers());
// The body is a stream, and for_each returns a new Future
// when the stream is finished, and calls the closure on
// each chunk of the body...
res.into_body().for_each(|chunk| {
io::stdout().write_all(&chunk)
.map_err(|e| panic!("example expects stdout is open, error={}", e))
})
})
// If all good, just tell the user...
.map(|_| {
println!("\n\nDone.");
})
// If there was an error, let the user know...
.map_err(|err| {
eprintln!("Error {}", err);
})
}));
// Run the runtime with the future trying to fetch and print this URL.
//
// 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));
}
fn fetch_url(url: hyper::Uri) -> impl Future<Item=(), Error=()> {
let client = Client::new();
client
// Fetch the url...
.get(url)
// And then, if we get a response back...
.and_then(|res| {
println!("Response: {}", res.status());
println!("Headers: {:#?}", res.headers());
// The body is a stream, and for_each returns a new Future
// when the stream is finished, and calls the closure on
// each chunk of the body...
res.into_body().for_each(|chunk| {
io::stdout().write_all(&chunk)
.map_err(|e| panic!("example expects stdout is open, error={}", e))
})
})
// If all good, just tell the user...
.map(|_| {
println!("\n\nDone.");
})
// If there was an error, let the user know...
.map_err(|err| {
eprintln!("Error {}", err);
})
}