Skip to content

Commit 0caac5c

Browse files
committed
feat(lib): add shortcut request methods
1 parent 78e8fc7 commit 0caac5c

24 files changed

Lines changed: 322 additions & 196 deletions

examples/form.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
// `tokio = { version = "1", features = ["full"] }`
66
#[tokio::main]
77
async fn main() {
8-
let response = wreq::Client::new()
9-
.post("http://www.baidu.com")
8+
let response = wreq::post("http://www.baidu.com")
109
.form(&[("one", "1")])
1110
.send()
1211
.await

examples/http1_websocket.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
1-
use std::time::Duration;
2-
31
use futures_util::{SinkExt, StreamExt, TryStreamExt};
4-
use wreq::{Client, header, ws::message::Message};
2+
use wreq::{header, ws::message::Message};
53

64
#[tokio::main]
75
async fn main() -> wreq::Result<()> {
86
tracing_subscriber::fmt()
97
.with_max_level(tracing::Level::TRACE)
108
.init();
119

12-
// Build a client
13-
let client = Client::builder()
14-
.cert_verification(false)
15-
.connect_timeout(Duration::from_secs(10))
16-
.build()?;
17-
1810
// Use the API you're already familiar with
19-
let resp = client
20-
.websocket("wss://echo.websocket.org")
11+
let resp = wreq::websocket("wss://echo.websocket.org")
2112
.header(header::USER_AGENT, env!("CARGO_PKG_NAME"))
2213
.read_buffer_size(1024 * 1024)
2314
.send()

examples/http2_websocket.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,17 @@
55
//! cargo run -p example-websockets-http2
66
//! ```
77
8-
use std::time::Duration;
9-
108
use futures_util::{SinkExt, StreamExt, TryStreamExt};
11-
use wreq::{Client, header, ws::message::Message};
9+
use wreq::{header, ws::message::Message};
1210

1311
#[tokio::main]
1412
async fn main() -> wreq::Result<()> {
1513
tracing_subscriber::fmt()
1614
.with_max_level(tracing::Level::TRACE)
1715
.init();
1816

19-
// Build a client
20-
let client = Client::builder()
21-
.connect_timeout(Duration::from_secs(10))
22-
.cert_verification(false)
23-
.build()?;
24-
2517
// Use the API you're already familiar with
26-
let resp = client
27-
.websocket("wss://127.0.0.1:3000/ws")
18+
let resp = wreq::websocket("wss://127.0.0.1:3000/ws")
2819
.force_http2()
2920
.header(header::USER_AGENT, env!("CARGO_PKG_NAME"))
3021
.read_buffer_size(1024 * 1024)

examples/json_dynamic.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
// `tokio = { version = "1", features = ["full"] }`
1010
#[tokio::main]
1111
async fn main() -> wreq::Result<()> {
12-
let echo_json: serde_json::Value = wreq::Client::new()
13-
.post("https://jsonplaceholder.typicode.com/posts")
12+
let echo_json: serde_json::Value = wreq::post("https://jsonplaceholder.typicode.com/posts")
1413
.json(&serde_json::json!({
1514
"title": "wreq.rs",
1615
"body": "https://docs.rs/wreq",

examples/json_typed.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ async fn main() -> wreq::Result<()> {
2727
body: "https://docs.rs/wreq".into(),
2828
user_id: 1,
2929
};
30-
let new_post: Post = wreq::Client::new()
31-
.post("https://jsonplaceholder.typicode.com/posts")
30+
let new_post: Post = wreq::post("https://jsonplaceholder.typicode.com/posts")
3231
.json(&new_post)
3332
.send()
3433
.await?

examples/request_with_emulation.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,7 @@ async fn main() -> wreq::Result<()> {
105105
.build();
106106

107107
// Use the API you're already familiar with
108-
let resp = Client::builder()
109-
.cert_verification(false)
110-
.build()?
111-
.post("https://tls.peet.ws/api/all")
108+
let resp = wreq::post("https://tls.peet.ws/api/all")
112109
.emulation(emulation)
113110
.send()
114111
.await?;

examples/request_with_interface.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ async fn main() -> wreq::Result<()> {
1717
.init();
1818

1919
// Use the API you're already familiar with
20-
let resp = wreq::Client::new()
21-
.get("https://api.ip.sb/ip")
20+
let resp = wreq::get("https://api.ip.sb/ip")
2221
.interface("utun4")
2322
.send()
2423
.await?;

examples/request_with_local_address.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ async fn main() -> wreq::Result<()> {
99
.init();
1010

1111
// Use the API you're already familiar with
12-
let resp = wreq::Client::new()
13-
.get("http://www.baidu.com")
12+
let resp = wreq::get("http://www.baidu.com")
1413
.redirect(Policy::default())
1514
.local_address(IpAddr::from([192, 168, 1, 226]))
1615
.send()

examples/request_with_redirect.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ async fn main() -> wreq::Result<()> {
77
.init();
88

99
// Use the API you're already familiar with
10-
let resp = wreq::Client::new()
11-
.get("http://google.com/")
10+
let resp = wreq::get("http://google.com/")
1211
.redirect(Policy::default())
1312
.send()
1413
.await?;

examples/request_with_version.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ async fn main() -> wreq::Result<()> {
77
.init();
88

99
// Use the API you're already familiar with
10-
let resp = wreq::Client::new()
11-
.get("https://www.google.com")
10+
let resp = wreq::get("https://www.google.com")
1211
.version(Version::HTTP_11)
1312
.send()
1413
.await?;

0 commit comments

Comments
 (0)