class Gapic::Presenters::Method::RestPaginationInfo

Pagination info determined from the proto method

Attributes

server_streaming[RW]

Whether the underlying proto rpc is a server streaming rpc @return [Boolean]

Public Class Methods

new(proto_method, api) click to toggle source

@param proto_method [Gapic::Schema::Method] the method to derive pagination info from @param api [Gapic::Schema::Api]

# File lib/gapic/presenters/method/rest_pagination_info.rb, line 29
def initialize proto_method, api
  @api = api
  @request = proto_method.input
  @response = proto_method.output
  @server_streaming = proto_method.server_streaming
end

Public Instance Methods

paged?() click to toggle source

Whether the method should be generated as paged

@return [Boolean]

# File lib/gapic/presenters/method/rest_pagination_info.rb, line 40
def paged?
  !server_streaming? && paged_request? && paged_response?
end
paged_element_doc_type() click to toggle source

Proto type of the repeated field in the response message

@return [String, nil]

# File lib/gapic/presenters/method/rest_pagination_info.rb, line 74
def paged_element_doc_type
  return nil if response_results_field.nil?
  field_paginated_elem_doc_type response_results_field
end
repeated_field_is_a_map?() click to toggle source

Whether the repeated field in the response message is a map

@return [Boolean, nil]

# File lib/gapic/presenters/method/rest_pagination_info.rb, line 66
def repeated_field_is_a_map?
  response_results_field&.map?
end
request_page_size_name() click to toggle source

Name of the request's field used for page size For Regapic can be either `page_size` or `max_results`

@return [String, nil]

# File lib/gapic/presenters/method/rest_pagination_info.rb, line 49
def request_page_size_name
  request_page_size_field&.name
end
response_repeated_field_name() click to toggle source

Name of the repeated field in the response message For REST gapics can be either a vanilla repeated field or a map

@return [String, nil]

# File lib/gapic/presenters/method/rest_pagination_info.rb, line 58
def response_repeated_field_name
  response_results_field&.name
end

Private Instance Methods

field_paginated_elem_doc_type(field) click to toggle source

A helper to get a Ruby doc-type for a paginated element.

@param field [Gapic::Schema::Field]

@return [String]

# File lib/gapic/presenters/method/rest_pagination_info.rb, line 196
def field_paginated_elem_doc_type field
  return field_paginated_elem_map_type field if field.map?
  if field.message?
    message_ruby_type field.message
  elsif field.enum?
    # TODO: handle when arg message is nil and enum is the type
    message_ruby_type field.enum
  else
    case field.type
    when 1, 2                              then "::Float"
    when 3, 4, 5, 6, 7, 13, 15, 16, 17, 18 then "::Integer"
    when 9, 12                             then "::String"
    when 8                                 then "::Boolean"
    else
      "::Object"
    end
  end
end
field_paginated_elem_map_type(field) click to toggle source

A helper to get a Ruby doc-type for a proto map's paginated element.

@param field [Gapic::Schema::Field]

@return [String]

# File lib/gapic/presenters/method/rest_pagination_info.rb, line 221
def field_paginated_elem_map_type field
  key_field = field.map_key_field
  value_field = field.map_val_field

  if key_field && value_field
    key_type = field_paginated_elem_doc_type key_field
    value_type = field_paginated_elem_doc_type value_field
    "#{key_type}, #{value_type}"
  else
    class_name
  end
end
message_ruby_type(message) click to toggle source

A helper to get a Ruby type for a proto message.

@param message [Gapic::Schema::Message]

@return [String]

# File lib/gapic/presenters/method/rest_pagination_info.rb, line 240
def message_ruby_type message
  ruby_namespace @api, message.address.join(".")
end
paged_request?() click to toggle source

Whether the request message for the REGAPIC rpc satisfies the criteria for the rpc to be classified and implemented as paged

@return [Boolean]

