class Barrister::HttpTransport

Default HTTP transport implementation. This is a simple implementation that doesn’t support many options. We may extend this class in the future, but you can always write your own transport class based on this one.

Public Class Methods

new(url) click to toggle source

Takes the URL to the server endpoint and parses it

# File lib/barrister.rb, line 379
def initialize(url)
  @url = url
  @uri = URI.parse(url)
end

Public Instance Methods

request(req) click to toggle source

‘request` is the only required method on a transport class.

‘req` is a JSON-RPC request with `id`, `method`, and optionally `params` slots.

The transport is very simple, and does the following:

  • Serialize ‘req` to JSON. Make sure to use `:ascii_only=true`

  • POST the JSON string to the endpoint, setting the MIME type correctly

  • Deserialize the JSON response string

  • Return the deserialized hash

# File lib/barrister.rb, line 395
def request(req)
  json_str = JSON::generate(req, { :ascii_only=>true })
  http    = Net::HTTP.new(@uri.host, @uri.port)
  request = Net::HTTP::Post.new(@uri.request_uri)
  request.body = json_str
  request["Content-Type"] = "application/json"
  response = http.request(request)
  if response.code != "200"
    raise RpcException.new(-32000, "Non-200 response #{response.code} from #{@url}")
  else
    return JSON::parse(response.body)
  end
end