# Class BSV::Auth::AuthFetch <a id="class-BSV-Auth-AuthFetch"></a>

**Inherits:** `Object`

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 <code>x-bsv-payment</code> 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.

**@example**
```ruby
client = BSV::Auth::AuthFetch.new(wallet: my_wallet)
response = client.fetch('https://api.example.com/resource')
puts response.status   # => 200
puts response.body     # => "..."
```

## Constants
### `DEFAULT_PAYMENT_RETRIES` <a id="constant-DEFAULT_PAYMENT_RETRIES"></a> <a id="DEFAULT_PAYMENT_RETRIES-constant"></a>
Not documented.

### `DEFAULT_TIMEOUT` <a id="constant-DEFAULT_TIMEOUT"></a> <a id="DEFAULT_TIMEOUT-constant"></a>
Not documented.

### `PAYMENT_PROTOCOL_ID` <a id="constant-PAYMENT_PROTOCOL_ID"></a> <a id="PAYMENT_PROTOCOL_ID-constant"></a>
Not documented.

### `PAYMENT_RETRY_DELAY_MS` <a id="constant-PAYMENT_RETRY_DELAY_MS"></a> <a id="PAYMENT_RETRY_DELAY_MS-constant"></a>
Not documented.

### `SUPPORTED_PAYMENT_VERSION` <a id="constant-SUPPORTED_PAYMENT_VERSION"></a> <a id="SUPPORTED_PAYMENT_VERSION-constant"></a>
Not documented.

## Public Instance Methods
### `fetch(url, method: = 'GET', headers: = {}, body: = nil, timeout: = DEFAULT_TIMEOUT)` <a id="method-i-fetch"></a> <a id="fetch-instance_method"></a>
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 <code>x-bsv-payment</code> header (up to `payment_max_retries`
times).
- **@param** `url` [String] full URL including scheme, host, optional port, path, and query
- **@param** `method` [String] HTTP method (default 'GET')
- **@param** `headers` [Hash] request headers; only +content-type+, +authorization+,
and +x-bsv-*+ (excluding +x-bsv-auth-*+) are allowed
- **@param** `body` [String, Hash, nil] request body
- **@param** `timeout` [Integer] seconds to wait for the authenticated response (default 30)
- **@raise** [ArgumentError] if disallowed headers are provided
- **@raise** [Timeout::Error] if no response arrives within +timeout+ seconds
- **@raise** [AuthError] if 402 payment handling fails or max retries are exceeded
- **@return** [AuthResponse]

### `initialize(wallet:, requested_certificates: = nil, session_manager: = nil, payment_max_retries: = DEFAULT_PAYMENT_RETRIES)` <a id="method-i-initialize"></a> <a id="initialize-instance_method"></a>
- **@param** `wallet` [BSV::Wallet::Interface] wallet for crypto operations
- **@param** `requested_certificates` [Hash, nil] certificate set to request from peers
- **@param** `session_manager` [SessionManager, nil] optional shared session store
- **@param** `payment_max_retries` [Integer] maximum number of payment retry attempts (default 3)
- **@return** [AuthFetch] a new instance of AuthFetch
