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,29 +1,16 @@
|
||||
#![deny(warnings)]
|
||||
|
||||
use futures::{Future, Stream};
|
||||
use reqwest::r#async::{Client, Decoder};
|
||||
use std::io::{self, Cursor};
|
||||
use std::mem;
|
||||
use reqwest::r#async::Client;
|
||||
|
||||
fn fetch() -> impl Future<Item = (), Error = ()> {
|
||||
Client::new()
|
||||
.get("https://hyper.rs")
|
||||
.send()
|
||||
.and_then(|mut res| {
|
||||
println!("{}", res.status());
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), reqwest::Error> {
|
||||
let mut res = Client::new().get("https://hyper.rs").send().await?;
|
||||
|
||||
let body = mem::replace(res.body_mut(), Decoder::empty());
|
||||
body.concat2()
|
||||
})
|
||||
.map_err(|err| println!("request error: {}", err))
|
||||
.map(|body| {
|
||||
let mut body = Cursor::new(body);
|
||||
let _ = io::copy(&mut body, &mut io::stdout()).map_err(|err| {
|
||||
println!("stdout error: {}", err);
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
fn main() {
|
||||
tokio::run(fetch());
|
||||
println!("Status: {}", res.status());
|
||||
|
||||
let body = res.text().await?;
|
||||
|
||||
println!("Body:\n\n{}", body);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -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(())
|
||||
}
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
#![deny(warnings)]
|
||||
|
||||
use std::io::{self, Cursor};
|
||||
use std::mem;
|
||||
use std::path::Path;
|
||||
|
||||
use bytes::Bytes;
|
||||
use futures::{try_ready, Async, Future, Poll, Stream};
|
||||
use reqwest::r#async::{Client, Decoder};
|
||||
use tokio::fs::File;
|
||||
use tokio::io::AsyncRead;
|
||||
|
||||
const CHUNK_SIZE: usize = 1024;
|
||||
|
||||
struct FileSource {
|
||||
inner: File,
|
||||
}
|
||||
|
||||
impl FileSource {
|
||||
fn new(file: File) -> FileSource {
|
||||
FileSource { inner: file }
|
||||
}
|
||||
}
|
||||
|
||||
impl Stream for FileSource {
|
||||
type Item = Bytes;
|
||||
type Error = io::Error;
|
||||
|
||||
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
|
||||
let mut buf = [0; CHUNK_SIZE];
|
||||
let size = try_ready!(self.inner.poll_read(&mut buf));
|
||||
if size > 0 {
|
||||
Ok(Async::Ready(Some(buf[0..size].into())))
|
||||
} else {
|
||||
Ok(Async::Ready(None))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn post<P>(path: P) -> impl Future<Item = (), Error = ()>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
File::open(path.as_ref().to_owned())
|
||||
.map_err(|err| println!("request error: {}", err))
|
||||
.and_then(|file| {
|
||||
let source: Box<dyn Stream<Item = Bytes, Error = io::Error> + Send> =
|
||||
Box::new(FileSource::new(file));
|
||||
|
||||
Client::new()
|
||||
.post("https://httpbin.org/post")
|
||||
.body(source)
|
||||
.send()
|
||||
.and_then(|mut res| {
|
||||
println!("{}", res.status());
|
||||
|
||||
let body = mem::replace(res.body_mut(), Decoder::empty());
|
||||
body.concat2()
|
||||
})
|
||||
.map_err(|err| println!("request error: {}", err))
|
||||
.map(|body| {
|
||||
let mut body = Cursor::new(body);
|
||||
let _ = io::copy(&mut body, &mut io::stdout()).map_err(|err| {
|
||||
println!("stdout error: {}", err);
|
||||
});
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let pool = tokio_threadpool::ThreadPool::new();
|
||||
let path = concat!(env!("CARGO_MANIFEST_DIR"), "/LICENSE-APACHE");
|
||||
tokio::run(pool.spawn_handle(post(path)));
|
||||
}
|
||||
@@ -3,12 +3,11 @@
|
||||
//! This is useful for some ad-hoc experiments and situations when you don't
|
||||
//! really care about the structure of the JSON and just need to display it or
|
||||
//! process it at runtime.
|
||||
use serde_json::json;
|
||||
|
||||
fn main() -> Result<(), reqwest::Error> {
|
||||
let echo_json: serde_json::Value = reqwest::Client::new()
|
||||
.post("https://jsonplaceholder.typicode.com/posts")
|
||||
.json(&json!({
|
||||
.json(&serde_json::json!({
|
||||
"title": "Reqwest.rs",
|
||||
"body": "https://docs.rs/reqwest",
|
||||
"userId": 1
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#![deny(warnings)]
|
||||
|
||||
//! `cargo run --example simple`
|
||||
#![deny(warnings)]
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
env_logger::init();
|
||||
@@ -13,7 +12,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!("Headers:\n{:?}", res.headers());
|
||||
|
||||
// copy the response body directly to stdout
|
||||
std::io::copy(&mut res, &mut std::io::stdout())?;
|
||||
res.copy_to(&mut std::io::stdout())?;
|
||||
|
||||
println!("\n\nDone.");
|
||||
Ok(())
|
||||
|
||||
Reference in New Issue
Block a user