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