refactor all to async/await (#617)
Co-authored-by: Danny Browning <danny.browning@protectwise.com> Co-authored-by: Daniel Eades <danieleades@hotmail.com>
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
#![deny(warnings)]
|
||||
|
||||
use futures::Future;
|
||||
use reqwest::r#async::{Client, Response};
|
||||
use serde::Deserialize;
|
||||
use std::future::Future;
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct Slideshow {
|
||||
@@ -15,26 +15,27 @@ struct SlideshowContainer {
|
||||
slideshow: Slideshow,
|
||||
}
|
||||
|
||||
fn fetch() -> impl Future<Item = (), Error = ()> {
|
||||
async fn into_json<F>(f: F) -> Result<SlideshowContainer, reqwest::Error>
|
||||
where
|
||||
F: Future<Output = Result<Response, reqwest::Error>>,
|
||||
{
|
||||
let mut resp = f.await?;
|
||||
resp.json::<SlideshowContainer>().await
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), reqwest::Error> {
|
||||
let client = Client::new();
|
||||
|
||||
let json = |mut res: Response| res.json::<SlideshowContainer>();
|
||||
let request1 = client.get("https://httpbin.org/json").send();
|
||||
|
||||
let request1 = client.get("https://httpbin.org/json").send().and_then(json);
|
||||
let request2 = client.get("https://httpbin.org/json").send();
|
||||
|
||||
let request2 = client.get("https://httpbin.org/json").send().and_then(json);
|
||||
let (try_json1, try_json2) =
|
||||
futures::future::join(into_json(request1), into_json(request2)).await;
|
||||
|
||||
request1
|
||||
.join(request2)
|
||||
.map(|(res1, res2)| {
|
||||
println!("{:?}", res1);
|
||||
println!("{:?}", res2);
|
||||
})
|
||||
.map_err(|err| {
|
||||
println!("stdout error: {}", err);
|
||||
})
|
||||
}
|
||||
|
||||
fn main() {
|
||||
tokio::run(fetch());
|
||||
println!("{:?}", try_json1?);
|
||||
println!("{:?}", try_json2?);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user