Crates.io | reqwest-enum |
lib.rs | reqwest-enum |
version | 0.3.3 |
source | src |
created_at | 2024-04-02 07:50:20.447484 |
updated_at | 2024-10-08 23:25:45.716564 |
description | Typed enum HTTP API for reqwest. |
homepage | |
repository | https://github.com/hewigovens/reqwest-enum |
max_upload_size | |
id | 1193299 |
size | 21,652 |
Type-safe and enum style API for Rust, some benefits:
Features:
Type-safe and enum style HTTP API
JSON-RPC with batching support
...
cargo add reqwest-enum
or add it to your Cargo.toml
:
[dependencies]
reqwest-enum = "0.3.2"
pub enum HttpBin {
Get,
Post,
Bearer,
}
Target
for the enum:pub trait Target {
fn base_url(&self) -> &'static str;
fn method(&self) -> HTTPMethod;
fn path(&self) -> String;
fn query(&self) -> HashMap<&'static str, &'static str>;
fn headers(&self) -> HashMap<&'static str, &'static str>;
fn authentication(&self) -> Option<AuthMethod>;
fn body(&self) -> HTTPBody;
fn timeout(&self) -> Option<Duration>;
}
let provider = Provider::<HttpBin>::default();
let response = provider.request(HttpBin::Get).await.unwrap();
assert_eq!(response.status(), 200);
Provider also allows you to customize the request by providing a EndpointFn
or RequestBuilderFn
closure if default behavior is not sufficient:
Full example can be found in examples/ethereum-rpc.
pub enum EthereumRPC {
ChainId,
GasPrice,
BlockNumber,
GetBalance(&'static str),
GetBlockByNumber(&'static str, bool),
GetTransactionCount(&'static str, BlockParameter),
Call(TransactionObject, BlockParameter),
EstimateGas(TransactionObject),
SendRawTransaction(&'static str),
}
Target
and JsonRpcTarger
for the enum:pub trait JsonRpcTarget: Target {
fn method_name(&self) -> &'static str;
fn params(&self) -> Vec<Value>;
}
let provider = Provider::<EthereumRPC>::default();
let response: JsonRpcResponse<String> =
provider.request_json(EthereumRPC::ChainId).await.unwrap();
assert_eq!(response.result, "0x1");
MIT or Apache-2.0