Class: Gapic::ServiceStub
- Inherits:
-
Object
- Object
- Gapic::ServiceStub
- Includes:
- UniverseDomainConcerns
- Defined in:
- lib/gapic/grpc/service_stub.rb,
lib/gapic/grpc/service_stub/channel.rb,
lib/gapic/grpc/service_stub/rpc_call.rb,
lib/gapic/grpc/service_stub/channel_pool.rb
Overview
Gapic gRPC Stub
This class wraps the actual gRPC Stub and ChannelPool objects.
Defined Under Namespace
Classes: RpcCall
Constant Summary
Constants included from UniverseDomainConcerns
UniverseDomainConcerns::ENDPOINT_SUBSTITUTION
Instance Attribute Summary collapse
-
#channel_pool ⇒ Gapic::ServiceStub::ChannelPool
readonly
The instance of the ChannelPool class.
-
#grpc_stub ⇒ Object
readonly
The instance of the gRPC stub class (
grpc_stub_class
) constructor argument.
Attributes included from UniverseDomainConcerns
#credentials, #endpoint, #universe_domain
Instance Method Summary collapse
-
#call_rpc(method_name, request, options: nil) {|response, operation| ... } ⇒ Object
Invoke the specified RPC call.
- #create_channel_pool(grpc_stub_class, endpoint:, credentials:, channel_args: nil, interceptors: nil, channel_pool_config: nil) ⇒ Object
- #create_grpc_stub(grpc_stub_class, endpoint:, credentials:, channel_args: nil, interceptors: nil) ⇒ Object
-
#initialize(grpc_stub_class, credentials:, endpoint: nil, endpoint_template: nil, universe_domain: nil, channel_args: nil, interceptors: nil, channel_pool_config: nil) ⇒ ServiceStub
constructor
Creates a Gapic gRPC stub object.
Constructor Details
#initialize(grpc_stub_class, credentials:, endpoint: nil, endpoint_template: nil, universe_domain: nil, channel_args: nil, interceptors: nil, channel_pool_config: nil) ⇒ ServiceStub
Creates a Gapic gRPC stub object.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/gapic/grpc/service_stub.rb', line 68 def initialize grpc_stub_class, credentials:, endpoint: nil, endpoint_template: nil, universe_domain: nil, channel_args: nil, interceptors: nil, channel_pool_config: nil raise ArgumentError, "grpc_stub_class is required" if grpc_stub_class.nil? setup_universe_domain universe_domain: universe_domain, endpoint: endpoint, endpoint_template: endpoint_template, credentials: credentials @channel_pool = nil @grpc_stub = nil channel_args = Hash channel_args interceptors = Array interceptors if channel_pool_config && channel_pool_config.channel_count > 1 create_channel_pool grpc_stub_class, endpoint: self.endpoint, credentials: self.credentials, channel_args: channel_args, interceptors: interceptors, channel_pool_config: channel_pool_config else create_grpc_stub grpc_stub_class, endpoint: self.endpoint, credentials: self.credentials, channel_args: channel_args, interceptors: interceptors end end |
Instance Attribute Details
#channel_pool ⇒ Gapic::ServiceStub::ChannelPool (readonly)
Returns The instance of the ChannelPool class.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 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 150 151 152 153 154 155 156 157 158 159 160 161 162 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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/gapic/grpc/service_stub.rb', line 33 class ServiceStub include UniverseDomainConcerns attr_reader :grpc_stub attr_reader :channel_pool ## # Creates a Gapic gRPC stub object. # # @param grpc_stub_class [Class] gRPC stub class to create a new instance of. # @param endpoint [String] The endpoint of the API. Overrides any endpoint_template. # @param endpoint_template [String] The endpoint of the API, where the # universe domain component of the hostname is marked by the string in # the constant {UniverseDomainConcerns::ENDPOINT_SUBSTITUTION}. # @param universe_domain [String] The universe domain in which calls should # be made. Defaults to `googleapis.com`. # @param credentials [Google::Auth::Credentials, Signet::OAuth2::Client, String, Hash, Proc, # ::GRPC::Core::Channel, ::GRPC::Core::ChannelCredentials] Provides the means for authenticating requests made by # the client. This parameter can be many types: # # * A `Google::Auth::Credentials` uses a the properties of its represented keyfile for authenticating requests # made by this client. # * A `Signet::OAuth2::Client` object used to apply the OAuth credentials. # * A `::GRPC::Core::Channel` will be used to make calls through. # * A `::GRPC::Core::ChannelCredentials` for the setting up the RPC client. The channel credentials should # already be composed with a `::GRPC::Core::CallCredentials` object. # * A `Proc` will be used as an updater_proc for the Grpc channel. The proc transforms the metadata for # requests, generally, to give OAuth credentials. # @param channel_args [Hash] The channel arguments. (This argument is ignored when `credentials` is # provided as a `::GRPC::Core::Channel`.) # @param interceptors [Array<::GRPC::ClientInterceptor>] An array of {::GRPC::ClientInterceptor} objects that will # be used for intercepting calls before they are executed Interceptors are an EXPERIMENTAL API. # @param channel_pool_config [::Gapic::ServiceStub:ChannelPool::Configuration] The configuration for channel # pool. This argument will raise error when `credentials` is provided as a `::GRPC::Core::Channel`. # def initialize grpc_stub_class, credentials:, endpoint: nil, endpoint_template: nil, universe_domain: nil, channel_args: nil, interceptors: nil, channel_pool_config: nil raise ArgumentError, "grpc_stub_class is required" if grpc_stub_class.nil? setup_universe_domain universe_domain: universe_domain, endpoint: endpoint, endpoint_template: endpoint_template, credentials: credentials @channel_pool = nil @grpc_stub = nil channel_args = Hash channel_args interceptors = Array interceptors if channel_pool_config && channel_pool_config.channel_count > 1 create_channel_pool grpc_stub_class, endpoint: self.endpoint, credentials: self.credentials, channel_args: channel_args, interceptors: interceptors, channel_pool_config: channel_pool_config else create_grpc_stub grpc_stub_class, endpoint: self.endpoint, credentials: self.credentials, channel_args: channel_args, interceptors: interceptors end end def create_channel_pool grpc_stub_class, endpoint:, credentials:, channel_args: nil, interceptors: nil, channel_pool_config: nil if credentials.is_a? ::GRPC::Core::Channel raise ArgumentError, "Cannot create a channel pool with GRPC::Core::Channel as credentials" end @channel_pool = ChannelPool.new grpc_stub_class, endpoint: endpoint, credentials: credentials, channel_args: channel_args, interceptors: interceptors, config: channel_pool_config end def create_grpc_stub grpc_stub_class, endpoint:, credentials:, channel_args: nil, interceptors: nil @grpc_stub = case credentials when ::GRPC::Core::Channel grpc_stub_class.new endpoint, nil, channel_override: credentials, interceptors: interceptors when ::GRPC::Core::ChannelCredentials, Symbol grpc_stub_class.new endpoint, credentials, channel_args: channel_args, interceptors: interceptors else updater_proc = credentials.updater_proc if credentials.respond_to? :updater_proc updater_proc ||= credentials if credentials.is_a? Proc raise ArgumentError, "invalid credentials (#{credentials.class})" if updater_proc.nil? call_creds = ::GRPC::Core::CallCredentials.new updater_proc chan_creds = ::GRPC::Core::ChannelCredentials.new.compose call_creds grpc_stub_class.new endpoint, chan_creds, channel_args: channel_args, interceptors: interceptors end end ## # Invoke the specified RPC call. # # @param method_name [Symbol] The RPC method name. # @param request [Object] The request object. # @param options [Gapic::CallOptions, Hash] The options for making the RPC call. A Hash can be provided to # customize the options object, using keys that match the arguments for {Gapic::CallOptions.new}. This object # should only be used once. # # @yield [response, operation] Access the response along with the RPC operation. # @yieldparam response [Object] The response object. # @yieldparam operation [::GRPC::ActiveCall::Operation] The RPC operation for the response. # # @return [Object] The response object. # # @example # require "google/showcase/v1beta1/echo_pb" # require "google/showcase/v1beta1/echo_services_pb" # require "gapic" # require "gapic/grpc" # # echo_channel = ::GRPC::Core::Channel.new( # "localhost:7469", nil, :this_channel_is_insecure # ) # echo_stub = Gapic::ServiceStub.new( # Google::Showcase::V1beta1::Echo::Stub, # endpoint: "localhost:7469", credentials: echo_channel # ) # # request = Google::Showcase::V1beta1::EchoRequest.new # response = echo_stub.call_rpc :echo, request # # @example Using custom call options: # require "google/showcase/v1beta1/echo_pb" # require "google/showcase/v1beta1/echo_services_pb" # require "gapic" # require "gapic/grpc" # # echo_channel = ::GRPC::Core::Channel.new( # "localhost:7469", nil, :this_channel_is_insecure # ) # echo_stub = Gapic::ServiceStub.new( # Google::Showcase::V1beta1::Echo::Stub, # endpoint: "localhost:7469", credentials: echo_channel # ) # # request = Google::Showcase::V1beta1::EchoRequest.new # options = Gapic::CallOptions.new( # retry_policy = { # retry_codes: [::GRPC::Core::StatusCodes::UNAVAILABLE] # } # ) # response = echo_stub.call_rpc :echo, request # options: options # # @example Accessing the response and RPC operation using a block: # require "google/showcase/v1beta1/echo_pb" # require "google/showcase/v1beta1/echo_services_pb" # require "gapic" # require "gapic/grpc" # # echo_channel = ::GRPC::Core::Channel.new( # "localhost:7469", nil, :this_channel_is_insecure # ) # echo_stub = Gapic::ServiceStub.new( # Google::Showcase::V1beta1::Echo::Stub, # endpoint: "localhost:7469", credentials: echo_channel # ) # # request = Google::Showcase::V1beta1::EchoRequest.new # echo_stub.call_rpc :echo, request do |response, operation| # operation.trailing_metadata # end # def call_rpc method_name, request, options: nil, &block if @channel_pool.nil? rpc_call = RpcCall.new @grpc_stub.method method_name rpc_call.call request, options: , &block else @channel_pool.call_rpc method_name, request, options: , &block end end end |
#grpc_stub ⇒ Object (readonly)
Returns The instance of the gRPC stub class (grpc_stub_class
) constructor argument.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 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 150 151 152 153 154 155 156 157 158 159 160 161 162 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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/gapic/grpc/service_stub.rb', line 33 class ServiceStub include UniverseDomainConcerns attr_reader :grpc_stub attr_reader :channel_pool ## # Creates a Gapic gRPC stub object. # # @param grpc_stub_class [Class] gRPC stub class to create a new instance of. # @param endpoint [String] The endpoint of the API. Overrides any endpoint_template. # @param endpoint_template [String] The endpoint of the API, where the # universe domain component of the hostname is marked by the string in # the constant {UniverseDomainConcerns::ENDPOINT_SUBSTITUTION}. # @param universe_domain [String] The universe domain in which calls should # be made. Defaults to `googleapis.com`. # @param credentials [Google::Auth::Credentials, Signet::OAuth2::Client, String, Hash, Proc, # ::GRPC::Core::Channel, ::GRPC::Core::ChannelCredentials] Provides the means for authenticating requests made by # the client. This parameter can be many types: # # * A `Google::Auth::Credentials` uses a the properties of its represented keyfile for authenticating requests # made by this client. # * A `Signet::OAuth2::Client` object used to apply the OAuth credentials. # * A `::GRPC::Core::Channel` will be used to make calls through. # * A `::GRPC::Core::ChannelCredentials` for the setting up the RPC client. The channel credentials should # already be composed with a `::GRPC::Core::CallCredentials` object. # * A `Proc` will be used as an updater_proc for the Grpc channel. The proc transforms the metadata for # requests, generally, to give OAuth credentials. # @param channel_args [Hash] The channel arguments. (This argument is ignored when `credentials` is # provided as a `::GRPC::Core::Channel`.) # @param interceptors [Array<::GRPC::ClientInterceptor>] An array of {::GRPC::ClientInterceptor} objects that will # be used for intercepting calls before they are executed Interceptors are an EXPERIMENTAL API. # @param channel_pool_config [::Gapic::ServiceStub:ChannelPool::Configuration] The configuration for channel # pool. This argument will raise error when `credentials` is provided as a `::GRPC::Core::Channel`. # def initialize grpc_stub_class, credentials:, endpoint: nil, endpoint_template: nil, universe_domain: nil, channel_args: nil, interceptors: nil, channel_pool_config: nil raise ArgumentError, "grpc_stub_class is required" if grpc_stub_class.nil? setup_universe_domain universe_domain: universe_domain, endpoint: endpoint, endpoint_template: endpoint_template, credentials: credentials @channel_pool = nil @grpc_stub = nil channel_args = Hash channel_args interceptors = Array interceptors if channel_pool_config && channel_pool_config.channel_count > 1 create_channel_pool grpc_stub_class, endpoint: self.endpoint, credentials: self.credentials, channel_args: channel_args, interceptors: interceptors, channel_pool_config: channel_pool_config else create_grpc_stub grpc_stub_class, endpoint: self.endpoint, credentials: self.credentials, channel_args: channel_args, interceptors: interceptors end end def create_channel_pool grpc_stub_class, endpoint:, credentials:, channel_args: nil, interceptors: nil, channel_pool_config: nil if credentials.is_a? ::GRPC::Core::Channel raise ArgumentError, "Cannot create a channel pool with GRPC::Core::Channel as credentials" end @channel_pool = ChannelPool.new grpc_stub_class, endpoint: endpoint, credentials: credentials, channel_args: channel_args, interceptors: interceptors, config: channel_pool_config end def create_grpc_stub grpc_stub_class, endpoint:, credentials:, channel_args: nil, interceptors: nil @grpc_stub = case credentials when ::GRPC::Core::Channel grpc_stub_class.new endpoint, nil, channel_override: credentials, interceptors: interceptors when ::GRPC::Core::ChannelCredentials, Symbol grpc_stub_class.new endpoint, credentials, channel_args: channel_args, interceptors: interceptors else updater_proc = credentials.updater_proc if credentials.respond_to? :updater_proc updater_proc ||= credentials if credentials.is_a? Proc raise ArgumentError, "invalid credentials (#{credentials.class})" if updater_proc.nil? call_creds = ::GRPC::Core::CallCredentials.new updater_proc chan_creds = ::GRPC::Core::ChannelCredentials.new.compose call_creds grpc_stub_class.new endpoint, chan_creds, channel_args: channel_args, interceptors: interceptors end end ## # Invoke the specified RPC call. # # @param method_name [Symbol] The RPC method name. # @param request [Object] The request object. # @param options [Gapic::CallOptions, Hash] The options for making the RPC call. A Hash can be provided to # customize the options object, using keys that match the arguments for {Gapic::CallOptions.new}. This object # should only be used once. # # @yield [response, operation] Access the response along with the RPC operation. # @yieldparam response [Object] The response object. # @yieldparam operation [::GRPC::ActiveCall::Operation] The RPC operation for the response. # # @return [Object] The response object. # # @example # require "google/showcase/v1beta1/echo_pb" # require "google/showcase/v1beta1/echo_services_pb" # require "gapic" # require "gapic/grpc" # # echo_channel = ::GRPC::Core::Channel.new( # "localhost:7469", nil, :this_channel_is_insecure # ) # echo_stub = Gapic::ServiceStub.new( # Google::Showcase::V1beta1::Echo::Stub, # endpoint: "localhost:7469", credentials: echo_channel # ) # # request = Google::Showcase::V1beta1::EchoRequest.new # response = echo_stub.call_rpc :echo, request # # @example Using custom call options: # require "google/showcase/v1beta1/echo_pb" # require "google/showcase/v1beta1/echo_services_pb" # require "gapic" # require "gapic/grpc" # # echo_channel = ::GRPC::Core::Channel.new( # "localhost:7469", nil, :this_channel_is_insecure # ) # echo_stub = Gapic::ServiceStub.new( # Google::Showcase::V1beta1::Echo::Stub, # endpoint: "localhost:7469", credentials: echo_channel # ) # # request = Google::Showcase::V1beta1::EchoRequest.new # options = Gapic::CallOptions.new( # retry_policy = { # retry_codes: [::GRPC::Core::StatusCodes::UNAVAILABLE] # } # ) # response = echo_stub.call_rpc :echo, request # options: options # # @example Accessing the response and RPC operation using a block: # require "google/showcase/v1beta1/echo_pb" # require "google/showcase/v1beta1/echo_services_pb" # require "gapic" # require "gapic/grpc" # # echo_channel = ::GRPC::Core::Channel.new( # "localhost:7469", nil, :this_channel_is_insecure # ) # echo_stub = Gapic::ServiceStub.new( # Google::Showcase::V1beta1::Echo::Stub, # endpoint: "localhost:7469", credentials: echo_channel # ) # # request = Google::Showcase::V1beta1::EchoRequest.new # echo_stub.call_rpc :echo, request do |response, operation| # operation.trailing_metadata # end # def call_rpc method_name, request, options: nil, &block if @channel_pool.nil? rpc_call = RpcCall.new @grpc_stub.method method_name rpc_call.call request, options: , &block else @channel_pool.call_rpc method_name, request, options: , &block end end end |
Instance Method Details
#call_rpc(method_name, request, options: nil) {|response, operation| ... } ⇒ Object
Invoke the specified RPC call.
202 203 204 205 206 207 208 209 |
# File 'lib/gapic/grpc/service_stub.rb', line 202 def call_rpc method_name, request, options: nil, &block if @channel_pool.nil? rpc_call = RpcCall.new @grpc_stub.method method_name rpc_call.call request, options: , &block else @channel_pool.call_rpc method_name, request, options: , &block end end |
#create_channel_pool(grpc_stub_class, endpoint:, credentials:, channel_args: nil, interceptors: nil, channel_pool_config: nil) ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/gapic/grpc/service_stub.rb', line 98 def create_channel_pool grpc_stub_class, endpoint:, credentials:, channel_args: nil, interceptors: nil, channel_pool_config: nil if credentials.is_a? ::GRPC::Core::Channel raise ArgumentError, "Cannot create a channel pool with GRPC::Core::Channel as credentials" end @channel_pool = ChannelPool.new grpc_stub_class, endpoint: endpoint, credentials: credentials, channel_args: channel_args, interceptors: interceptors, config: channel_pool_config end |
#create_grpc_stub(grpc_stub_class, endpoint:, credentials:, channel_args: nil, interceptors: nil) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/gapic/grpc/service_stub.rb', line 108 def create_grpc_stub grpc_stub_class, endpoint:, credentials:, channel_args: nil, interceptors: nil @grpc_stub = case credentials when ::GRPC::Core::Channel grpc_stub_class.new endpoint, nil, channel_override: credentials, interceptors: interceptors when ::GRPC::Core::ChannelCredentials, Symbol grpc_stub_class.new endpoint, credentials, channel_args: channel_args, interceptors: interceptors else updater_proc = credentials.updater_proc if credentials.respond_to? :updater_proc updater_proc ||= credentials if credentials.is_a? Proc raise ArgumentError, "invalid credentials (#{credentials.class})" if updater_proc.nil? call_creds = ::GRPC::Core::CallCredentials.new updater_proc chan_creds = ::GRPC::Core::ChannelCredentials.new.compose call_creds grpc_stub_class.new endpoint, chan_creds, channel_args: channel_args, interceptors: interceptors end end |