module Google::Gax::Grpc

Grpc adapts the gRPC surface

Constants

API_ERRORS
STATUS_CODE_NAMES

Public Class Methods

create_stub(service_path, port, channel: nil, chan_creds: nil, updater_proc: nil, scopes: nil, interceptors: []) { |address, nil, channel_override: channel, interceptors: interceptors| ... } click to toggle source

Creates a gRPC client stub.

@param service_path [String] The domain name of the API remote host.

@param port [Fixnum] The port on which to connect to the remote host.

@param channel [Object]

A Channel object through which to make calls. If nil, a secure
channel is constructed.

@param chan_creds [Grpc::Core::ChannelCredentials]

A ChannelCredentials object for use with an SSL-enabled Channel.
If nil, credentials are pulled from a default location.

@param updater_proc [Proc]

A function that transforms the metadata for requests, e.g., to give
OAuth credentials.

@param scopes [Array<String>]

The OAuth scopes for this service. This parameter is ignored if
a custom metadata_transformer is supplied.

@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.

@raise [ArgumentError] if a combination channel, chan_creds, and

updater_proc are passed.

@yield [address, creds, channel_override, interceptors]

the generated gRPC method to create a stub.

@return A gRPC client stub.

# File lib/google/gax/grpc.rb, line 116
def create_stub(service_path,
                port,
                channel: nil,
                chan_creds: nil,
                updater_proc: nil,
                scopes: nil,
                interceptors: [])
  verify_params(channel, chan_creds, updater_proc)
  address = "#{service_path}:#{port}"
  default_channel_args = { 'grpc.service_config_disable_resolution' => 1 }
  if channel
    yield(address, nil, channel_override: channel,
                        interceptors: interceptors)
  elsif chan_creds
    yield(address, chan_creds, interceptors: interceptors,
                               channel_args: default_channel_args)
  else
    if updater_proc.nil?
      auth_creds = Google::Auth.get_application_default(scopes)
      updater_proc = auth_creds.updater_proc
    end
    call_creds = GRPC::Core::CallCredentials.new(updater_proc)
    chan_creds = GRPC::Core::ChannelCredentials.new.compose(call_creds)
    yield(address, chan_creds, interceptors: interceptors,
                               channel_args: default_channel_args)
  end
end
deserialize_error_status_details(error) click to toggle source
# File lib/google/gax/grpc.rb, line 50
def deserialize_error_status_details(error)
  return unless error.is_a? GRPC::BadStatus
  # If error status is malformed, swallow the gRPC error that gets raised.
  begin
    details =
      GRPC::GoogleRpcStatusUtils.extract_google_rpc_status(
        error.to_status
      ).details
  rescue
    return 'Could not parse error details due to a malformed server '\
           'response trailer.'
  end
  return if details.nil?
  details =
    GRPC::GoogleRpcStatusUtils.extract_google_rpc_status(
      error.to_status
    ).details
  details.map do |any|
    # deserialize the proto wrapped by the Any in the error details
    begin
      type = Google::Protobuf::DescriptorPool.generated_pool.lookup(
        any.type_name
      )
      any.unpack(type.msgclass)
    rescue
      any
    end
  end
end

Private Class Methods

class_case(modules) click to toggle source

Capitalize all modules except the message class, which is already correctly cased

# File lib/google/gax/grpc.rb, line 158
def self.class_case(modules)
  message = modules.pop
  modules = modules.map(&:capitalize)
  modules << message
end
verify_params(channel, chan_creds, updater_proc) click to toggle source
# File lib/google/gax/grpc.rb, line 146
def self.verify_params(channel, chan_creds, updater_proc)
  if (channel && chan_creds) ||
     (channel && updater_proc) ||
     (chan_creds && updater_proc)
    raise ArgumentError.new('Only one of channel, chan_creds, and ' \
        'updater_proc should be passed into ' \
        'Google::Gax::Grpc#create_stub.')
  end
end

Private Instance Methods

create_stub(service_path, port, channel: nil, chan_creds: nil, updater_proc: nil, scopes: nil, interceptors: []) { |address, nil, channel_override: channel, interceptors: interceptors| ... } click to toggle source

Creates a gRPC client stub.

@param service_path [String] The domain name of the API remote host.

@param port [Fixnum] The port on which to connect to the remote host.

@param channel [Object]

A Channel object through which to make calls. If nil, a secure
channel is constructed.

@param chan_creds [Grpc::Core::ChannelCredentials]

A ChannelCredentials object for use with an SSL-enabled Channel.
If nil, credentials are pulled from a default location.

@param updater_proc [Proc]

A function that transforms the metadata for requests, e.g., to give
OAuth credentials.

@param scopes [Array<String>]

The OAuth scopes for this service. This parameter is ignored if
a custom metadata_transformer is supplied.

@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.

@raise [ArgumentError] if a combination channel, chan_creds, and

updater_proc are passed.

@yield [address, creds, channel_override, interceptors]

the generated gRPC method to create a stub.

@return A gRPC client stub.

# File lib/google/gax/grpc.rb, line 116
def create_stub(service_path,
                port,
                channel: nil,
                chan_creds: nil,
                updater_proc: nil,
                scopes: nil,
                interceptors: [])
  verify_params(channel, chan_creds, updater_proc)
  address = "#{service_path}:#{port}"
  default_channel_args = { 'grpc.service_config_disable_resolution' => 1 }
  if channel
    yield(address, nil, channel_override: channel,
                        interceptors: interceptors)
  elsif chan_creds
    yield(address, chan_creds, interceptors: interceptors,
                               channel_args: default_channel_args)
  else
    if updater_proc.nil?
      auth_creds = Google::Auth.get_application_default(scopes)
      updater_proc = auth_creds.updater_proc
    end
    call_creds = GRPC::Core::CallCredentials.new(updater_proc)
    chan_creds = GRPC::Core::ChannelCredentials.new.compose(call_creds)
    yield(address, chan_creds, interceptors: interceptors,
                               channel_args: default_channel_args)
  end
end
deserialize_error_status_details(error) click to toggle source
# File lib/google/gax/grpc.rb, line 50
def deserialize_error_status_details(error)
  return unless error.is_a? GRPC::BadStatus
  # If error status is malformed, swallow the gRPC error that gets raised.
  begin
    details =
      GRPC::GoogleRpcStatusUtils.extract_google_rpc_status(
        error.to_status
      ).details
  rescue
    return 'Could not parse error details due to a malformed server '\
           'response trailer.'
  end
  return if details.nil?
  details =
    GRPC::GoogleRpcStatusUtils.extract_google_rpc_status(
      error.to_status
    ).details
  details.map do |any|
    # deserialize the proto wrapped by the Any in the error details
    begin
      type = Google::Protobuf::DescriptorPool.generated_pool.lookup(
        any.type_name
      )
      any.unpack(type.msgclass)
    rescue
      any
    end
  end
end