Class: Gruf::Client
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Gruf::Client
- Includes:
- Loggable
- Defined in:
- lib/gruf/client.rb,
lib/gruf/client/error.rb,
lib/gruf/client/errors.rb,
lib/gruf/client/error_factory.rb
Overview
Abstracts out the calling interface for interacting with gRPC clients. Streamlines calling and provides instrumented response objects that also can contain deserialized error messages via serialized objects transported via the service’s trailing metadata.
begin
client = ::Gruf::Client.new(service: ::Demo::ThingService)
response = client.call(:GetMyThing, id: 123)
puts response..inspect
rescue Gruf::Client::Error => e
puts e.error.inspect
end
Utilizes SimpleDelegator to wrap the given service that the client is connecting to, which allows a clean interface to the underlying RPC descriptors and methods.
Direct Known Subclasses
Defined Under Namespace
Modules: Errors Classes: Error, ErrorFactory
Instance Attribute Summary collapse
-
#base_klass ⇒ Object
readonly
Returns the value of attribute base_klass.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#service_klass ⇒ Object
readonly
Returns the value of attribute service_klass.
Instance Method Summary collapse
-
#call(request_method, params = {}, metadata = {}, opts = {}, &block) ⇒ Gruf::Response
Call the client’s method with given params.
-
#initialize(service:, options: {}, client_options: {}) ⇒ Client
constructor
Initialize the client and setup the stub.
-
#timeout ⇒ Integer|NilClass
Returns the currently set timeout on the client stub.
Methods included from Loggable
Constructor Details
#initialize(service:, options: {}, client_options: {}) ⇒ Client
Initialize the client and setup the stub
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/gruf/client.rb', line 57 def initialize(service:, options: {}, client_options: {}) @base_klass = service @service_klass = "#{base_klass}::Service".constantize @opts = || {} @opts[:password] = @opts.fetch(:password, '').to_s @opts[:hostname] = @opts.fetch(:hostname, Gruf.default_client_host) @opts[:channel_credentials] = @opts.fetch(:channel_credentials, Gruf.default_channel_credentials) @error_factory = Gruf::Client::ErrorFactory.new [:timeout] = parse_timeout([:timeout]) if .key?(:timeout) client = "#{service}::Stub".constantize.new(@opts[:hostname], build_ssl_credentials, **) super(client) end |
Instance Attribute Details
#base_klass ⇒ Object (readonly)
Returns the value of attribute base_klass.
40 41 42 |
# File 'lib/gruf/client.rb', line 40 def base_klass @base_klass end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
46 47 48 |
# File 'lib/gruf/client.rb', line 46 def opts @opts end |
#service_klass ⇒ Object (readonly)
Returns the value of attribute service_klass.
43 44 45 |
# File 'lib/gruf/client.rb', line 43 def service_klass @service_klass end |
Instance Method Details
#call(request_method, params = {}, metadata = {}, opts = {}, &block) ⇒ Gruf::Response
Call the client’s method with given params
required for the given above call error type that was returned
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/gruf/client.rb', line 82 def call(request_method, params = {}, = {}, opts = {}, &block) request_method = request_method.to_sym req = if params.respond_to?(:to_proto) || streaming_request?(request_method) params else request_object(request_method, params) end md = () call_sig = call_signature(request_method) unless call_sig raise NotImplementedError, "The method #{request_method} has not been implemented in this service." end resp, operation = execute(call_sig, req, md, opts, &block) raise @error_factory.from_exception(resp.result) unless resp.success? Gruf::Response.new(operation: operation, message: resp.result, execution_time: resp.time) end |
#timeout ⇒ Integer|NilClass
Returns the currently set timeout on the client stub
108 109 110 |
# File 'lib/gruf/client.rb', line 108 def timeout __getobj__.instance_variable_get(:@timeout) end |