Class: Gapic::Presenters::MethodPresenter

Inherits:
Object
  • Object
show all
Includes:
Helpers::NamespaceHelper
Defined in:
lib/gapic/presenters/method_presenter.rb

Overview

A presenter for rpc methods.

Defined Under Namespace

Classes: YieldParams

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::NamespaceHelper

#ensure_absolute_namespace, #fix_namespace, #ruby_namespace, #ruby_namespace_for_address

Constructor Details

#initialize(service_presenter, api, method) ⇒ MethodPresenter

Returns a new instance of MethodPresenter.

Parameters:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/gapic/presenters/method_presenter.rb', line 55

def initialize service_presenter, api, method
  @service_presenter = service_presenter
  @api = api
  @method = method
  @type = "method"

  # Service config override should only happen for Operations.
  # For the main services we expect service config overrides to be rolled into the protos by the publisher.
  # For all other mixins, since we do not generate them for the service,
  # the overrides should get configured separately.
  is_lro = @service_presenter.address.join(".") == Gapic::Model::Mixins::LRO_SERVICE
  service_config_override_http = is_lro ? @api.service_config : nil
  @http = Gapic::Model::Method::HttpAnnotation.create_with_override @method, service_config_override_http

  @http_bindings = @http.bindings.map { |binding| Gapic::Presenters::Method::HttpBindingPresenter.new binding }
  @routing = Gapic::Model::Method::Routing.new @method.routing, @http
  @lro = Gapic::Model::Method.parse_lro @method, @api

  @rest = MethodRestPresenter.new self, @api
end

Instance Attribute Details

#httpGapic::Model::Method::HttpAnnotation



39
40
41
# File 'lib/gapic/presenters/method_presenter.rb', line 39

def http
  @http
end

#http_bindingsArray<Gapic::Presenters::Method::HttpBindingsPresenter>

Returns:

  • (Array<Gapic::Presenters::Method::HttpBindingsPresenter>)


42
43
44
# File 'lib/gapic/presenters/method_presenter.rb', line 42

def http_bindings
  @http_bindings
end

#lroGapic::Model::Method::AipLro, ...



45
46
47
# File 'lib/gapic/presenters/method_presenter.rb', line 45

def lro
  @lro
end

#methodGapic::Schema::Method



30
31
32
# File 'lib/gapic/presenters/method_presenter.rb', line 30

def method
  @method
end

#restGapic::Presenters::MethodRestPresenter



33
34
35
# File 'lib/gapic/presenters/method_presenter.rb', line 33

def rest
  @rest
end

#routingGapic::Model::Method::Routing



36
37
38
# File 'lib/gapic/presenters/method_presenter.rb', line 36

def routing
  @routing
end

#typeString (readonly)

Returns String representation of this presenter type.

Returns:

  • (String)

    String representation of this presenter type.



48
49
50
# File 'lib/gapic/presenters/method_presenter.rb', line 48

def type
  @type
end

Instance Method Details

#all_snippets(transport: nil) ⇒ Array<Gapic::Presenters::SnippetPresenter>



96
97
98
99
# File 'lib/gapic/presenters/method_presenter.rb', line 96

def all_snippets transport: nil
  configs = @api.snippet_configs_for(@method.full_name) + [nil]
  configs.map { |config| SnippetPresenter.new self, @api, config: config, transport: transport }
end

#argumentsObject



170
171
172
173
# File 'lib/gapic/presenters/method_presenter.rb', line 170

def arguments
  arguments = @method.input.fields.reject(&:output_only?)
  arguments.map { |arg| FieldPresenter.new @api, @method.input, arg }
end

#auto_populated_fieldsArray<Gapic::Presenters::FieldPresenter>

Returns List of auto populated fields.

Returns:



176
177
178
179
180
181
# File 'lib/gapic/presenters/method_presenter.rb', line 176

def auto_populated_fields
  field_names_to_filter = @api..auto_populated_fields_by_method_name[name] || []
  @method.input.fields.filter_map do |field|
    FieldPresenter.new @api, @method.input, field if field_names_to_filter.include? field.name
  end
end

#can_generate_rest?Boolean

Whether this method can be generated in REST clients Only methods with http bindings can be generated, and additionally only unary methods are currently supported.

Returns:

  • (Boolean)


375
376
377
# File 'lib/gapic/presenters/method_presenter.rb', line 375

def can_generate_rest?
  rest.can_generate_rest?
end

#client_streaming?Boolean

Returns:

  • (Boolean)


282
283
284
# File 'lib/gapic/presenters/method_presenter.rb', line 282

def client_streaming?
  @method.client_streaming
end

#doc_description(transport: nil) ⇒ String

The description as it should appear in YARD docs.

Parameters:

  • transport (:grpc, :rest) (defaults to: nil)

    Whether xref links should go to REST or gRPC client classes. Uses the default transport if not provided.

Returns:

  • (String)


146
147
148
# File 'lib/gapic/presenters/method_presenter.rb', line 146

