Class: BSV::Auth::SimplifiedFetchTransport

Inherits:
Object
  • Object
show all
Includes:
Transport
Defined in:
lib/bsv/auth/simplified_fetch_transport.rb

Overview

BRC-104 client-side HTTP transport implementing Transport.

Maps BRC-103 auth message types to HTTP requests:

  • Non-general messages (initialRequest, initialResponse, certificateRequest, certificateResponse) are POSTed as JSON to #{base_url}/.well-known/auth. Keys are converted to camelCase on the wire and back to snake_case symbols on receipt.

  • General messages have their binary payload deserialised via AuthPayload.deserialize_request, sent as a real HTTP request with x-bsv-auth-* headers attached, and the HTTP response serialised back to a binary payload via AuthPayload.serialize_response.

Examples:

transport = BSV::Auth::SimplifiedFetchTransport.new('https://api.example.com')
transport.on_data { |msg| peer.handle_incoming_message(msg) }
transport.send(message)

Instance Method Summary collapse

Constructor Details

#initialize(base_url, http_client: nil) ⇒ SimplifiedFetchTransport

Returns a new instance of SimplifiedFetchTransport.

Parameters:

  • base_url (String)

    scheme + host (+ optional port), e.g. “api.example.com

  • http_client (Class, nil) (defaults to: nil)

    injectable HTTP client; defaults to Net::HTTP



33
34
35
36
37
# File 'lib/bsv/auth/simplified_fetch_transport.rb', line 33

def initialize(base_url, http_client: nil)
  @base_url    = base_url.chomp('/')
  @http_client = http_client || Net::HTTP
  @on_data_callback = nil
end

Instance Method Details

#on_data {|message| ... } ⇒ Object

Registers the callback invoked when a response arrives.

Must be called before #send.

Yield Parameters:

  • message (Hash)

    incoming auth message with symbol keys



44
45
46
# File 'lib/bsv/auth/simplified_fetch_transport.rb', line 44

def on_data(&block)
  @on_data_callback = block
end

#send(message) ⇒ Object

Sends an auth message to the remote peer.

Parameters:

  • message (Hash)

    auth message produced by Peer

Raises:

  • (RuntimeError)

    if no callback has been registered via #on_data



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/bsv/auth/simplified_fetch_transport.rb', line 52

def send(message)
  raise "#{self.class}#send called before on_data callback was registered" if @on_data_callback.nil?

  type = fetch_key(message, :message_type)

  if type == MSG_GENERAL
    send_general(message)
  else
    send_non_general(message)
  end
end