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