pub struct X402Client<TSelector = FirstMatch> { /* private fields */ }client only.Expand description
HTTP middleware over a core PaymentClient.
On 402 Payment Required, signs a payment and retries once (or twice if
on_payment_response signals recovery). Other statuses, including 412 and
502, are never retried.
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(self, scheme: impl SchemeClient + 'static) -> Self
pub fn register(self, scheme: impl SchemeClient + 'static) -> Self
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(self, policy: impl PaymentPolicy + 'static) -> Self
pub fn with_policy(self, policy: impl PaymentPolicy + 'static) -> 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).
Sourcepub fn with_extension(self, extension: impl ClientExtension + 'static) -> Self
pub fn with_extension(self, extension: impl ClientExtension + 'static) -> Self
Adds a client extension (HTTP 402 header hook and payload enrich).
Sourcepub fn with_spend_controls(self, controls: SpendControls) -> Self
pub fn with_spend_controls(self, controls: SpendControls) -> Self
Enables spend controls with the given configuration.
Sourcepub fn disable_spend_controls(self) -> Self
pub fn disable_spend_controls(self) -> Self
Disables all spend controls (any asset, no caps).
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 plus advertised extension headers from a 402.
SIGN-IN-WITH-X is included when a registered extension’s key is in
PaymentRequired.extensions.
§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.
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.
Trait Implementations§
Source§impl<TSelector> Debug for X402Client<TSelector>
impl<TSelector> Debug for X402Client<TSelector>
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,
next.run(req, extensions). Read moreAuto Trait Implementations§
impl<TSelector = FirstMatch> !RefUnwindSafe for X402Client<TSelector>
impl<TSelector = FirstMatch> !UnwindSafe for X402Client<TSelector>
impl<TSelector> Freeze for X402Client<TSelector>where
PaymentClient<TSelector>: Freeze,
impl<TSelector> Send for X402Client<TSelector>where
PaymentClient<TSelector>: Send,
impl<TSelector> Sync for X402Client<TSelector>where
PaymentClient<TSelector>: Sync,
impl<TSelector> Unpin for X402Client<TSelector>where
PaymentClient<TSelector>: Unpin,
impl<TSelector> UnsafeUnpin for X402Client<TSelector>where
PaymentClient<TSelector>: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more