Class: BSV::Auth::AuthFetch
- Inherits:
-
Object
- Object
- BSV::Auth::AuthFetch
- Defined in:
- lib/bsv/auth/auth_fetch.rb
Overview
BRC-104 high-level client for authenticated HTTP requests.
Manages a pool of Peer instances (one per base URL) and serialises HTTP requests as BRC-104 binary payloads. The handshake is initiated automatically on the first request to each base URL.
When the server responds with HTTP 402 Payment Required, AuthFetch automatically constructs a BSV payment transaction via the wallet and retries the original request with an x-bsv-payment header attached.
Thread safety: the @peers hash is protected by a mutex. Each in-flight request uses its own Queue to receive exactly the matching response.
Constant Summary collapse
- DEFAULT_TIMEOUT =
30- DEFAULT_PAYMENT_RETRIES =
3- PAYMENT_RETRY_DELAY_MS =
250- PAYMENT_PROTOCOL_ID =
[2, '3241645161d8'].freeze
- SUPPORTED_PAYMENT_VERSION =
'1.0'
Instance Method Summary collapse
-
#fetch(url, method: 'GET', headers: {}, body: nil, timeout: DEFAULT_TIMEOUT) ⇒ AuthResponse
Sends an authenticated HTTP request to
url. -
#initialize(wallet:, requested_certificates: nil, session_manager: nil, payment_max_retries: DEFAULT_PAYMENT_RETRIES) ⇒ AuthFetch
constructor
A new instance of AuthFetch.
Constructor Details
#initialize(wallet:, requested_certificates: nil, session_manager: nil, payment_max_retries: DEFAULT_PAYMENT_RETRIES) ⇒ AuthFetch
Returns a new instance of AuthFetch.
40 41 42 43 44 45 46 47 |
# File 'lib/bsv/auth/auth_fetch.rb', line 40 def initialize(wallet:, requested_certificates: nil, session_manager: nil, payment_max_retries: DEFAULT_PAYMENT_RETRIES) @wallet = wallet @requested_certificates = requested_certificates @session_manager = session_manager @payment_max_retries = payment_max_retries @peers = {} @peers_mutex = Mutex.new end |
Instance Method Details
#fetch(url, method: 'GET', headers: {}, body: nil, timeout: DEFAULT_TIMEOUT) ⇒ AuthResponse
Sends an authenticated HTTP request to url.
The first request to a new base URL performs a BRC-103 mutual-auth handshake automatically. Subsequent requests reuse the cached peer.
If the server responds with 402, a BSV payment is created and the request is retried with an x-bsv-payment header (up to payment_max_retries times).
68 69 70 71 |
# File 'lib/bsv/auth/auth_fetch.rb', line 68 def fetch(url, method: 'GET', headers: {}, body: nil, timeout: DEFAULT_TIMEOUT) do_fetch(url, method: method, headers: headers, body: body, timeout: timeout, retry_count: 0, payment_context: nil) end |