class Gapic::Presenters::MethodRestPresenter

A presenter for rpc methods (REST submethods)

Attributes

pagination[R]

@return [Gapic::Presenters::Method::RestPaginationInfo]

Public Class Methods

new(main_method, api) click to toggle source

@param main_method [Gapic::Presenters::MethodPresenter] the main presenter for this method. @param api [Gapic::Schema::Api]

# File lib/gapic/presenters/method_rest_presenter.rb, line 33
def initialize main_method, api
  @api = api
  @main_method = main_method
  @proto_method = main_method.method

  @pagination = Gapic::Presenters::Method::RestPaginationInfo.new @proto_method, api
end

Public Instance Methods

body() click to toggle source

@return [String] A body specified for the given method in proto or an empty string if not specified

# File lib/gapic/presenters/method_rest_presenter.rb, line 138
def body
  @proto_method.http&.body || ""
end
body?() click to toggle source

@return [Boolean] Whether method has body specified in proto

# File lib/gapic/presenters/method_rest_presenter.rb, line 120
def body?
  return false if @proto_method.http.nil?

  !@proto_method.http.body.empty?
end
body_interpolated(request_obj_name = "request_pb") click to toggle source

Performs a limited version of grpc transcoding to create a string that will interpolate the values from the request object to create a request body at runtime. Currently only supports either “*” for “the whole request object” or “value” for “request_object.value”

@param [String] request_obj_name the name of the request object for the interpolation

defaults to "request_pb"

@return [String] A string to interpolate values from the request object into body

# File lib/gapic/presenters/method_rest_presenter.rb, line 160
def body_interpolated request_obj_name = "request_pb"
  return "\"\"" unless body?

  return "#{request_obj_name}.to_json" if body_is_request_object?

  "#{request_obj_name}.#{body}.to_json"
end
body_is_request_object?() click to toggle source

@return [Boolean] True if body contains full request object (`*` in the annotation), false otherwise

# File lib/gapic/presenters/method_rest_presenter.rb, line 145
def body_is_request_object?
  body == "*"
end
body_var_name() click to toggle source

Name of the variable to use for storing the body result of the transcoding call Normally “body” but use “_body” for discarding the result for the calls that do not send body @return [String]

# File lib/gapic/presenters/method_rest_presenter.rb, line 131
def body_var_name
  body? ? "body" : "_body"
end
doc_response_type() click to toggle source

Full class name of the return type of the method (including LRO and Paged cases)

@return [String]

# File lib/gapic/presenters/method_rest_presenter.rb, line 237
def doc_response_type
  return "::Gapic::Rest::PagedEnumerable<#{pagination.paged_element_doc_type}>" if paged?
  return "::Gapic::Rest::BaseOperation" if lro?
  return_type
end
lro?() click to toggle source

Whether the REGAPIC method should be rendered as LRO

TODO (virost, 2021-08) Update this when DiReGapic LRO annotations are added to the Compute protos

@return [Boolean]

# File lib/gapic/presenters/method_rest_presenter.rb, line 258
def lro?
  return_type == "::Google::Cloud::Compute::V1::Operation" &&
    @main_method.service.name != "ZoneOperations" &&
    @main_method.service.name != "RegionOperations" &&
    @main_method.service.name != "GlobalOperations" &&
    @main_method.service.name != "GlobalOrganizationOperations"
end
name() click to toggle source

Method name

@return [String]

# File lib/gapic/presenters/method_rest_presenter.rb, line 209
def name
  @main_method.name
end
paged?() click to toggle source

Whether the REGAPIC method should be rendered as paged

@return [Boolean]

# File lib/gapic/presenters/method_rest_presenter.rb, line 248
def paged?
  @pagination.paged?
end
path() click to toggle source

@return [String] A method path or an empty string if not present

