module HTTPX::Plugins::GRPC::InstanceMethods

  1. lib/httpx/plugins/grpc.rb

Public Instance methods

build_stub(origin, service: nil, compression: false)
[show source]
    # File lib/httpx/plugins/grpc.rb
180 def build_stub(origin, service: nil, compression: false)
181   scheme = @options.ssl.empty? ? "http" : "https"
182 
183   origin = URI.parse("#{scheme}://#{origin}")
184 
185   session = self
186 
187   if service && service.respond_to?(:rpc_descs)
188     # it's a grpc generic service
189     service.rpc_descs.each do |rpc_name, rpc_desc|
190       rpc_opts = {
191         marshal_method: rpc_desc.marshal_method,
192         unmarshal_method: rpc_desc.unmarshal_method,
193       }
194 
195       input = rpc_desc.input
196       input = input.type if input.respond_to?(:type)
197 
198       output = rpc_desc.output
199       if output.respond_to?(:type)
200         rpc_opts[:stream] = true
201         output = output.type
202       end
203 
204       session = session.rpc(rpc_name, input, output, **rpc_opts)
205     end
206 
207     service = service.service_name
208   end
209 
210   session.with(origin: origin, grpc_service: service, grpc_compression: compression)
211 end
execute(rpc_method, input, deadline: DEADLINE, metadata: nil, **opts)
[show source]
    # File lib/httpx/plugins/grpc.rb
213 def execute(rpc_method, input,
214             deadline: DEADLINE,
215             metadata: nil,
216             **opts)
217   grpc_request = build_grpc_request(rpc_method, input, deadline: deadline, metadata: metadata, **opts)
218   response = request(grpc_request, **opts)
219   response.raise_for_status unless opts[:stream]
220   GRPC::Call.new(response)
221 end
rpc(rpc_name, input, output, **opts)
[show source]
    # File lib/httpx/plugins/grpc.rb
145         def rpc(rpc_name, input, output, **opts)
146           rpc_name = rpc_name.to_s
147           raise Error, "rpc #{rpc_name} already defined" if @options.grpc_rpcs.key?(rpc_name)
148 
149           rpc_opts = {
150             deadline: @options.grpc_deadline,
151           }.merge(opts)
152 
153           local_rpc_name = rpc_name.underscore
154 
155           session_class = Class.new(self.class) do
156             # define rpc method with ruby style name
157             class_eval(<<-OUT, __FILE__, __LINE__ + 1)
158               def #{local_rpc_name}(input, **opts)              # def grpc_action(input, **opts)
159                 rpc_execute("#{local_rpc_name}", input, **opts) #   rpc_execute("grpc_action", input, **opts)
160               end                                               # end
161             OUT
162 
163             # define rpc method with original name
164             unless local_rpc_name == rpc_name
165               class_eval(<<-OUT, __FILE__, __LINE__ + 1)
166                 def #{rpc_name}(input, **opts)                    # def grpcAction(input, **opts)
167                   rpc_execute("#{local_rpc_name}", input, **opts) #   rpc_execute("grpc_action", input, **opts)
168                 end                                               # end
169               OUT
170             end
171           end
172 
173           session_class.new(@options.merge(
174                               grpc_rpcs: @options.grpc_rpcs.merge(
175                                 local_rpc_name => [rpc_name, input, output, rpc_opts]
176                               ).freeze
177                             ))
178         end
with_channel_credentials(ca_path, key = nil, cert = nil, **ssl_opts)
[show source]
    # File lib/httpx/plugins/grpc.rb
126 def with_channel_credentials(ca_path, key = nil, cert = nil, **ssl_opts)
127   # @type var ssl_params: ::Hash[::Symbol, untyped]
128   ssl_params = {
129     **ssl_opts,
130     ca_file: ca_path,
131   }
132   if key
133     key = File.read(key) if File.file?(key)
134     ssl_params[:key] = OpenSSL::PKey.read(key)
135   end
136 
137   if cert
138     cert = File.read(cert) if File.file?(cert)
139     ssl_params[:cert] = OpenSSL::X509::Certificate.new(cert)
140   end
141 
142   with(ssl: ssl_params)
143 end