Class: Twirp::ClientJSON
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
-
#initialize(conn, opts = {}) ⇒ ClientJSON
constructor
A new instance of ClientJSON.
-
#rpc(rpc_method, attrs = {}) ⇒ 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.
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.
9 10 11 12 13 14 15 16 |
# File 'lib/twirp/client_json.rb', line 9 def initialize(conn, opts={}) super(conn, opts) package = opts[:package].to_s service = opts[:service].to_s 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 = {}) ⇒ 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.
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/twirp/client_json.rb', line 20 def rpc(rpc_method, attrs={}) body = Encoding.encode_json(attrs) resp = self.class.make_http_request(@conn, @service_full_name, rpc_method, Encoding::JSON, body) if resp.status != 200 return ClientResp.new(nil, self.class.error_from_response(resp)) end data = Encoding.decode_json(resp.body) return ClientResp.new(data, nil) end |