All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ruby-client.api.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
=begin
{{> api_info}}
=end

require 'cgi'

module {{moduleName}}
{{#operations}}
  class {{classname}}
    attr_accessor :api_client

    def initialize(api_client = ApiClient.default)
      @api_client = api_client
    end
{{#operation}}
    {{#summary}}
    # {{{.}}}
    {{/summary}}
    {{#notes}}
    # {{{.}}}
    {{/notes}}
{{#allParams}}{{#required}}    # @param {{paramName}} [{{{dataType}}}] {{description}}
{{/required}}{{/allParams}}    # @param [Hash] opts the optional parameters
{{#allParams}}{{^required}}    # @option opts [{{{dataType}}}] :{{paramName}} {{description}}{{#defaultValue}} (default to {{{.}}}){{/defaultValue}}
{{/required}}{{/allParams}}    # @return [{{{returnType}}}{{^returnType}}nil{{/returnType}}]
    def {{operationId}}({{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}opts = {})
      {{#returnType}}data, _status_code, _headers = {{/returnType}}{{operationId}}_with_http_info({{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}opts)
      {{#returnType}}data{{/returnType}}{{^returnType}}nil{{/returnType}}
    end

    {{#summary}}
    # {{.}}
    {{/summary}}
    {{#notes}}
    # {{.}}
    {{/notes}}
{{#allParams}}{{#required}}    # @param {{paramName}} [{{{dataType}}}] {{description}}
{{/required}}{{/allParams}}    # @param [Hash] opts the optional parameters
{{#allParams}}{{^required}}    # @option opts [{{{dataType}}}] :{{paramName}} {{description}}
{{/required}}{{/allParams}}    # @return [Array<({{{returnType}}}{{^returnType}}nil{{/returnType}}, Integer, Hash)>] {{#returnType}}{{{.}}} data{{/returnType}}{{^returnType}}nil{{/returnType}}, response status code and response headers
    def {{operationId}}_with_http_info({{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}opts = {})
      if @api_client.config.debugging
        @api_client.config.logger.debug 'Calling API: {{classname}}.{{operationId}} ...'
      end
      {{#allParams}}
      {{^isNullable}}
      {{#required}}
      # verify the required parameter '{{paramName}}' is set
      if @api_client.config.client_side_validation && {{{paramName}}}.nil?
        fail ArgumentError, "Missing the required parameter '{{paramName}}' when calling {{classname}}.{{operationId}}"
      end
      {{#isEnum}}
      {{^isContainer}}
      # verify enum value
      allowable_values = [{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}]
      if @api_client.config.client_side_validation && !allowable_values.include?({{{paramName}}})
        fail ArgumentError, "invalid value for \"{{{paramName}}}\", must be one of #{allowable_values}"
      end
      {{/isContainer}}
      {{/isEnum}}
      {{/required}}
      {{/isNullable}}
      {{^required}}
      {{#isEnum}}
      {{#collectionFormat}}
      allowable_values = [{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}]
      if @api_client.config.client_side_validation && opts[:'{{{paramName}}}'] && !opts[:'{{{paramName}}}'].all? { |item| allowable_values.include?(item) }
        fail ArgumentError, "invalid value for \"{{{paramName}}}\", must include one of #{allowable_values}"
      end
      {{/collectionFormat}}
      {{^collectionFormat}}
      allowable_values = [{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}]
      if @api_client.config.client_side_validation && opts[:'{{{paramName}}}'] && !allowable_values.include?(opts[:'{{{paramName}}}'])
        fail ArgumentError, "invalid value for \"{{{paramName}}}\", must be one of #{allowable_values}"
      end
      {{/collectionFormat}}
      {{/isEnum}}
      {{/required}}
      {{#hasValidation}}
      {{#maxLength}}
      if @api_client.config.client_side_validation && {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}.to_s.length > {{{maxLength}}}
        fail ArgumentError, 'invalid value for "{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:"{{{paramName}}}"]{{/required}}" when calling {{classname}}.{{operationId}}, the character length must be smaller than or equal to {{{maxLength}}}.'
      end

      {{/maxLength}}
      {{#minLength}}
      if @api_client.config.client_side_validation && {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}.to_s.length < {{{minLength}}}
        fail ArgumentError, 'invalid value for "{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:"{{{paramName}}}"]{{/required}}" when calling {{classname}}.{{operationId}}, the character length must be great than or equal to {{{minLength}}}.'
      end

      {{/minLength}}
      {{#maximum}}
      if @api_client.config.client_side_validation && {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{{maximum}}}
        fail ArgumentError, 'invalid value for "{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:"{{{paramName}}}"]{{/required}}" when calling {{classname}}.{{operationId}}, must be smaller than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{{maximum}}}.'
      end

      {{/maximum}}
      {{#minimum}}
      if @api_client.config.client_side_validation && {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{{minimum}}}
        fail ArgumentError, 'invalid value for "{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:"{{{paramName}}}"]{{/required}}" when calling {{classname}}.{{operationId}}, must be greater than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{{minimum}}}.'
      end

      {{/minimum}}
      {{#pattern}}
      pattern = Regexp.new({{{pattern}}})
      if @api_client.config.client_side_validation && {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}} !~ pattern
        fail ArgumentError, "invalid value for '{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:\"{{{paramName}}}\"]{{/required}}' when calling {{classname}}.{{operationId}}, must conform to the pattern #{pattern}."
      end

      {{/pattern}}
      {{#maxItems}}
      if @api_client.config.client_side_validation && {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}.length > {{{maxItems}}}
        fail ArgumentError, 'invalid value for "{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:"{{{paramName}}}"]{{/required}}" when calling {{classname}}.{{operationId}}, number of items must be less than or equal to {{{maxItems}}}.'
      end

      {{/maxItems}}
      {{#minItems}}
      if @api_client.config.client_side_validation && {{^required}}!opts[:'{{{paramName}}}'].nil? && {{/required}}{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}.length < {{{minItems}}}
        fail ArgumentError, 'invalid value for "{{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:"{{{paramName}}}"]{{/required}}" when calling {{classname}}.{{operationId}}, number of items must be greater than or equal to {{{minItems}}}.'
      end

      {{/minItems}}
      {{/hasValidation}}
      {{/allParams}}
      # resource path
      local_var_path = '{{{path}}}'{{#pathParams}}.sub('{' + '{{baseName}}' + '}', CGI.escape({{paramName}}.to_s){{^strictSpecBehavior}}.gsub('%2F', '/'){{/strictSpecBehavior}}){{/pathParams}}

      # query parameters
      query_params = opts[:query_params] || {}
      {{#queryParams}}
      {{#required}}
      query_params[:'{{{baseName}}}'] = {{#collectionFormat}}@api_client.build_collection_param({{{paramName}}}, :{{{collectionFormat}}}){{/collectionFormat}}{{^collectionFormat}}{{{paramName}}}{{/collectionFormat}}
      {{/required}}
      {{/queryParams}}
      {{#queryParams}}
      {{^required}}
      query_params[:'{{{baseName}}}'] = {{#collectionFormat}}@api_client.build_collection_param(opts[:'{{{paramName}}}'], :{{{collectionFormat}}}){{/collectionFormat}}{{^collectionFormat}}opts[:'{{{paramName}}}']{{/collectionFormat}} if !opts[:'{{{paramName}}}'].nil?
      {{/required}}
      {{/queryParams}}

      # header parameters
      header_params = opts[:header_params] || {}
      {{#hasProduces}}
      # HTTP header 'Accept' (if needed)
      header_params['Accept'] = @api_client.select_header_accept([{{#produces}}'{{{mediaType}}}'{{^-last}}, {{/-last}}{{/produces}}])
      {{/hasProduces}}
      {{#hasConsumes}}
      # HTTP header 'Content-Type'
      content_type = @api_client.select_header_content_type([{{#consumes}}'{{{mediaType}}}'{{^-last}}, {{/-last}}{{/consumes}}])
      if !content_type.nil?
          header_params['Content-Type'] = content_type
      end
      {{/hasConsumes}}
      {{#headerParams}}
      {{#required}}
      header_params[:'{{{baseName}}}'] = {{#collectionFormat}}@api_client.build_collection_param({{{paramName}}}, :{{{collectionFormat}}}){{/collectionFormat}}{{^collectionFormat}}{{{paramName}}}{{/collectionFormat}}
      {{/required}}
      {{/headerParams}}
      {{#headerParams}}
      {{^required}}
      header_params[:'{{{baseName}}}'] = {{#collectionFormat}}@api_client.build_collection_param(opts[:'{{{paramName}}}'], :{{{collectionFormat}}}){{/collectionFormat}}{{^collectionFormat}}opts[:'{{{paramName}}}']{{/collectionFormat}} if !opts[:'{{{paramName}}}'].nil?
      {{/required}}
      {{/headerParams}}

      # form parameters
      form_params = opts[:form_params] || {}
      {{#formParams}}
      {{#required}}
      form_params['{{baseName}}'] = {{#collectionFormat}}@api_client.build_collection_param({{{paramName}}}, :{{{collectionFormat}}}){{/collectionFormat}}{{^collectionFormat}}{{{paramName}}}{{/collectionFormat}}
      {{/required}}
      {{/formParams}}
      {{#formParams}}
      {{^required}}
      form_params['{{baseName}}'] = {{#collectionFormat}}@api_client.build_collection_param(opts[:'{{{paramName}}}'], :{{{collectionFormat}}}){{/collectionFormat}}{{^collectionFormat}}opts[:'{{{paramName}}}']{{/collectionFormat}} if !opts[:'{{paramName}}'].nil?
      {{/required}}
      {{/formParams}}

      # http body (model)
      post_body = opts[:debug_body]{{#bodyParam}} || @api_client.object_to_http_body({{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}){{/bodyParam}}

      # return_type
      return_type = opts[:debug_return_type]{{#returnType}} || '{{{.}}}'{{/returnType}}

      # auth_names
      auth_names = opts[:debug_auth_names] || [{{#authMethods}}'{{name}}'{{^-last}}, {{/-last}}{{/authMethods}}]

      new_options = opts.merge(
        :operation => :"{{classname}}.{{operationId}}",
        :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(:{{httpMethod}}, local_var_path, new_options)
      if @api_client.config.debugging
        @api_client.config.logger.debug "API called: {{classname}}#{{operationId}}\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
      end
      return data, status_code, headers
    end
{{^-last}}

{{/-last}}
{{/operation}}
  end
{{/operations}}
end




© 2015 - 2024 Weber Informatics LLC | Privacy Policy