def doc_description transport: nil
  @method.docs_leading_comments transport: transport
end

#doc_response_typeObject



150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/gapic/presenters/method_presenter.rb', line 150

def doc_response_type
  ret = return_type
  ret = "::Gapic::Operation" if lro?
  if server_streaming?
    ret = "::Enumerable<#{ret}>"
  elsif paged?
    paged_type = paged_response_type
    paged_type = "::Gapic::Operation" if paged_type == "::Google::Longrunning::Operation"
    ret = "::Gapic::PagedEnumerable<#{paged_type}>"
  end
  ret
end

#drift_manifestHash

Returns a hash with a drift_manifest of this rpc method describing correspondence between the proto description of the rpc with the generated code for the method. For ruby currently [03/2021] only one method is generated per RPC, so the correspondence is very basic. See https://github.com/googleapis/googleapis/blob/master/gapic/metadata/gapic_metadata.proto

Returns:

  • (Hash)


364
365
366
# File 'lib/gapic/presenters/method_presenter.rb', line 364

def drift_manifest
  { methods: [name] }
end

#fieldsObject



183
184
185
# File 'lib/gapic/presenters/method_presenter.rb', line 183

def fields
  @method.input.fields.map { |field| FieldPresenter.new @api, @method.input, field }
end

#fields_with_first_oneofObject



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/gapic/presenters/method_presenter.rb', line 187

def fields_with_first_oneof
  return fields if @method.input.oneof_decl.empty?

  selected_fields = []
  have_oneof = []

  @method.input.fields.each do |field|
    unless field.oneof?
      selected_fields << field
      next
    end

    idx = field.oneof_index
    selected_fields << field unless have_oneof.include? idx
    have_oneof << idx
  end

  selected_fields.map { |field| FieldPresenter.new @api, @method.input, field }
end

#generate_yardoc_snippets?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/gapic/presenters/method_presenter.rb', line 101

def generate_yardoc_snippets?
  @api.generate_yardoc_snippets?
end

#grpc_full_nameString

Fully qualified proto name of the method (namespace.PascalCase)

Returns:

  • (String)


121
122
123
# File 'lib/gapic/presenters/method_presenter.rb', line 121

def grpc_full_name
  @method.full_name
end

#grpc_method_nameObject



351
352
353
# File 'lib/gapic/presenters/method_presenter.rb', line 351

def grpc_method_name
  @method.name
end

#grpc_nameString

Proto name of the method (PascalCase)

Returns:

  • (String)


115
116
117
# File 'lib/gapic/presenters/method_presenter.rb', line 115

def grpc_name
  @method.name
end

#grpc_service_configObject



340
341
342
343
344
345
# File 'lib/gapic/presenters/method_presenter.rb', line 340

def grpc_service_config
  if @api.grpc_service_config&.service_method_level_configs&.key?(service.grpc_full_name) &&
     @api.grpc_service_config.service_method_level_configs[service.grpc_full_name]&.key?(grpc_method_name)
    @api.grpc_service_config.service_method_level_configs[service.grpc_full_name][grpc_method_name]
  end
end

#grpc_service_config_presenterObject



347
348
349
# File 'lib/gapic/presenters/method_presenter.rb', line 347

def grpc_service_config_presenter
  GrpcServiceConfigPresenter.new grpc_service_config
end

#is_deprecated?Boolean

Returns Whether the method is marked as deprecated.

Returns:

  • (Boolean)

    Whether the method is marked as deprecated.



166
167
168
# File 'lib/gapic/presenters/method_presenter.rb', line 166

def is_deprecated?
  @method.is_deprecated?
end

#kindObject



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/gapic/presenters/method_presenter.rb', line 125

def kind
  if client_streaming?
    if server_streaming?
      :bidi
    else
      :client
    end
  elsif server_streaming?
    :server
  else
    :normal
  end
end

#lro?Boolean

Whether this method uses standard (AIP-151) LROs

Returns:

  • (Boolean)


257
258
259
260
261
# File 'lib/gapic/presenters/method_presenter.rb', line 257

def lro?
  return paged_response_type == "::Google::Longrunning::Operation" if paged?

  return_type == "::Google::Longrunning::Operation"
end

#nameObject



105
106
107
108
109
110
111
# File 'lib/gapic/presenters/method_presenter.rb', line 105

def name
  @name ||= begin
    candidate = ActiveSupport::Inflector.underscore @method.name
    candidate = "call_#{candidate}" if Gapic::RubyInfo.excluded_method_names.include? candidate
    candidate
  end
end

#nonstandard_lro?Boolean

Whether this method uses nonstandard LROs

Returns:

  • (Boolean)


268
269
270
# File 'lib/gapic/presenters/method_presenter.rb', line 268

def nonstandard_lro?
  @lro.nonstandard_lro?
end

#nonstandard_lro_clientGapic::Presenters::Service::LroClientPresenter?

The presenter for the nonstandard LRO client of the kind this method uses

