Expand description
§HTTP Provider Macro
A procedural macro for generating HTTP client providers with compile-time endpoint definitions. This macro eliminates boilerplate code when creating HTTP clients by automatically generating methods for your API endpoints.
§Features
- Zero runtime overhead - All HTTP client code is generated at compile time
- Automatic method generation - Function names auto-generated from HTTP method and path
- Type-safe requests/responses - Full Rust type checking for all parameters
- Full HTTP method support - GET, POST, PUT, DELETE
- Path parameters - Dynamic URL path substitution with
{param}syntax - Query parameters - Automatic query string serialization
- Custom headers - Per-request header support
- Async/await - Built on reqwest with full async support
- Configurable timeouts - Per-client timeout configuration
§Quick Start
use http_provider_macro::http_provider;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
struct User {
id: u32,
name: String,
}
#[derive(Serialize)]
struct CreateUser {
name: String,
}
// Define your HTTP provider
http_provider!(
UserApi,
{
{
path: "/users",
method: GET,
res: Vec<User>,
},
{
path: "/users",
method: POST,
req: CreateUser,
res: User,
}
}
);
let base_url = reqwest::Url::parse("https://api.example.com")?;
let client = UserApi::new(base_url, 30);
// Auto-generated methods
let users = client.get_users().await?;
let new_user = client.post_users(&CreateUser {
name: "John".to_string()
}).await?;§Endpoint Configuration
Each endpoint is defined within braces with these fields:
§Required Fields
path: API endpoint path (string literal)method: HTTP method (GET, POST, PUT, DELETE)res: Response type implementingserde::Deserialize
§Optional Fields
fn_name: Custom function name (auto-generated if omitted)req: Request body type implementingserde::Serializeheaders: Header type (typicallyreqwest::header::HeaderMap)query_params: Query parameters type implementingserde::Serializepath_params: Path parameters type with fields matching{param}in path
§Examples
§Path Parameters
#[derive(Serialize)]
struct UserPath {
id: u32,
}
#[derive(Deserialize)]
struct User {
id: u32,
name: String,
}
http_provider!(
UserApi,
{
{
path: "/users/{id}",
method: GET,
path_params: UserPath,
res: User,
}
}
);§Query Parameters and Headers
#[derive(Serialize)]
struct SearchQuery {
q: String,
limit: u32,
}
#[derive(Deserialize)]
struct SearchResults {
results: Vec<String>,
}
http_provider!(
SearchApi,
{
{
path: "/search",
method: GET,
fn_name: search_items,
query_params: SearchQuery,
headers: HeaderMap,
res: SearchResults,
}
}
);Macros§
- http_
provider - Generates an HTTP client provider struct with methods for each defined endpoint.