.generator/src/generator/templates/api.j2 in datadog_api_client-1.8.0 vs .generator/src/generator/templates/api.j2 in datadog_api_client-1.9.0
- old
+ new
@@ -2,11 +2,11 @@
{% include "api_info.j2" %}
=end
require 'cgi'
-module {{ config.moduleName }}
+module {{ module_name }}::{{ version|upper }}
{%- set classname = name.replace(" ", "") + "API" %}
class {{ classname }}
attr_accessor :api_client
def initialize(api_client = APIClient.default)
@@ -15,30 +15,23 @@
{%- for path, method, operation in operations|sort(attribute="2.operationId") %}
{%- set httpMethod = method.upper() %}
{%- set returnType = operation|return_type %}
- {{ operation.summary|block_comment|indent(4) }}
- {%- if operation.description %}
- {{ operation.description|block_comment|indent(4) }}
- {%- endif %}
- {%- for name, parameter in operation|parameters if parameter.required %}
- # @param {{ name|attribute_name }} [{{ get_type_for_parameter(parameter) }}] {{ parameter.get("description", "").replace('\n', ' ') }}
- {%- endfor %}
- # @param opts [Hash] the optional parameters
- {%- for name, parameter in operation|parameters if not parameter.required %}
- # @option opts [{{ get_type_for_parameter(parameter) }}] :{{ name|attribute_name }} {{ parameter.get("description", "").replace('\n', ' ') }}{% if "default" in parameter%} (default to {{ parameter.default|format_value }}) {% endif %}
- {%- endfor %}
- # @return [{% if returnType %}{{ returnType }}{% else %}nil{% endif %}]
+ {{ operation.summary|block_comment|indent(4) }}.
+ #
+ # @see #{{ operation.operationId|snake_case }}_with_http_info
def {{ operation.operationId|snake_case }}({% for name, parameter in operation|parameters if parameter.required %}{{ name|attribute_name }}, {% endfor %}opts = {})
{% if returnType %}data, _status_code, _headers = {% endif %}{{ operation.operationId|snake_case }}_with_http_info({% for name, parameter in operation|parameters if parameter.required %}{{ name|attribute_name }}, {% endfor %}opts)
{% if returnType %}data{% else %}nil{% endif %}
end
- {{ operation.summary|block_comment|indent(4) }}
+ {{ operation.summary|block_comment|indent(4) }}.
{%- if operation.description %}
+ #
{{ operation.description|block_comment|indent(4) }}
+ #
{%- endif %}
{%- for name, parameter in operation|parameters if parameter.required %}
# @param {{ name|attribute_name }} [{{ get_type_for_parameter(parameter) }}] {{ parameter.get("description", "").replace('\n', ' ') }}
{%- endfor %}
# @param opts [Hash] the optional parameters
@@ -162,11 +155,11 @@
header_params['Content-Type'] = @api_client.select_header_content_type([{% for mediaType in operation.requestBody.content.keys() %}'{{ mediaType }}'{%- if not loop.last %}, {% endif %}{% endfor %}])
{%- endif %}
{%- for name, parameter in operation|parameters if parameter.in == "header" and parameter.required %}
{%- set collection = parameter|collection_format %}
- header_params['{{ name }}'] = {% if collection %}@api_client.build_collection_param({{ name|attribute_name }}, :{{ collection }}){% else %}{ name|attribute_name }}{% endif %}
+ header_params['{{ name }}'] = {% if collection %}@api_client.build_collection_param({{ name|attribute_name }}, :{{ collection }}){% else %}{{ name|attribute_name }}{% endif %}
{%- endfor %}
{%- for name, parameter in operation|parameters if parameter.in == "header" and not parameter.required %}
{%- set collection = parameter|collection_format %}
header_params['{{ name }}'] = {% if collection %}@api_client.build_collection_param(opts[:'{{ name|attribute_name }}'], :{{ collection }}){% else %}opts[:'{{ name|attribute_name }}']{% endif %} if !opts[:'{{ name|attribute_name }}'].nil?
{%- endfor %}
@@ -206,18 +199,46 @@
:body => post_body,
:auth_names => auth_names,
:return_type => return_type
)
- data, status_code, headers = @api_client.call_api(:{{ httpMethod }}, local_var_path, new_options)
+ data, status_code, headers = @api_client.call_api(Net::HTTP::{{ httpMethod|camel_case }}, local_var_path, new_options)
if @api_client.config.debugging
@api_client.config.logger.debug "API called: {{ classname }}#{{ operation.operationId|snake_case }}\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
end
return data, status_code, headers
end
+{%- if operation["x-pagination"] %}
+{%- set pagination = operation["x-pagination"] %}
+
+ {{ operation.summary|block_comment|indent(4) }}.
+ #
+ # Provide a paginated version of {{ '{' }}#{{ operation.operationId|snake_case }}{{ '}' }}, returning all items.
+ #
+ # To use it you need to use a block: {{ operation.operationId|snake_case }}_with_pagination { |item| p item }
+ #
+ # @yield [{{ get_type_at_path(operation, pagination.resultsPath) }}] Paginated items
+ def {{ operation.operationId|snake_case }}_with_pagination({% for name, parameter in operation|parameters if parameter.required %}{{ name|attribute_name }}, {% endfor %}opts = {})
+ page_size = @api_client.get_attribute_from_path({{ get_container(operation, pagination.limitParam) }}, {{ get_default(operation, pagination.limitParam) }})
+ @api_client.set_attribute_from_path({{ get_container(operation, pagination.limitParam, True) }}, page_size)
+ while true do
+ response = {{ operation.operationId|snake_case }}({% for name, parameter in operation|parameters if parameter.required %}{{ name|attribute_name }}, {% endfor %}opts)
+ @api_client.get_attribute_from_path(response, "{{ pagination.resultsPath|attribute_path }}").each { |item| yield(item) }
+ if @api_client.get_attribute_from_path(response, "{{ pagination.resultsPath|attribute_path }}").length < page_size
+ break
+ end
+ {%- if pagination.pageOffsetParam %}
+ @api_client.set_attribute_from_path({{ get_container(operation, pagination.pageOffsetParam, True) }}, @api_client.get_attribute_from_path({{ get_container(operation, pagination.pageOffsetParam) }}, 0) + page_size)
+ {%- endif %}
+ {%- if pagination.cursorParam %}
+ @api_client.set_attribute_from_path({{ get_container(operation, pagination.cursorParam, True) }}, @api_client.get_attribute_from_path(response, "{{ pagination.cursorPath }}"))
+ {%- endif %}
+ end
+ end
+{%- endif %}
{%- if not loop.last %}
{%- endif %}
{%- endfor %}
end
end
-{# keep new line at the end of file #}
\ No newline at end of file
+{# keep new line at the end of file #}