=begin {% include "api_info.j2" %} =end require 'cgi' module {{ module_name }}::{{ version|upper }} {%- set classname = name.replace(" ", "") + "API" %} class {{ classname }} attr_accessor :api_client def initialize(api_client = APIClient.default) @api_client = api_client end {%- 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) }}. # # @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) }}. {%- 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|json }}) {% endif %} {%- endfor %} # @return [Array<({% if returnType %}{{ returnType }}{% else %}nil{% endif %}, Integer, Hash)>] {% if returnType %}{{ returnType }} data{% else %}nil{% endif %}, response status code and response headers def {{ operation.operationId|snake_case }}_with_http_info({% for name, parameter in operation|parameters if parameter.required %}{{ name|attribute_name }}, {% endfor %}opts = {}) if @api_client.config.unstable_operations.has_key?(:{{ operation.operationId|snake_case }}) unstable_enabled = @api_client.config.unstable_operations[:{{ operation.operationId|snake_case }}] if unstable_enabled @api_client.config.logger.warn format("Using unstable operation '%s'", "{{ operation.operationId|snake_case }}") else raise APIError.new(message: format("Unstable operation '%s' is disabled", "{{ operation.operationId|snake_case }}")) end end if @api_client.config.debugging @api_client.config.logger.debug 'Calling API: {{classname}}.{{ operation.operationId|snake_case }} ...' end {%- for name, parameter in operation|parameters %} {%- set schema = parameter|parameter_schema %} {%- if parameter.required %} {%- if not parameter.nullable %} # verify the required parameter '{{ name|attribute_name }}' is set if @api_client.config.client_side_validation && {{ name|attribute_name }}.nil? fail ArgumentError, "Missing the required parameter '{{ name|attribute_name }}' when calling {{classname}}.{{ operation.operationId|snake_case }}" end {%- if schema.enum is defined and not parameter|collection_format %} # verify enum value allowable_values = [{% for value in schema.enum %}{{ value|format_value }}{%- if not loop.last %}, {% endif %}{% endfor %}] if @api_client.config.client_side_validation && !allowable_values.include?({{ name|attribute_name }}) fail ArgumentError, "invalid value for \"{{ name|attribute_name }}\", must be one of #{allowable_values}" end {%- endif %} {%- endif %} {%- else %} {%- if schema.enum is defined %} allowable_values = [{% for value in schema.enum %}{{ value|format_value }}{%- if not loop.last %}, {% endif %}{% endfor %}] {%- if parameter|collection_format %} if @api_client.config.client_side_validation && opts[:'{{ name|attribute_name }}'] && !opts[:'{{ name|attribute_name }}'].all? { |item| allowable_values.include?(item) } fail ArgumentError, "invalid value for \"{{ name|attribute_name }}\", must include one of #{allowable_values}" end {%- else %} if @api_client.config.client_side_validation && opts[:'{{ name|attribute_name }}'] && !allowable_values.include?(opts[:'{{ name|attribute_name }}']) fail ArgumentError, "invalid value for \"{{ name|attribute_name }}\", must be one of #{allowable_values}" end {%- endif %} {%- endif %} {%- endif %} {%- if schema.maxLength is defined %} if @api_client.config.client_side_validation && {% if not parameter.required %}!opts[:'{{ name|attribute_name }}'].nil? && {% endif %}{% if parameter.required %}{{ name|attribute_name }}{% else %}opts[:'{{ name|attribute_name }}']{% endif %}.to_s.length > {{ schema.maxLength }} fail ArgumentError, 'invalid value for "{% if parameter.required %}{{ name|attribute_name }}{% else %}opts[:"{{ name|attribute_name }}"]{% endif %}" when calling {{classname}}.{{ operation.operationId|snake_case }}, the character length must be smaller than or equal to {{ schema.maxLength }}.' end {%- endif %} {%- if schema.minLength is defined %} if @api_client.config.client_side_validation && {% if not parameter.required %}!opts[:'{{ name|attribute_name }}'].nil? && {% endif %}{% if parameter.required %}{{ name|attribute_name }}{% else %}opts[:'{{ name|attribute_name }}']{% endif %}.to_s.length < {{ schema.minLength }} fail ArgumentError, 'invalid value for "{% if parameter.required %}{{ name|attribute_name }}{% else %}opts[:"{{ name|attribute_name }}"]{% endif %}" when calling {{classname}}.{{ operation.operationId|snake_case }}, the character length must be great than or equal to {{ schema.minLength }}.' end {%- endif %} {%- if schema.maximum is defined %} if @api_client.config.client_side_validation && {% if not parameter.required %}!opts[:'{{ name|attribute_name }}'].nil? && {% endif %}{% if parameter.required %}{{ name|attribute_name }}{% else %}opts[:'{{ name|attribute_name }}']{% endif %} >{% if parameter.exclusiveMaximum %}={% endif %} {{ schema.maximum }} fail ArgumentError, 'invalid value for "{% if parameter.required %}{{ name|attribute_name }}{% else %}opts[:"{{ name|attribute_name }}"]{% endif %}" when calling {{classname}}.{{ operation.operationId|snake_case }}, must be smaller than {% if not parameter.exclusiveMaximum %}or equal to {% endif %}{{ schema.maximum }}.' end {%- endif %} {%- if schema.minimum is defined %} if @api_client.config.client_side_validation && {% if not parameter.required %}!opts[:'{{ name|attribute_name }}'].nil? && {% endif %}{% if parameter.required %}{{ name|attribute_name }}{% else %}opts[:'{{ name|attribute_name }}']{% endif %} <{% if parameter.exclusiveMinimum %}={% endif %} {{ schema.minimum }} fail ArgumentError, 'invalid value for "{% if parameter.required %}{{ name|attribute_name }}{% else %}opts[:"{{ name|attribute_name }}"]{% endif %}" when calling {{classname}}.{{ operation.operationId|snake_case }}, must be greater than {% if not parameter.exclusiveMinimum %}or equal to {% endif %}{{ schema.minimum }}.' end {%- endif %} {%- if schema.pattern is defined %} pattern = Regexp.new({{ schema.pattern }}) if @api_client.config.client_side_validation && {% if not parameter.required %}!opts[:'{{ name|attribute_name }}'].nil? && {% endif %}{% if parameter.required %}{{ name|attribute_name }}{% else %}opts[:'{{ name|attribute_name }}']{% endif %} !~ pattern fail ArgumentError, "invalid value for '{% if parameter.required %}{{ name|attribute_name }}{% else %}opts[:\"{{ name|attribute_name }}\"]{% endif %}' when calling {{classname}}.{{ operation.operationId|snake_case }}, must conform to the pattern #{pattern}." end {%- endif %} {%- if schema.maxItems is defined %} if @api_client.config.client_side_validation && {% if not parameter.required %}!opts[:'{{ name|attribute_name }}'].nil? && {% endif %}{% if parameter.required %}{{ name|attribute_name }}{% else %}opts[:'{{ name|attribute_name }}']{% endif %}.length > {{ schema.maxItems }} fail ArgumentError, 'invalid value for "{% if parameter.required %}{{ name|attribute_name }}{% else %}opts[:"{{ name|attribute_name }}"]{% endif %}" when calling {{classname}}.{{ operation.operationId|snake_case }}, number of items must be less than or equal to {{ schema.maxItems }}.' end {%- endif %} {%- if schema.minItems is defined %} if @api_client.config.client_side_validation && {% if not parameter.required %}!opts[:'{{ name|attribute_name }}'].nil? && {% endif %}{% if parameter.required %}{{ name|attribute_name }}{% else %}opts[:'{{ name|attribute_name }}']{% endif %}.length < {{ schema.minItems }} fail ArgumentError, 'invalid value for "{% if parameter.required %}{{ name|attribute_name }}{% else %}opts[:"{{ name|attribute_name }}"]{% endif %}" when calling {{classname}}.{{ operation.operationId|snake_case }}, number of items must be greater than or equal to {{ schema.minItems }}.' end {%- endif %} {%- endfor %} # resource path local_var_path = '{{ path }}'{% for parameter in operation.parameters if parameter.in == "path" %}.sub('{{ '{' }}{{ parameter.name }}{{ '}'}}', CGI.escape({{ parameter.name }}.to_s).gsub('%2F', '/')){% endfor %} # query parameters query_params = opts[:query_params] || {} {%- for name, parameter in operation|parameters if parameter.in == "query" and parameter.required %} {%- set collection = parameter|collection_format %} query_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 == "query" and not parameter.required %} {%- set collection = parameter|collection_format %} query_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 %} # header parameters header_params = opts[:header_params] || {} {%- if operation.responses %} # HTTP header 'Accept' (if needed) header_params['Accept'] = @api_client.select_header_accept([{% for mediaType in operation|accept_headers %}'{{ mediaType }}'{%- if not loop.last %}, {% endif %}{% endfor %}]) {%- endif %} {%- if operation.requestBody %} # HTTP header 'Content-Type' 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 %} {%- 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 %} # form parameters form_params = opts[:form_params] || {} {%- for name, parameter in operation|parameters if parameter.in == "form" and parameter.required %} {%- set collection = parameter|collection_format %} form_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 == "form" and not parameter.required %} {%- set collection = parameter|collection_format %} form_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 %} # http body (model) post_body = opts[:debug_body]{% if operation.requestBody %} || @api_client.object_to_http_body( {%- if operation.requestBody.required -%} {{ operation.get("x-codegen-request-body-name", "body") }} {%- else -%} opts[:'{{ operation.get("x-codegen-request-body-name", "body") }}'] {%- endif -%} ){% endif %} # return_type return_type = opts[:debug_return_type]{% if returnType %} || '{{ returnType }}'{% endif %} # auth_names {%- set authMethods = operation.security if "security" in operation else openapi.security %} auth_names = opts[:debug_auth_names] || [{% for auth in (authMethods or []) %}:{{ ", :".join(auth.keys()) }}{%- if not loop.last %}, {% endif %}{% endfor %}] new_options = opts.merge( :operation => :{{ operation.operationId|snake_case }}, :header_params => header_params, :query_params => query_params, :form_params => form_params, :body => post_body, :auth_names => auth_names, :return_type => return_type ) 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 #}