11 releases (4 breaking)

0.6.0 Nov 15, 2024
0.5.0 Nov 15, 2024
0.4.0 Nov 14, 2024
0.3.1 Sep 11, 2024
0.1.0 Jun 11, 2024

#214 in HTTP client

Download history 668/week @ 2024-09-07 132/week @ 2024-09-14 40/week @ 2024-09-21 56/week @ 2024-09-28 2/week @ 2024-10-05 1/week @ 2024-10-12 228/week @ 2024-11-09 134/week @ 2024-11-16 11/week @ 2024-11-23 12/week @ 2024-11-30

385 downloads per month
Used in auto-merge

GPL-3.0 license

19KB
213 lines

reqwew

HTTP client effortless wrapper.

License Checks Release GitHub tag (latest by date) GitHub code lines GitHub last commit

At the beginning, the goal was to create an easy-to-use wrapper for reqwest.

Now it has evolved into a more generic solution, allowing you to implement the HTTP trait for any client to enjoy the handy features provided by reqwew.

Usage

Async

// std
use std::sync::LazyLock;
// crates.io
use reqwew::{reqwest::Client, Http, Response};
use serde_json::Value;

// Lazy static.
pub static CLIENT: LazyLock<Client> = reqwew::lazy(|| Client::default());

// Async.
let resp = CLIENT
	.request_with_retries(
		CLIENT.request(Method::GET, "https://httpbin.org/get").build().unwrap(),
		3,
		50,
	)
	.await
	.unwrap();

assert!(resp.clone().text().contains("httpbin.org"));
assert_eq!(resp.json::<Value>().unwrap()["headers"]["Host"].as_str().unwrap(), "httpbin.org");

let resp = CLIENT
	.request_with_retries(
		CLIENT.request(Method::POST, "https://httpbin.org/post").body("hello").build().unwrap(),
		3,
		50,
	)
	.await
	.unwrap();

assert!(resp.clone().text().contains("https://httpbin.org/post"));
assert_eq!(resp.json::<Value>().unwrap()["url"].as_str().unwrap(), "https://httpbin.org/post");

Blocking

// std
use std::sync::LazyLock;
// crates.io
use reqwew::{
	blocking::Http as BlockingHttp, reqwest::blocking::Client as BlockingClient, Response,
};
use serde_json::Value;

// Lazy static.
pub static BLOCKING_CLIENT: LazyLock<BlockingClient> = reqwew::lazy(|| BlockingClient::default());

// Blocking.
let resp = BLOCKING_CLIENT
	.request_with_retries(
		BLOCKING_CLIENT.request(Method::GET, "https://httpbin.org/get").build().unwrap(),
		3,
		50,
	)
	.unwrap();

assert!(resp.clone().text().contains("httpbin.org"));
assert_eq!(resp.json::<Value>().unwrap()["headers"]["Host"].as_str().unwrap(), "httpbin.org");

let resp = BLOCKING_CLIENT
	.request_with_retries(
		BLOCKING_CLIENT
			.request(Method::POST, "https://httpbin.org/post")
			.body("hello")
			.build()
			.unwrap(),
		3,
		50,
	)
	.unwrap();

assert!(resp.clone().text().contains("https://httpbin.org/post"));
assert_eq!(resp.json::<Value>().unwrap()["url"].as_str().unwrap(), "https://httpbin.org/post");

Dependencies

~7–24MB
~418K SLoC