Class: Gruf::Client
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Gruf::Client
- Includes:
- Loggable
- Defined in:
- lib/gruf/client.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.
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#base_klass ⇒ Class
readonly
The base, friendly name of the service being requested.
-
#opts ⇒ Hash
readonly
A hash of options for the client.
-
#service_klass ⇒ Class
readonly
The class name of the gRPC service being requested.
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
constructor
Initialize the client and setup the stub.
Methods included from Loggable
Constructor Details
#initialize(service:, options: {}) ⇒ Client
Initialize the client and setup the stub
70 71 72 73 74 75 76 77 78 |
# File 'lib/gruf/client.rb', line 70 def initialize(service:, options: {}) @base_klass = service @service_klass = "#{base_klass}::Service".constantize @opts = || {} @opts[:password] = .fetch(:password, '').to_s @opts[:hostname] = .fetch(:hostname, Gruf.default_client_host) client = "#{service}::Stub".constantize.new(@opts[:hostname], build_ssl_credentials) super(client) end |
Instance Attribute Details
#base_klass ⇒ Class (readonly)
Returns The base, friendly name of the service being requested.
56 57 58 |
# File 'lib/gruf/client.rb', line 56 def base_klass @base_klass end |
#opts ⇒ Hash (readonly)
Returns A hash of options for the client.
60 61 62 |
# File 'lib/gruf/client.rb', line 60 def opts @opts end |
#service_klass ⇒ Class (readonly)
Returns The class name of the gRPC service being requested.
58 59 60 |
# File 'lib/gruf/client.rb', line 58 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
for the given above call error type that was returned
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/gruf/client.rb', line 91 def call(request_method, params = {}, = {}, opts = {}, &block) request_method = request_method.to_sym req = streaming_request?(request_method) ? params : 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 resp = execute(call_sig, req, md, opts, &block) Gruf::Response.new(resp.result, resp.time) 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 rescue StandardError => e Gruf.logger.error e. raise end |