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 ;
use Duration;
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.
Constructors
UserApi::new(url, timeout)— uses a defaultreqwest::Client.UserApi::with_client(url, client, timeout)— supply your ownreqwest::Clientto share a connection pool, TLS config, proxy, or default headers.
timeout accepts a std::time::Duration, or None for the 5-second default.
let http = builder.user_agent.build?;
let client = with_client;
Errors
Every method returns Result<Res, UserApiError>. On a non-2xx response you get the
Http variant, which carries the server's response body so you can see why a request
was rejected:
match client.get_users_by_id.await
The enum is #[non_exhaustive], so matching downstream should keep a _ arm.
Generated Code
For a client named UserApi, the macro generates:
- A struct
UserApi(derivesClone) withnewandwith_clientconstructors - 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 resourceserror_handling.rs— inspecting a failed request's body
License
Licensed under either of
at your option.