Skip to content

Simplify client doctest with default Runtime::block_on #1565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 25 additions & 35 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! HTTP Client
//!
//! There are two levels of APIs provided for construct HTTP clients:
//! There are two levels of APIs provided to construct HTTP clients:
//!
//! - The higher-level [`Client`](Client) type.
//! - The lower-level [conn](conn) module.
Expand All @@ -25,51 +25,41 @@
//!
//! ```no_run
//! extern crate hyper;
//! # #[cfg(feature = "runtime")]
//! extern crate tokio;
//!
//! use std::io::{stdout, Write};
//! use hyper::Client;
//! # #[cfg(feature = "runtime")]
//! use hyper::rt::{self, lazy, Future, Stream};
//! use hyper::rt::{Future, Stream};
//! # #[cfg(feature = "runtime")]
//! use tokio::runtime::Runtime;
//!
//! # #[cfg(feature = "runtime")]
//! fn main() {
//! // A runtime is needed to execute our asynchronous code.
//! rt::run(lazy(|| {
//! let client = Client::new();
//!
//! client
//! // Make a GET /ip to 'http://httpbin.org'
//! .get("http://httpbin.org/ip".parse().unwrap())
//!
//! // And then, if the request gets a response...
//! .and_then(|res| {
//! println!("status: {}", res.status());
//!
//! // Concatenate the body stream into a single buffer...
//! // This returns a new future, since we must stream body.
//! res.into_body().concat2()
//! })
//!
//! // And then, if reading the full body succeeds...
//! .and_then(|body| {
//! // The body is just bytes, but let's print a string...
//! let s = ::std::str::from_utf8(&body)
//! .expect("httpbin sends utf-8 JSON");
//! let mut rt = Runtime::new().unwrap();
//! let client = Client::new();
//! let job = client
//! // Make a GET /ip to 'http://httpbin.org'
//! .get("http://httpbin.org/ip".parse().unwrap())
//!
//! println!("body: {}", s);
//! // And then, if the request gets a response...
//! .and_then(|res| {
//! println!("status: {}", res.status());
//!
//! // and_then requires we return a new Future, and it turns
//! // out that Result is a Future that is ready immediately.
//! Ok(())
//! })
//! // Concatenate the body stream into a single buffer,
//! // returning a new future.
//! res.into_body().concat2()
//! });
//!
//! // Map any errors that might have happened...
//! .map_err(|err| {
//! println!("error: {}", err);
//! })
//! }));
//! // Run the job, blocking until it is complete
//! let body = rt.block_on(job).unwrap();
//! // Write body to stdout
//! stdout().write_all(&body).unwrap();
//! }
//! #
//! # #[cfg(not(feature = "runtime"))]
//! # fn main () {}
//! # fn main() {}
//! ```

use std::fmt;
Expand Down