Module: HTTPX::Plugins::GRPC::InstanceMethods
- Defined in:
- lib/httpx/plugins/grpc.rb,
sig/plugins/grpc.rbs
Instance Method Summary collapse
-
#build_stub(origin, service: nil, compression: false) ⇒ instance
returns a new GRPC Service enabled session pointing to a
originURI; when theserviceis a GRPC Service object (as the ones provided by thegrpcgem), it'll automatically generate the RPC methods. -
#execute(rpc_method, input, deadline: DEADLINE, metadata: nil, **opts) ⇒ Call
executes an RPC service identified by
rpc_method. -
#rpc(rpc_name, input, output, **opts) ⇒ instance
returns a new session with a service RPC identified by
rpc_name, passinginputandoutputas its input encoder and output decoder respectively; extra options (like :deadline) can be passed usingopts. -
#with_channel_credentials(ca_path, key = nil, cert = nil, **ssl_opts) ⇒ instance
returns a new session which sets the TLS channel credentials (+ca_path+ as the CA file,
keyandcertas the TLS public key and certificate, as well as other TLS options viassl_opts, which should be compatible with what OpenSSL::SSL::SSLContext#set_params receives).
Instance Method Details
#build_stub(origin, service: nil, compression: false) ⇒ instance
returns a new GRPC Service enabled session pointing to a origin URI; when the
service is a GRPC Service object (as the ones provided by the grpc gem), it'll
automatically generate the RPC methods.
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/httpx/plugins/grpc.rb', line 201 def build_stub(origin, service: nil, compression: false) scheme = @options.ssl.empty? ? "http" : "https" origin = URI.parse("#{scheme}://#{origin}") session = self if service && service.respond_to?(:rpc_descs) # it's a grpc generic service service.rpc_descs.each do |rpc_name, rpc_desc| rpc_opts = { marshal_method: rpc_desc.marshal_method, unmarshal_method: rpc_desc.unmarshal_method, } input = rpc_desc.input input = input.type if input.respond_to?(:type) output = rpc_desc.output if output.respond_to?(:type) rpc_opts[:stream] = true output = output.type end session = session.rpc(rpc_name, input, output, **rpc_opts) end service = service.service_name end session.with(origin: origin, grpc_service: service, grpc_compression: compression) end |
#execute(rpc_method, input, deadline: DEADLINE, metadata: nil, **opts) ⇒ Call
executes an RPC service identified by rpc_method. You're recommended to use the generated
RPC methods directly though, in case you're using #build_stub.
236 237 238 239 240 241 242 243 244 |
# File 'lib/httpx/plugins/grpc.rb', line 236 def execute(rpc_method, input, deadline: DEADLINE, metadata: nil, **opts) grpc_request = build_grpc_request(rpc_method, input, deadline: deadline, metadata: , **opts) response = request(grpc_request, **opts) response.raise_for_status unless opts[:stream] GRPC::Call.new(response) end |
#rpc(rpc_name, input, output, **opts) ⇒ instance
returns a new session with a service RPC identified by rpc_name, passing input and output as its
input encoder and output decoder respectively; extra options (like :deadline) can be passed using opts.
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/httpx/plugins/grpc.rb', line 163 def rpc(rpc_name, input, output, **opts) rpc_name = rpc_name.to_s raise Error, "rpc #{rpc_name} already defined" if @options.grpc_rpcs.key?(rpc_name) rpc_opts = { deadline: @options.grpc_deadline, }.merge(opts) local_rpc_name = rpc_name.underscore session_class = Class.new(self.class) do # define rpc method with ruby style name class_eval(<<-OUT, __FILE__, __LINE__ + 1) def #{local_rpc_name}(input, **opts) # def grpc_action(input, **opts) rpc_execute("#{local_rpc_name}", input, **opts) # rpc_execute("grpc_action", input, **opts) end # end OUT # define rpc method with original name unless local_rpc_name == rpc_name class_eval(<<-OUT, __FILE__, __LINE__ + 1) def #{rpc_name}(input, **opts) # def grpcAction(input, **opts) rpc_execute("#{local_rpc_name}", input, **opts) # rpc_execute("grpc_action", input, **opts) end # end OUT end end session_class.new(@options.merge( grpc_rpcs: @options.grpc_rpcs.merge( local_rpc_name => [rpc_name, input, output, rpc_opts] ).freeze )) end |
#with_channel_credentials(ca_path, key = nil, cert = nil, **ssl_opts) ⇒ instance
returns a new session which sets the TLS channel credentials (+ca_path+ as the CA file,
key and cert as the TLS public key and certificate, as well as other TLS options via
ssl_opts, which should be compatible with what OpenSSL::SSL::SSLContext#set_params receives).
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/httpx/plugins/grpc.rb', line 142 def with_channel_credentials(ca_path, key = nil, cert = nil, **ssl_opts) # @type var ssl_params: ::Hash[::Symbol, untyped] ssl_params = { **ssl_opts, ca_file: ca_path, } if key key = File.read(key) if File.file?(key) ssl_params[:key] = OpenSSL::PKey.read(key) end if cert cert = File.read(cert) if File.file?(cert) ssl_params[:cert] = OpenSSL::X509::Certificate.new(cert) end with(ssl: ssl_params) end |