class Barrister::Client

This is the main class used when writing a client for a Barrister service.

Clients accept a transport class on the constructor which encapsulates serialization and network communciation. Currently this module only provides a basic HTTP transport, but other transports can be easily written.

Attributes

trans[RW]

Public Class Methods

new(trans, validate_req=true, validate_result=true) click to toggle source

Create a new Client. This immediately makes a ‘barrister-idl` request to fetch the IDL from the Server. A Barrister::Contract is created from this IDL and used to expose proxy objects for each interface on the IDL.

  • ‘trans` - Transport instance to use. Must have a `request(req)` method

  • ‘validate_req` - If true, request parameters will be validated against the IDL

    before sending the request to the transport.
  • ‘validate_result` - If true, the result from the server will be validated against the IDL

# File lib/barrister.rb, line 285
def initialize(trans, validate_req=true, validate_result=true)
  @trans           = trans
  @validate_req    = validate_req
  @validate_result = validate_result

  load_contract
  init_proxies
end

Public Instance Methods

get_meta() click to toggle source

Returns the hash of metadata from the Contract, which includes the date the IDL was translated to JSON, the Barrister version used to translate the IDL, and a checksum of the IDL which can be used to detect version changes.

# File lib/barrister.rb, line 297
def get_meta
  return @contract.meta
end
init_proxies() click to toggle source

Internal method invoked by ‘initialize`. Iterates through the Contract and creates proxy classes for each interface.

# File lib/barrister.rb, line 322
def init_proxies
  singleton = class << self; self end
  @contract.interfaces.each do |iface|
    proxy = InterfaceProxy.new(self, iface)
    singleton.send :define_method, iface.name do
      return proxy
    end
  end
end
load_contract() click to toggle source

Internal method invoked by ‘initialize`. Sends a `barrister-idl` request to the server and creates a Barrister::Contract with the result.

# File lib/barrister.rb, line 310
def load_contract
  req = { "jsonrpc" => "2.0", "id" => "1", "method" => "barrister-idl" }
  resp = @trans.request(req)
  if resp.key?("result")
    @contract = Contract.new(resp["result"])
  else
    raise RpcException.new(-32000, "Invalid contract response: #{resp}")
  end
end
request(method, params) click to toggle source

Sends a JSON-RPC request. This method is automatically called by the proxy classes, so in practice you don’t usually call it directly. However, it is available if you wish to avoid the use of proxy classes.

  • ‘method` - string of the method to invoke. Format: “interface.function”.

    For example: "ContactService.saveContact"
    
  • ‘params` - parameters to pass to the function. Must be an Array

# File lib/barrister.rb, line 339
def request(method, params)
  req = { "jsonrpc" => "2.0", "id" => Barrister::rand_str(22), "method" => method }
  if params
    req["params"] = params
  end
  
  # We always validate that the method is valid
  err_resp, iface, func = @contract.resolve_method(req)
  if err_resp != nil
    return err_resp
  end
    
  if @validate_req
    err_resp = @contract.validate_params(req, func)
    if err_resp != nil
      return err_resp
    end
  end
  
  # This makes the request to the server
  resp = @trans.request(req)
  
  if @validate_result && resp != nil && resp.key?("result")
    err_resp = @contract.validate_result(req, resp["result"], func)
    if err_resp != nil
      resp = err_resp
    end
  end
  
  return resp
end
start_batch() click to toggle source

Returns a Barrister::BatchClient instance that is associated with this Client instance

Batches let you send multiple requests in a single round trip

# File lib/barrister.rb, line 304
def start_batch
  return BatchClient.new(self, @contract)
end