HTTP Provider Macro
A Rust procedural macro that generates HTTP client providers with compile-time endpoint definitions. This macro eliminates boilerplate code for 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
Add this to your Cargo.toml:
[]
= "0.1.0"
= { = "0.11", = ["json"] }
= { = "1.0", = ["derive"] }
= { = "1.0", = ["full"] }
Basic Usage
use http_provider;
use ;
// Define your HTTP provider
http_provider!;
async
Endpoint Configuration
Each endpoint is defined within braces {} with the following fields:
Required Fields
path: The API endpoint path (string literal)method: HTTP method (GET,POST,PUT,DELETE)res: Response type that implementsDeserialize
Optional Fields
fn_name: Custom function name (defaults to auto-generated)req: Request body type that implementsSerializeheaders: Header type (typicallyreqwest::header::HeaderMap)query_params: Query parameters type that implementsSerializepath_params: Path parameters type with fields matching{param}in path
Advanced Examples
Custom Function Names and Headers
use HeaderMap;
http_provider!;
// Usage
let mut headers = new;
headers.insert;
let data = client.fetch_protected_data.await?;
Query Parameters
http_provider!;
// Usage
let results = client.get_search.await?;
Complex Path Parameters
http_provider!;
// Usage
let resource = client.get_users_user_id_resources_resource_id.await?;
All Parameters Combined
http_provider!;
// Usage
let post = client.create_user_post.await?;
Generated Code Structure
The macro generates:
- Struct Definition: A provider struct with
url,client, andtimeoutfields - Constructor:
new(url: reqwest::Url, timeout: u64) -> Self - HTTP Methods: One async method per endpoint definition
Method Signatures
Generated methods follow this pattern:
pub async
Auto-generated Function Names
When fn_name is not specified, names are generated as:
{method}_{path}where path slashes become underscores- Examples:
GET /users→get_usersPOST /api/v1/posts→post_api_v1_postsPUT /users/{id}→put_users_id
Error Handling
All generated methods return Result<T, String> where errors include:
- URL construction errors: Invalid path parameter substitution
- Network errors: Connection timeouts, DNS failures, etc.
- HTTP errors: Non-2xx status codes with status information
- Deserialization errors: JSON parsing failures
Requirements
- Rust 1.70+: For latest async/await and procedural macro features
- reqwest: HTTP client library
- serde: Serialization framework
- tokio: Async runtime
License
Licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.