# File lib/gapic/presenters/method/rest_pagination_info.rb, line 98
def paged_request?
  # Has a String page_token field which specifies the actual (next) page to retrieve.
  # Has an int32 page_size or int32 max_results field
  # which defines the maximum number of paginated resources to return in the response.
  !request_page_token_field.nil? && !request_page_size_field.nil?
end
paged_response?() click to toggle source

Whether the response message for the REGAPIC rpc satisfies the criteria for the rpc to be classified and implemented as paged

@return [Boolean]

# File lib/gapic/presenters/method/rest_pagination_info.rb, line 146
def paged_response?
  # Has the string next_page_token field to be used in the next request as page_token to retrieve the next page.
  # Has only one repeated or map<string, ?> field containing a list of paginated resources.
  !response_next_page_token_field.nil? && !response_results_field.nil?
end
request_page_size_field() click to toggle source

The field in the request that holds a page_size For Regapic can have a name of either `page_size` or `max_results`

@return[Gapic::Schema::Field, nil]

# File lib/gapic/presenters/method/rest_pagination_info.rb, line 121
def request_page_size_field
  @request_page_size_field ||=
    begin
      page_size_names = ["page_size", "max_results"]

      # Has the int32 page_size or int32 max_results field
      # which defines the maximum number of paginated resources to return in the response.
      page_size_types = [
        Google::Protobuf::FieldDescriptorProto::Type::TYPE_UINT32,
        Google::Protobuf::FieldDescriptorProto::Type::TYPE_INT32
      ]

      field = @request.fields.find do |f|
        page_size_names.include?(f.name) && page_size_types.include?(f.type)
      end

      field
    end
end
request_page_token_field() click to toggle source

The field in the request that holds a page_token

@return[Gapic::Schema::Field, nil]

# File lib/gapic/presenters/method/rest_pagination_info.rb, line 109
def request_page_token_field
  # Has a String page_token field which specifies the actual (next) page to retrieve.
  @request_page_token_field ||= @request.fields.find do |f|
    f.name == "page_token" && f.type == Google::Protobuf::FieldDescriptorProto::Type::TYPE_STRING
  end
end
response_next_page_token_field() click to toggle source

The field in the response that holds a next page_token

@return[Gapic::Schema::Field, nil]

# File lib/gapic/presenters/method/rest_pagination_info.rb, line 156
def response_next_page_token_field
  # Has the string next_page_token field to be used in the next request as page_token to retrieve the next page.
  @response_next_page_token_field ||= @response.fields.find do |f|
    f.name == "next_page_token" && f.type == Google::Protobuf::FieldDescriptorProto::Type::TYPE_STRING
  end
end
response_results_field() click to toggle source

The field in the response that holds the results For Regapic can be either a vanilla repeated field or a map

@return [Gapic::Schema::Field, nil]

# File lib/gapic/presenters/method/rest_pagination_info.rb, line 168
def response_results_field
  @response_results_field ||= begin
    map_fields = @response.fields.find_all(&:map?)
    repeated_fields = @response.fields.find_all do |f|
      !f.map? &&
        f.label == Google::Protobuf::FieldDescriptorProto::Label::LABEL_REPEATED
    end

    if map_fields.count == 1
      # If the response message has only one map<string, ?> field
      # treat it as the one with paginated resources (i.e. ignore the repeated fields if any).
      map_fields.first
    elsif repeated_fields.count == 1 && map_fields.empty?
      # If the response message contains only one repeated field,
      # treat that field as the one containing the paginated resources.
      repeated_fields.first
    end
    # If the response message contains more than one repeated field or does not have repeated fields at all
    # but has more than one map<string, ?> field, do not generate any paginated methods for such rpc.
  end
end
server_streaming?() click to toggle source

Whether the underlying proto rpc is a server streaming rpc

@return [Boolean]

# File lib/gapic/presenters/method/rest_pagination_info.rb, line 89
def server_streaming?
  @server_streaming
end