Class: Twirp::ClientJSON

Inherits:
Client
  • Object
show all
Defined in:
lib/twirp/client_json.rb

Overview

Convenience class to call any rpc method with dynamic json attributes, without a service definition. This is useful to test a service before doing any code-generation.

Instance Method Summary collapse

Methods inherited from Client

client_for, error_from_response, is_http_redirect?, make_http_request, rpc_define_method, twirp_error_from_intermediary, twirp_redirect_error

Methods included from ServiceDSL

#package, #package_name, #rpcs, #service, #service_full_name, #service_name

Constructor Details

#initialize(conn, opts = {}) ⇒ ClientJSON

Returns a new instance of ClientJSON.

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
# File 'lib/twirp/client_json.rb', line 22

def initialize(conn, opts={})
  super(conn, opts)

  package = opts[:package].to_s
  service = opts[:service].to_s
  @strict = opts.fetch( :strict, false )
  raise ArgumentError.new("Missing option :service") if service.empty?
  @service_full_name = package.empty? ? service : "#{package}.#{service}"
end

Instance Method Details

#rpc(rpc_method, attrs = {}, req_opts = nil) ⇒ Object

This implementation does not use the defined Protobuf messages to serialize/deserialize data; the request attrs can be anything and the response data is always a plain Hash of attributes.



34
35
36
37
38
39
40
41
42
43
# File 'lib/twirp/client_json.rb', line 34

def rpc(rpc_method, attrs={}, req_opts=nil)
  body = Encoding.encode_json(attrs)

  encoding = @strict ? Encoding::JSON_STRICT : Encoding::JSON
  resp = self.class.make_http_request(@conn, @service_full_name, rpc_method, encoding, req_opts, body)

  rpc_response_thennable(resp) do |resp|
    rpc_response_to_clientresp(resp)
  end
end