Class: Yrpc::Client

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/yrpc/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Client.



4
5
6
7
8
9
10
11
12
# File 'lib/yrpc/client.rb', line 4

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

Instance Attribute Details

#base_klassObject (readonly)

Returns the value of attribute base_klass.



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

def base_klass
  @base_klass
end

#optsObject (readonly)

Returns the value of attribute opts.



16
17
18
# File 'lib/yrpc/client.rb', line 16

def opts
  @opts
end

#service_klassObject (readonly)

Returns the value of attribute service_klass.



14
15
16
# File 'lib/yrpc/client.rb', line 14

def service_klass
  @service_klass
end

Instance Method Details

#build_metadata(metadata = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/yrpc/client.rb', line 29

def ( = {})
  unless opts[:password].empty?
    username = opts.fetch(:username, 'grpc').to_s
    username = username.empty? ? '' : "#{username}:"
    auth_string = Base64.encode64("#{username}#{opts[:password]}")
    [:authorization] = "Basic #{auth_string}".tr("\n", '')
  end
  
end

#build_ssl_credentialsObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/yrpc/client.rb', line 61

def build_ssl_credentials
  cert = nil
  if opts[:ssl_certificate_file]
    cert = File.read(opts[:ssl_certificate_file]).to_s.strip
  elsif opts[:ssl_certificate]
    cert = opts[:ssl_certificate].to_s.strip
  end

  cert ? GRPC::Core::ChannelCredentials.new(cert) : :this_channel_is_insecure
end

#call_signature(request_method) ⇒ Object



56
57
58
59
# File 'lib/yrpc/client.rb', line 56

def call_signature(request_method)
  desc = rpc_desc(request_method)
  desc && desc.name ? desc.name.to_s.underscore.to_sym : nil
end

#execute(call_sig, req, metadata, opts = {}, &block) ⇒ Object



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

def execute(call_sig, req, , opts = {}, &block)
  # Timer.time do
  opts[:return_op] = true
  opts[:metadata] = 
  # end
  send(call_sig, req, opts, &block)
end

#invoke(request_method, params = {}, metadata = {}, opts = {}, &block) ⇒ Object

Raises:

  • (NotImplementedError)


18
19
20
21
22
23
24
25
26
27
# File 'lib/yrpc/client.rb', line 18

def invoke(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)
  p resp
  Response.new(resp)
end

#request_object(request_method, params = {}) ⇒ Object



51
52
53
54
# File 'lib/yrpc/client.rb', line 51

def request_object(request_method, params = {})
  desc = rpc_desc(request_method)
  desc && desc.input ? desc.input.new(params) : nil
end

#rpc_desc(request_method) ⇒ Object



47
48
49
# File 'lib/yrpc/client.rb', line 47

def rpc_desc(request_method)
  service_klass.rpc_descs[request_method]
end

#streaming_request?(request_method) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
# File 'lib/yrpc/client.rb', line 72

def streaming_request?(request_method)
  desc = rpc_desc(request_method)
  desc && (desc.client_streamer? || desc.bidi_streamer?)
end