beckon
Generate type-safe, async HTTP clients from endpoint definitions.
beckon! takes a client name and a list of endpoints, and expands to a struct with one
async method per endpoint — plus a trait for mocking and a typed error enum. No hand-written
request boilerplate.
Install
You'll also need the runtime dependencies the generated code uses:
Example
use beckon;
use ;
beckon!;
async
Endpoint Fields
Required:
method: HTTP method (GET,POST,PUT,DELETE,PATCH)
Optional:
path: URL path (e.g.,"/users/{id}")res: Response type (defaults to())req: Request body typepath_params: Type for path parametersquery_params: Type for query parametersheaders: Header type (e.g.,reqwest::header::HeaderMap)fn_name: Custom method nameretries: Retry count for this endpoint (overrides the global setting)
Auth
Add automatic authentication to every request. Three strategies are supported:
// Bearer token — injects `Authorization: Bearer <token>`
beckon!;
let client = new;
// Basic auth — injects `Authorization: Basic <base64>`
beckon!;
let client = new;
// API key — injects a custom header
beckon!;
let client = new;
Omitting auth keeps the plain new(url, timeout) constructor. Auth composes with every
other feature, including retries.
Retry with Backoff
Set a global retry count that applies to all endpoints. Retries use exponential backoff (100ms base, 2x multiplier, 5s cap) and trigger on 5xx errors and request timeouts. 4xx errors are never retried.
beckon!;
Per-endpoint retries overrides the global value. Omitting retries entirely means no retries.
Generated Code
For a client named UserApi, the macro generates:
- A struct
UserApiwith anew(url, timeout)constructor - An async method for each endpoint
- A trait
UserApiTraitfor mocking in tests - An error enum
UserApiErrorwith variants for URL, request, HTTP, and deserialization errors
Examples
See the examples/ directory:
basic.rs— simple GET requestsparams.rs— path and query parametersadvanced.rs— all featuresmocking.rs— testing with the generated traitmultiple_path_params.rs— nested resources
License
Licensed under either of
at your option.