|
27 | 27 | //! [full client example](https://github.com/hyperium/hyper/blob/master/examples/client.rs).
|
28 | 28 | //!
|
29 | 29 | //! ```
|
30 |
| -//! extern crate hyper; |
31 |
| -//! |
| 30 | +//! # #![feature(async_await)] |
32 | 31 | //! use hyper::{Client, Uri};
|
33 |
| -//! # #[cfg(feature = "runtime")] |
34 |
| -//! use hyper::rt::{self, Future, Stream}; |
35 | 32 | //!
|
36 | 33 | //! # #[cfg(feature = "runtime")]
|
37 |
| -//! # fn fetch_httpbin() { |
| 34 | +//! # async fn fetch_httpbin() -> hyper::Result<()> { |
38 | 35 | //! let client = Client::new();
|
39 | 36 | //!
|
40 |
| -//! let fut = client |
41 |
| -//! |
42 |
| -//! // Make a GET /ip to 'http://httpbin.org' |
43 |
| -//! .get(Uri::from_static("http://httpbin.org/ip")) |
44 |
| -//! |
45 |
| -//! // And then, if the request gets a response... |
46 |
| -//! .and_then(|res| { |
47 |
| -//! println!("status: {}", res.status()); |
48 |
| -//! |
49 |
| -//! // Concatenate the body stream into a single buffer... |
50 |
| -//! // This returns a new future, since we must stream body. |
51 |
| -//! res.into_body().concat2() |
52 |
| -//! }) |
53 |
| -//! |
54 |
| -//! // And then, if reading the full body succeeds... |
55 |
| -//! .and_then(|body| { |
56 |
| -//! // The body is just bytes, but let's print a string... |
57 |
| -//! let s = ::std::str::from_utf8(&body) |
58 |
| -//! .expect("httpbin sends utf-8 JSON"); |
| 37 | +//! // Make a GET /ip to 'http://httpbin.org' |
| 38 | +//! let res = client.get(Uri::from_static("http://httpbin.org/ip")).await?; |
59 | 39 | //!
|
60 |
| -//! println!("body: {}", s); |
| 40 | +//! // And then, if the request gets a response... |
| 41 | +//! println!("status: {}", res.status()); |
61 | 42 | //!
|
62 |
| -//! // and_then requires we return a new Future, and it turns |
63 |
| -//! // out that Result is a Future that is ready immediately. |
64 |
| -//! Ok(()) |
65 |
| -//! }) |
| 43 | +//! // Concatenate the body stream into a single buffer... |
| 44 | +//! let mut body = res.into_body(); |
| 45 | +//! let mut bytes = Vec::new(); |
| 46 | +//! while let Some(next) = body.next().await { |
| 47 | +//! let chunk = next?; |
| 48 | +//! bytes.extend(chunk); |
| 49 | +//! } |
66 | 50 | //!
|
67 |
| -//! // Map any errors that might have happened... |
68 |
| -//! .map_err(|err| { |
69 |
| -//! println!("error: {}", err); |
70 |
| -//! }); |
| 51 | +//! // And then, if reading the full body succeeds... |
| 52 | +//! // The body is just bytes, but let's print a string... |
| 53 | +//! let s = std::str::from_utf8(&bytes) |
| 54 | +//! .expect("httpbin sends utf-8 JSON"); |
71 | 55 | //!
|
72 |
| -//! // A runtime is needed to execute our asynchronous code. In order to |
73 |
| -//! // spawn the future into the runtime, it should already have been |
74 |
| -//! // started and running before calling this code. |
75 |
| -//! rt::spawn(fut); |
| 56 | +//! println!("body: {}", s); |
| 57 | +//! # Ok(()) |
76 | 58 | //! # }
|
77 | 59 | //! # fn main () {}
|
78 | 60 | //! ```
|
|
0 commit comments