Crates.io | reqwew |
lib.rs | reqwew |
version | 0.6.0 |
source | src |
created_at | 2024-06-11 19:42:49.337947 |
updated_at | 2024-11-15 18:09:17.296515 |
description | Reqwest effortless wrapper. |
homepage | https://hack.ink/reqwew |
repository | https://github.com/hack-ink/reqwew |
max_upload_size | |
id | 1268859 |
size | 53,662 |
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.
// 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");
// 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");