Class: Gruf::Client

Inherits:
SimpleDelegator
  • Object
show all
Includes:
Loggable
Defined in:
lib/gruf/client.rb

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#logger

Constructor Details

#initialize(service:, options: {}) ⇒ Client

Returns a new instance of Client.

Parameters:

  • service (Module)

    The namespace of the client Stub that is desired to load

  • options (Hash) (defaults to: {})

Options Hash (options:):

  • :password (String)

    The password for basic authentication for the service.

  • :hostname (String)

    The hostname of the service. Defaults to linkerd.



37
38
39
40
41
42
43
44
45
# File 'lib/gruf/client.rb', line 37

def initialize(service:, options: {})
  @base_klass = service
  @service_klass = "#{base_klass}::Service".constantize
  @opts = options || {}
  @opts[:password] = options.fetch(:password, '').to_s
  @opts[:hostname] = options.fetch(:hostname, Gruf.default_client_host)
  client = "#{service}::Stub".constantize.new(@opts[:hostname], build_ssl_credentials)
  super(client)
end

Instance Attribute Details

#base_klassObject (readonly)

Returns the value of attribute base_klass.



29
30
31
# File 'lib/gruf/client.rb', line 29

def base_klass
  @base_klass
end

#optsObject (readonly)

Returns the value of attribute opts.



29
30
31
# File 'lib/gruf/client.rb', line 29

def opts
  @opts
end

#service_klassObject (readonly)

Returns the value of attribute service_klass.



29
30
31
# File 'lib/gruf/client.rb', line 29

def service_klass
  @service_klass
end

Instance Method Details

#call(request_method, params = {}, metadata = {}) ⇒ Object

Call the client’s method with given params



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/gruf/client.rb', line 50

def call(request_method, params = {},  = {})
  req = request_object(request_method, params)
  md = ()
  call_sig = call_signature(request_method)

  raise NotImplementedError, "The method #{request_method} has not been implemented in this service." unless call_sig

  execute(call_sig, req, md)
rescue GRPC::BadStatus => e
  emk = Gruf..to_s
  raise Gruf::Client::Error, error_deserializer_class.new(e.[emk]).deserialize if e.respond_to?(:metadata) && e..key?(emk)
  raise # passthrough
end