Class: Twirbet::Client

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
DSL
Defined in:
lib/twirbet/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DSL

#full_name, #package_name, #rpc, #service_name

Constructor Details

#initialize(base_url, prefix: "/twirp", transport: Transports::NetHTTPTransport.new) ⇒ Client

Returns a new instance of Client.



24
25
26
27
28
# File 'lib/twirbet/client.rb', line 24

def initialize(base_url, prefix: "/twirp", transport: Transports::NetHTTPTransport.new)
  @base_url = base_url
  @prefix = prefix
  @transport = transport
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



15
16
17
# File 'lib/twirbet/client.rb', line 15

def base_url
  @base_url
end

#prefixObject (readonly)

Returns the value of attribute prefix.



18
19
20
# File 'lib/twirbet/client.rb', line 18

def prefix
  @prefix
end

#transportObject (readonly)

Returns the value of attribute transport.



21
22
23
# File 'lib/twirbet/client.rb', line 21

def transport
  @transport
end

Instance Method Details

#call(method_name, request, headers = {}) ⇒ Object

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/twirbet/client.rb', line 31

def call(method_name, request, headers = {})
  method = rpc(method_name)
  raise ArgumentError, "Unknown method: #{method_name}" unless method

  url = "#{base_url}#{prefix}/#{full_name}/#{method_name}"
  body = Encoding.encode_request(request, method.request)
  headers = headers.merge({ "Content-Type" => "application/protobuf" })

  response = transport.call(Transport::Request.new(url, body, headers))
  raise Twirbet::Error.from_response(response) if response.status != 200

  Encoding.decode(response.body, method.response, "application/protobuf")
end