Returns:

  • (Gapic::Presenters::Service::LroClientPresenter, nil)


277
278
279
280
# File 'lib/gapic/presenters/method_presenter.rb', line 277

def nonstandard_lro_client
  return unless nonstandard_lro?
  service.nonstandard_lros.find { |model| model.service == @lro.service_full_name }
end

#paged?Boolean

Returns:

  • (Boolean)


290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/gapic/presenters/method_presenter.rb', line 290

def paged?
  return false if server_streaming? # Cannot page a streaming response

  # HACK(dazuma, 2020-04-06): Two specific RPCs should not be paged.
  # This is an intentionally hard-coded exception (and a temporary one,
  # to be removed when these methods no longer conform to AIP-4233.) For
  # detailed information, see internal link go/actools-talent-pagination.
  address = @method.address.join "."
  return false if address == "google.cloud.talent.v4beta1.ProfileService.SearchProfiles"
  return false if address == "google.cloud.talent.v4beta1.JobService.SearchJobs"
  return false if address == "google.cloud.talent.v4beta1.JobService.SearchJobsForAlert"

  paged_request?(@method.input) && paged_response?(@method.output)
end

#paged_response_repeated_field_nameString

Returns The name of the repeated field in paginated responses.

Returns:

  • (String)

    The name of the repeated field in paginated responses



317
318
319
320
321
322
323
324
# File 'lib/gapic/presenters/method_presenter.rb', line 317

def paged_response_repeated_field_name
  return nil unless paged_response? @method.output

  repeated_field = @method.output.fields.find do |f|
    f.label == :LABEL_REPEATED && f.type == :TYPE_MESSAGE
  end
  repeated_field.name
end

#paged_response_typeObject



305
306
307
308
309
310
311
312
# File 'lib/gapic/presenters/method_presenter.rb', line 305

def paged_response_type
  return nil unless paged_response? @method.output

  repeated_field = @method.output.fields.find do |f|
    f.label == :LABEL_REPEATED && f.type == :TYPE_MESSAGE
  end
  message_ruby_type repeated_field.message
end

#request_typeObject



207
208
209
# File 'lib/gapic/presenters/method_presenter.rb', line 207

def request_type
  message_ruby_type @method.input
end

#return_typeObject



211
212
213
# File 'lib/gapic/presenters/method_presenter.rb', line 211

def return_type
  message_ruby_type @method.output
end

#routing_paramsArray<String>

Returns The segment key names.

Returns:

  • (Array<String>)

    The segment key names.



329
330
331
# File 'lib/gapic/presenters/method_presenter.rb', line 329

def routing_params
  @http.routing_params
end

#routing_params?Boolean

Returns Whether any routing params are present.

Returns:

  • (Boolean)

    Whether any routing params are present



336
337
338
# File 'lib/gapic/presenters/method_presenter.rb', line 336

def routing_params?
  @routing.routing_params?
end

#samplesObject

speech_transcribe_sync_gcs.yaml



244
245
246
247
248
249
250
# File 'lib/gapic/presenters/method_presenter.rb', line 244

def samples
  sample_configs = @api.incode_samples.select do |sample_config|
    sample_config["service"] == @method.address[0...-1].join(".") &&
      sample_config["rpc"] == @method.name
  end
  sample_configs.map { |sample_config| SamplePresenter.new @api, sample_config }
end

#server_streaming?Boolean

Returns:

  • (Boolean)


286
287
288
# File 'lib/gapic/presenters/method_presenter.rb', line 286

def server_streaming?
  @method.server_streaming
end

#serviceGapic::Presenters::ServicePresenter



79
80
81
# File 'lib/gapic/presenters/method_presenter.rb', line 79

def service
  @service_presenter
end

#snippet(transport: nil) ⇒ Gapic::Presenters::SnippetPresenter

Return the "primary" snippet for this method. This should be used for inline snippets.



88
89
90
91
# File 'lib/gapic/presenters/method_presenter.rb', line 88

def snippet transport: nil
  configs = @api.snippet_configs_for @method.full_name
  SnippetPresenter.new self, @api, config: configs.first, transport: transport
end

#yield_doc_descriptionObject



220
221
222
223
224
# File 'lib/gapic/presenters/method_presenter.rb', line 220

def yield_doc_description
  return "Register a callback to be run when an operation is done." if lro?

  "Access the result along with the RPC operation"
end

#yield_paramsObject



229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/gapic/presenters/method_presenter.rb', line 229

def yield_params
  if lro?
    [
      YieldParams.new("operation", "::Gapic::Operation")
    ]
  else
    [
      YieldParams.new("result", return_type),
      YieldParams.new("operation", "::GRPC::ActiveCall::Operation")
    ]
  end
end

#yields?Boolean

Returns:

  • (Boolean)


215
216
217
218
# File 'lib/gapic/presenters/method_presenter.rb', line 215

def yields?
  # Server streaming RCP calls are the only one that does not yield
  !server_streaming?
end