# File lib/gapic/presenters/method_rest_presenter.rb, line 75
def path
  return "" if @proto_method.http.nil?

  verb_path = [
    @proto_method.http.get, @proto_method.http.post, @proto_method.http.put,
    @proto_method.http.patch, @proto_method.http.delete
  ].find { |x| !x.empty? }

  verb_path || @proto_method.http.custom&.path || ""
end
path?() click to toggle source

@return [Boolean] Whether a method path is present and non-empty

# File lib/gapic/presenters/method_rest_presenter.rb, line 68
def path?
  !path.empty?
end
query_string_params() click to toggle source

@return [Array<String>]

# File lib/gapic/presenters/method_rest_presenter.rb, line 176
def query_string_params
  return [] if body_is_request_object?

  routing_params_set = routing_params.to_set
  @main_method.arguments
              .reject { |arg| routing_params_set.include? arg.name }
              .reject { |arg| body == arg.name }
              .reject(&:message?)
              .reject { |arg| arg.default_value_for_type.nil? }
end
query_string_params?() click to toggle source

@return [Boolean] whether any query string parameters are present

# File lib/gapic/presenters/method_rest_presenter.rb, line 171
def query_string_params?
  query_string_params.any?
end
query_string_params_var_name() click to toggle source

Name of the variable to use for storing the query_string_params result of the transcoding call Normally “query_string_params” but use “_query_string_params” for discarding the result for the calls that do not sent query_string_params @return [String]

# File lib/gapic/presenters/method_rest_presenter.rb, line 192
def query_string_params_var_name
  query_string_params? ? "query_string_params" : "_query_string_params"
end
request_type() click to toggle source

Full class name of the request type

@return [String]

# File lib/gapic/presenters/method_rest_presenter.rb, line 218
def request_type
  @main_method.request_type
end
return_type() click to toggle source

Full class name of the raw return type of the RPC

@return [String]

# File lib/gapic/presenters/method_rest_presenter.rb, line 227
def return_type
  @main_method.return_type
end
routing_params() click to toggle source

@return [Array<String>] The segment key names.

# File lib/gapic/presenters/method_rest_presenter.rb, line 96
def routing_params
  Gapic::UriTemplate.parse_arguments path
end
routing_params?() click to toggle source

@return [Boolean] Whether any routing params are present

# File lib/gapic/presenters/method_rest_presenter.rb, line 89
def routing_params?
  routing_params.any?
end
transcoding_helper_name() click to toggle source

Name for the GRPC transcoding helper method

@return [String]

# File lib/gapic/presenters/method_rest_presenter.rb, line 200
def transcoding_helper_name
  "transcode_#{name}_request"
end
uri_interpolated(request_obj_name = "request_pb") click to toggle source

Performs a limited version of grpc transcoding to create a string that will interpolate the values from the request object to create a request URI at runtime. Currently only supports “value” into “request_object.value” @param [String] request_obj_name the name of the request object for the interpolation

defaults to "request_pb"

@return [String] A string to interpolate values from the request object into URI

# File lib/gapic/presenters/method_rest_presenter.rb, line 108
def uri_interpolated request_obj_name = "request_pb"
  return path unless routing_params?

  routing_params.reduce path do |uri, param|
    param_esc = Regexp.escape param
    uri.gsub(/{#{param_esc}[^}]*}/, "\#{#{request_obj_name}.#{param}}")
  end
end
verb() click to toggle source

@return [Symbol] a http verb for this method

# File lib/gapic/presenters/method_rest_presenter.rb, line 51
def verb
  return nil if @proto_method.http.nil?

  method = {
    get:    @proto_method.http.get,
    post:   @proto_method.http.post,
    put:    @proto_method.http.put,
    patch:  @proto_method.http.patch,
    delete: @proto_method.http.delete
  }.find { |_, value| !value.empty? }

  method[0] unless method.nil?
end
verb?() click to toggle source

@return [Boolean] Whether a http verb is present for this method

# File lib/gapic/presenters/method_rest_presenter.rb, line 44
def verb?
  !verb.nil?
end