Class: Remitmd::X402Client
- Inherits:
-
Object
- Object
- Remitmd::X402Client
- Defined in:
- lib/remitmd/x402_client.rb
Overview
x402 client - fetch wrapper that auto-pays HTTP 402 Payment Required responses.
On receiving a 402, the client:
- Decodes the PAYMENT-REQUIRED header (base64 JSON)
- Checks the amount is within max_auto_pay_usdc
- Calls /x402/prepare to get hash + authorization fields
- Signs the hash
- Base64-encodes the PAYMENT-SIGNATURE header
- Retries the original request with payment attached
Instance Attribute Summary collapse
-
#last_payment ⇒ Object
readonly
Returns the value of attribute last_payment.
Instance Method Summary collapse
-
#fetch(url, method: :get, headers: {}, body: nil) ⇒ Net::HTTPResponse
Make an HTTP request, auto-paying any 402 responses within the configured limit.
-
#initialize(wallet:, api_transport: nil, max_auto_pay_usdc: 0.10) ⇒ X402Client
constructor
A new instance of X402Client.
Constructor Details
#initialize(wallet:, api_transport: nil, max_auto_pay_usdc: 0.10) ⇒ X402Client
Returns a new instance of X402Client.
45 46 47 48 49 50 |
# File 'lib/remitmd/x402_client.rb', line 45 def initialize(wallet:, api_transport: nil, max_auto_pay_usdc: 0.10) @wallet = wallet @api_transport = api_transport @max_auto_pay_usdc = max_auto_pay_usdc @last_payment = nil end |
Instance Attribute Details
#last_payment ⇒ Object (readonly)
Returns the value of attribute last_payment.
40 41 42 |
# File 'lib/remitmd/x402_client.rb', line 40 def last_payment @last_payment end |
Instance Method Details
#fetch(url, method: :get, headers: {}, body: nil) ⇒ Net::HTTPResponse
Make an HTTP request, auto-paying any 402 responses within the configured limit.
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/remitmd/x402_client.rb', line 59 def fetch(url, method: :get, headers: {}, body: nil) uri = URI(url) resp = make_request(uri, method, headers, body) if resp.code.to_i == 402 handle402(uri, resp, method, headers, body) else resp end end |