pub struct X402Client<TSelector = FirstMatch> { /* private fields */ }client only.Expand description
HTTP middleware over a core PaymentClient.
Automatically handles 402 Payment Required by signing a payment and
retrying once (or twice if on_payment_response signals recovery).
Implementations§
Source§impl X402Client<FirstMatch>
impl X402Client<FirstMatch>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new client with FirstMatch selection.
Source§impl<TSelector> X402Client<TSelector>
impl<TSelector> X402Client<TSelector>
Sourcepub const fn from_payment_client(inner: PaymentClient<TSelector>) -> Self
pub const fn from_payment_client(inner: PaymentClient<TSelector>) -> Self
Builds from an existing core payment client.
Sourcepub const fn payment_client(&self) -> &PaymentClient<TSelector>
pub const fn payment_client(&self) -> &PaymentClient<TSelector>
Returns a shared reference to the core payment client.
Sourcepub fn register<S>(self, scheme: S) -> Selfwhere
S: SchemeClient + 'static,
pub fn register<S>(self, scheme: S) -> Selfwhere
S: SchemeClient + 'static,
Registers a scheme client.
Sourcepub fn with_selector<P: PaymentSelector>(self, selector: P) -> X402Client<P>
pub fn with_selector<P: PaymentSelector>(self, selector: P) -> X402Client<P>
Sets a custom payment selector.
Sourcepub fn with_policy<P: PaymentPolicy + 'static>(self, policy: P) -> Self
pub fn with_policy<P: PaymentPolicy + 'static>(self, policy: P) -> Self
Adds a payment policy.
Sourcepub fn with_hook(self, hook: impl ClientHooks + 'static) -> Self
pub fn with_hook(self, hook: impl ClientHooks + 'static) -> Self
Adds a client lifecycle hook (including on_payment_response).
Source§impl<TSelector> X402Client<TSelector>where
TSelector: PaymentSelector,
impl<TSelector> X402Client<TSelector>where
TSelector: PaymentSelector,
Sourcepub async fn make_payment_headers(
&self,
res: Response,
) -> Result<HeaderMap, ClientError>
pub async fn make_payment_headers( &self, res: Response, ) -> Result<HeaderMap, ClientError>
Creates Payment-Signature headers from a 402 response.
§Errors
Parse, selection, signing, or before-hook abort.
Sourcepub async fn create_payment(
&self,
payment_required: &PaymentRequired,
) -> Result<CreatedPayment, ClientError>
pub async fn create_payment( &self, payment_required: &PaymentRequired, ) -> Result<CreatedPayment, ClientError>
Creates a payment for an already-parsed challenge.
§Errors
Selection, signing, or before-hook abort.
Source§impl<S> X402Client<S>where
Self: Middleware,
impl<S> X402Client<S>where
Self: Middleware,
Sourcepub fn wrap(self, client: Client) -> ClientWithMiddleware
pub fn wrap(self, client: Client) -> ClientWithMiddleware
Wraps a reqwest Client with x402 payment middleware.
Returns a rqm::ClientWithMiddleware that automatically handles
402 Payment Required responses using registered scheme clients.
Sourcepub fn wrap_builder(self, client: Client) -> ClientBuilder
pub fn wrap_builder(self, client: Client) -> ClientBuilder
Wraps a reqwest Client and returns the middleware builder
for further customization (e.g., adding more middleware).
Trait Implementations§
Source§impl Default for X402Client<FirstMatch>
impl Default for X402Client<FirstMatch>
Source§impl<TSelector> Middleware for X402Client<TSelector>
impl<TSelector> Middleware for X402Client<TSelector>
Source§fn handle<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
req: Request,
extensions: &'life1 mut Extensions,
next: Next<'life2>,
) -> Pin<Box<dyn Future<Output = Result<Response>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn handle<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
req: Request,
extensions: &'life1 mut Extensions,
next: Next<'life2>,
) -> Pin<Box<dyn Future<Output = Result<Response>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Handles a request, automatically handling 402 responses.
When a 402 is received:
- Extract requirements, create signed payment (hooks)
- Retry with
Payment-Signature - Dispatch
on_payment_responseon settle / corrective 402 - If recovered, rebuild payment and retry once more