Class: Async::GRPC::Client
- Inherits:
-
Protocol::HTTP::Middleware
- Object
- Protocol::HTTP::Middleware
- Async::GRPC::Client
- Defined in:
- lib/async/grpc/client.rb
Overview
Represents a client for making gRPC calls over HTTP/2.
Constant Summary collapse
- ENDPOINT =
nil
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
- #The default headers for requests.(defaultheaders) ⇒ Object readonly
Class Method Summary collapse
-
.connect(endpoint) ⇒ Object
Connect to the given endpoint, returning the HTTP client.
-
.open(endpoint = self::ENDPOINT, headers: Protocol::HTTP::Headers.new, **options) ⇒ Object
Create a new client for the given endpoint.
-
.with(parent, headers: {}) ⇒ Object
Create a new client with merged headers from a parent client.
Instance Method Summary collapse
-
#call(request) ⇒ Object
Call the underlying HTTP client with merged headers.
-
#initialize(delegate, headers: Protocol::HTTP::Headers.new) ⇒ Client
constructor
Initialize a new gRPC client.
-
#inspect ⇒ Object
Get a string representation of the client.
-
#invoke(service, method, request = nil, metadata: {}, timeout: nil, encoding: nil, initial: nil, &block) ⇒ Object
Make a gRPC call.
-
#stub(interface_class, service_name) ⇒ Object
Create a stub for the given interface.
-
#to_s ⇒ Object
Get a string representation of the client.
Constructor Details
#initialize(delegate, headers: Protocol::HTTP::Headers.new) ⇒ Client
Initialize a new gRPC client.
68 69 70 71 72 |
# File 'lib/async/grpc/client.rb', line 68 def initialize(delegate, headers: Protocol::HTTP::Headers.new) super(delegate) @headers = headers end |
Instance Attribute Details
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
75 76 77 |
# File 'lib/async/grpc/client.rb', line 75 def headers @headers end |
#The default headers for requests.(defaultheaders) ⇒ Object (readonly)
75 |
# File 'lib/async/grpc/client.rb', line 75 attr_reader :headers |
Class Method Details
.connect(endpoint) ⇒ Object
Connect to the given endpoint, returning the HTTP client.
30 31 32 |
# File 'lib/async/grpc/client.rb', line 30 def self.connect(endpoint) HTTP::Client.new(endpoint) end |
.open(endpoint = self::ENDPOINT, headers: Protocol::HTTP::Headers.new, **options) ⇒ Object
Create a new client for the given endpoint.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/async/grpc/client.rb', line 39 def self.open(endpoint = self::ENDPOINT, headers: Protocol::HTTP::Headers.new, **) endpoint = Async::HTTP::Endpoint.parse(endpoint) if endpoint.is_a?(String) client = connect(endpoint) grpc_client = new(client, headers: headers, **) return grpc_client unless block_given? Sync do yield grpc_client ensure grpc_client.close end end |
.with(parent, headers: {}) ⇒ Object
Create a new client with merged headers from a parent client.
59 60 61 62 63 |
# File 'lib/async/grpc/client.rb', line 59 def self.with(parent, headers: {}) merged_headers = parent.headers.merge(headers) new(parent.delegate, headers: merged_headers) end |
Instance Method Details
#call(request) ⇒ Object
Call the underlying HTTP client with merged headers.
101 102 103 104 105 106 107 |
# File 'lib/async/grpc/client.rb', line 101 def call(request) request.headers = @headers.merge(request.headers) super.tap do |response| response.headers.policy = Protocol::GRPC::HEADER_POLICY end end |
#inspect ⇒ Object
Get a string representation of the client.
79 80 81 |
# File 'lib/async/grpc/client.rb', line 79 def inspect "\#<#{self.class} #{@headers.inspect}>" end |
#invoke(service, method, request = nil, metadata: {}, timeout: nil, encoding: nil, initial: nil, &block) ⇒ Object
Make a gRPC call.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/async/grpc/client.rb', line 121 def invoke(service, method, request = nil, metadata: {}, timeout: nil, encoding: nil, initial: nil, &block) rpc = service.class.lookup_rpc(method) raise ArgumentError, "Unknown method: #{method}" unless rpc path = service.path(method) headers = Protocol::GRPC::Methods.build_headers( metadata: , timeout: timeout, content_type: "application/grpc+proto" ) headers["grpc-encoding"] = encoding if encoding streaming = rpc.streaming request_class = rpc.request_class response_class = rpc.response_class case streaming when :unary unary_call(path, headers, request, request_class, response_class, encoding) when :server_streaming server_streaming_call(path, headers, request, request_class, response_class, encoding, &block) when :client_streaming client_streaming_call(path, headers, request_class, response_class, encoding, &block) when :bidirectional bidirectional_call(path, headers, request_class, response_class, encoding, initial: initial, &block) else raise ArgumentError, "Unknown streaming type: #{streaming}" end end |
#stub(interface_class, service_name) ⇒ Object
Create a stub for the given interface.
93 94 95 96 |
# File 'lib/async/grpc/client.rb', line 93 def stub(interface_class, service_name) interface = interface_class.new(service_name) Stub.new(self, interface) end |
#to_s ⇒ Object
Get a string representation of the client.
85 86 87 |
# File 'lib/async/grpc/client.rb', line 85 def to_s "\#<#{self.class}>" end |