Make the async Client default (#626)

The previously default Client is moved to `reqwest::blocking`, while the
async client becomes the main API.

Closes #622
This commit is contained in:
Sean McArthur
2019-09-09 17:20:51 -07:00
committed by GitHub
parent 5fb04356fc
commit 87a09322d6
30 changed files with 1110 additions and 1066 deletions

View File

@@ -1,16 +0,0 @@
#![deny(warnings)]
use reqwest::r#async::Client;
#[tokio::main]
async fn main() -> Result<(), reqwest::Error> {
let mut res = Client::new().get("https://hyper.rs").send().await?;
println!("Status: {}", res.status());
let body = res.text().await?;
println!("Body:\n\n{}", body);
Ok(())
}

View File

@@ -1,41 +0,0 @@
#![deny(warnings)]
use reqwest::r#async::{Client, Response};
use serde::Deserialize;
use std::future::Future;
#[derive(Deserialize, Debug)]
struct Slideshow {
title: String,
author: String,
}
#[derive(Deserialize, Debug)]
struct SlideshowContainer {
slideshow: Slideshow,
}
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 request1 = client.get("https://httpbin.org/json").send();
let request2 = client.get("https://httpbin.org/json").send();
let (try_json1, try_json2) =
futures::future::join(into_json(request1), into_json(request2)).await;
println!("{:?}", try_json1?);
println!("{:?}", try_json2?);
Ok(())
}

19
examples/blocking.rs Normal file
View File

@@ -0,0 +1,19 @@
//! `cargo run --example blocking`
#![deny(warnings)]
fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
println!("GET https://www.rust-lang.org");
let mut res = reqwest::blocking::get("https://www.rust-lang.org/")?;
println!("Status: {}", res.status());
println!("Headers:\n{:?}", res.headers());
// copy the response body directly to stdout
res.copy_to(&mut std::io::stdout())?;
println!("\n\nDone.");
Ok(())
}

View File

@@ -1,7 +1,9 @@
fn main() {
#[tokio::main]
async fn main() {
reqwest::Client::new()
.post("http://www.baidu.com")
.form(&[("one", "1")])
.send()
.await
.unwrap();
}

View File

@@ -4,7 +4,8 @@
//! really care about the structure of the JSON and just need to display it or
//! process it at runtime.
fn main() -> Result<(), reqwest::Error> {
#[tokio::main]
async fn main() -> Result<(), reqwest::Error> {
let echo_json: serde_json::Value = reqwest::Client::new()
.post("https://jsonplaceholder.typicode.com/posts")
.json(&serde_json::json!({
@@ -12,8 +13,10 @@ fn main() -> Result<(), reqwest::Error> {
"body": "https://docs.rs/reqwest",
"userId": 1
}))
.send()?
.json()?;
.send()
.await?
.json()
.await?;
println!("{:#?}", echo_json);
// Object(

View File

@@ -15,7 +15,8 @@ struct Post {
user_id: i32,
}
fn main() -> Result<(), reqwest::Error> {
#[tokio::main]
async fn main() -> Result<(), reqwest::Error> {
let new_post = Post {
id: None,
title: "Reqwest.rs".into(),
@@ -25,8 +26,10 @@ fn main() -> Result<(), reqwest::Error> {
let new_post: Post = reqwest::Client::new()
.post("https://jsonplaceholder.typicode.com/posts")
.json(&new_post)
.send()?
.json()?;
.send()
.await?
.json()
.await?;
println!("{:#?}", new_post);
// Post {

View File

@@ -1,19 +1,17 @@
//! `cargo run --example simple`
#![deny(warnings)]
fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
println!("GET https://www.rust-lang.org");
let mut res = reqwest::get("https://www.rust-lang.org/")?;
#[tokio::main]
async fn main() -> Result<(), reqwest::Error> {
let mut res = reqwest::Client::new()
.get("https://hyper.rs")
.send()
.await?;
println!("Status: {}", res.status());
println!("Headers:\n{:?}", res.headers());
// copy the response body directly to stdout
res.copy_to(&mut std::io::stdout())?;
let body = res.text().await?;
println!("Body:\n\n{}", body);
println!("\n\nDone.");
Ok(())
}