Module: Google::Gax::Grpc

Defined in:
lib/google/gax/grpc.rb

Overview

Grpc adapts the gRPC surface

Constant Summary collapse

STATUS_CODE_NAMES =
Hash[
  GRPC::Core::StatusCodes.constants.map do |sym|
    [sym.to_s, GRPC::Core::StatusCodes.const_get(sym)]
  end
].freeze
API_ERRORS =
[GRPC::BadStatus, GRPC::Cancelled].freeze

Class Method Summary collapse

Class Method Details

.create_stub(service_path, port, channel: nil, chan_creds: nil, updater_proc: nil, scopes: nil, interceptors: []) {|address, creds, channel_override, interceptors| ... } ⇒ Object

Creates a gRPC client stub.

Parameters:

  • service_path (String)

    The domain name of the API remote host.

  • port (Fixnum)

    The port on which to connect to the remote host.

  • channel (Object) (defaults to: nil)

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

  • chan_creds (Grpc::Core::ChannelCredentials) (defaults to: nil)

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

  • updater_proc (Proc) (defaults to: nil)

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

  • scopes (Array<String>) (defaults to: nil)

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

  • interceptors (Array<GRPC::ClientInterceptor>) (defaults to: [])

    An array of GRPC::ClientInterceptor objects that will be used for intercepting calls before they are executed Interceptors are an EXPERIMENTAL API.

Yields:

  • (address, creds, channel_override, interceptors)

    the generated gRPC method to create a stub.

Returns:

  • A gRPC client stub.

Raises:

  • (ArgumentError)

    if a combination channel, chan_creds, and updater_proc are passed.



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
# 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) ⇒ Object



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
# 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