Created
November 2, 2018 14:37
-
-
Save pimeys/b1bb7bd29daf842d920254fcc93ecae8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(await_macro, async_await, futures_api)] | |
#[macro_use] | |
extern crate tokio; | |
extern crate hyper; | |
use tokio::prelude::*; | |
use hyper::Client; | |
use std::time::Duration; | |
use std::str; | |
async fn do_request() -> Result<String, hyper::error::Error> { | |
let client = Client::new(); | |
let uri = "http://httpbin.org/ip".parse().unwrap(); | |
let response = await!( | |
client | |
.get(uri) | |
.timeout(Duration::from_secs(10)) | |
).unwrap(); | |
let body = await!(response.into_body().concat2()).unwrap(); | |
Ok(String::from_utf8(body.to_vec()).unwrap()) | |
} | |
pub fn main() { | |
tokio::run_async(async { | |
match await!(do_request()) { | |
Ok(body) => println!("{}", body), | |
Err(e) => println!("ERROR: {:?}", e) | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment