Method: GRPC::GenericService::Dsl#rpc_stub_class

Defined in:
src/ruby/lib/grpc/generic/service.rb

#rpc_stub_classObject

Creates a rpc client class with methods for accessing the methods currently in rpc_descs.



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
# File 'src/ruby/lib/grpc/generic/service.rb', line 148

def rpc_stub_class
  descs = rpc_descs
  route_prefix = service_name
  Class.new(ClientStub) do
    # @param host [String] the host the stub connects to
    # @param creds [Core::ChannelCredentials|Symbol] The channel
    #     credentials to use, or :this_channel_is_insecure otherwise
    # @param kw [KeywordArgs] the channel arguments, plus any optional
    #                         args for configuring the client's channel
    def initialize(host, creds, **kw)
      super(host, creds, **kw)
    end

    # Used define_method to add a method for each rpc_desc.  Each method
    # calls the base class method for the given descriptor.
    descs.each_pair do |name, desc|
      mth_name = GenericService.underscore(name.to_s).to_sym
      marshal = desc.marshal_proc
      unmarshal = desc.unmarshal_proc(:output)
      route = "/#{route_prefix}/#{name}"
      if desc.request_response?
        define_method(mth_name) do |req,  = {}|
          GRPC.logger.debug("calling #{@host}:#{route}")
          request_response(route, req, marshal, unmarshal, **)
        end
      elsif desc.client_streamer?
        define_method(mth_name) do |reqs,  = {}|
          GRPC.logger.debug("calling #{@host}:#{route}")
          client_streamer(route, reqs, marshal, unmarshal, **)
        end
      elsif desc.server_streamer?
        define_method(mth_name) do |req,  = {}, &blk|
          GRPC.logger.debug("calling #{@host}:#{route}")
          server_streamer(route, req, marshal, unmarshal, **, &blk)
        end
      else  # is a bidi_stream
        define_method(mth_name) do |reqs,  = {}, &blk|
          GRPC.logger.debug("calling #{@host}:#{route}")
          bidi_streamer(route, reqs, marshal, unmarshal, **, &blk)
        end
      end
    end
  end
end