mirror of
https://github.com/valitydev/openapi-generator.git
synced 2024-11-08 03:18:53 +00:00
Add configuration object in ruby client
This commit is contained in:
parent
7af5db3565
commit
a481db7486
@ -110,6 +110,7 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
String gemFolder = libFolder + File.separator + gemName;
|
||||
supportingFiles.add(new SupportingFile("api_client.mustache", gemFolder, "api_client.rb"));
|
||||
supportingFiles.add(new SupportingFile("api_error.mustache", gemFolder, "api_error.rb"));
|
||||
supportingFiles.add(new SupportingFile("configuration.mustache", gemFolder, "configuration.rb"));
|
||||
supportingFiles.add(new SupportingFile("version.mustache", gemFolder, "version.rb"));
|
||||
String modelFolder = gemFolder + File.separator + modelPackage.replace("/", File.separator);
|
||||
supportingFiles.add(new SupportingFile("base_object.mustache", modelFolder, "base_object.rb"));
|
||||
|
@ -6,9 +6,8 @@ module {{moduleName}}
|
||||
attr_accessor :api_client
|
||||
|
||||
def initialize(api_client = nil)
|
||||
@api_client = api_client || ApiClient.default
|
||||
@api_client = api_client || Configuration.api_client
|
||||
end
|
||||
|
||||
{{#operation}}
|
||||
{{newline}}
|
||||
# {{summary}}
|
||||
@ -18,8 +17,8 @@ module {{moduleName}}
|
||||
{{#allParams}}{{^required}} # @option opts [{{{dataType}}}] :{{paramName}} {{description}}
|
||||
{{/required}}{{/allParams}} # @return [{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}nil{{/returnType}}]
|
||||
def {{nickname}}({{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}opts = {})
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "Calling API: {{classname}}#{{nickname}} ..."
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "Calling API: {{classname}}#{{nickname}} ..."
|
||||
end
|
||||
{{#allParams}}{{#required}}
|
||||
# verify the required parameter '{{paramName}}' is set
|
||||
@ -71,8 +70,8 @@ module {{moduleName}}
|
||||
:body => post_body,
|
||||
:auth_names => auth_names,
|
||||
:return_type => '{{{returnType}}}')
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "API called: {{classname}}#{{nickname}}. Result: #{result.inspect}"
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "API called: {{classname}}#{{nickname}}. Result: #{result.inspect}"
|
||||
end
|
||||
return result{{/returnType}}{{^returnType}}@api_client.call_api(:{{httpMethod}}, path,
|
||||
:header_params => header_params,
|
||||
@ -80,8 +79,8 @@ module {{moduleName}}
|
||||
:form_params => form_params,
|
||||
:body => post_body,
|
||||
:auth_names => auth_names)
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "API called: {{classname}}#{{nickname}}"
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "API called: {{classname}}#{{nickname}}"
|
||||
end
|
||||
return nil{{/returnType}}
|
||||
end
|
||||
@ -89,3 +88,7 @@ module {{moduleName}}
|
||||
end
|
||||
{{/operations}}
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -7,69 +7,8 @@ require 'uri'
|
||||
|
||||
module {{moduleName}}
|
||||
class ApiClient
|
||||
attr_accessor :scheme, :host, :base_path, :user_agent
|
||||
|
||||
# Defines the username used with HTTP basic authentication.
|
||||
#
|
||||
# @return [String]
|
||||
attr_accessor :username
|
||||
|
||||
# Defines the password used with HTTP basic authentication.
|
||||
#
|
||||
# @return [String]
|
||||
attr_accessor :password
|
||||
|
||||
# Defines API keys used with API Key authentications.
|
||||
#
|
||||
# @return [Hash] key: parameter name, value: parameter value (API key)
|
||||
#
|
||||
# @example parameter name is "api_key", API key is "xxx" (e.g. "api_key=xxx" in query string)
|
||||
# config.api_key['api_key'] = 'xxx'
|
||||
attr_accessor :api_key
|
||||
|
||||
# Defines API key prefixes used with API Key authentications.
|
||||
#
|
||||
# @return [Hash] key: parameter name, value: API key prefix
|
||||
#
|
||||
# @example parameter name is "Authorization", API key prefix is "Token" (e.g. "Authorization: Token xxx" in headers)
|
||||
# config.api_key_prefix['api_key'] = 'Token'
|
||||
attr_accessor :api_key_prefix
|
||||
|
||||
# Set this to false to skip verifying SSL certificate when calling API from https server.
|
||||
# Default to true.
|
||||
#
|
||||
# @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
|
||||
#
|
||||
# @return [true, false]
|
||||
attr_accessor :verify_ssl
|
||||
|
||||
# Set this to customize the certificate file to verify the peer.
|
||||
#
|
||||
# @return [String] the path to the certificate file
|
||||
#
|
||||
# @see The `cainfo` option of Typhoeus, `--cert` option of libcurl. Related source code:
|
||||
# https://github.com/typhoeus/typhoeus/blob/master/lib/typhoeus/easy_factory.rb#L145
|
||||
attr_accessor :ssl_ca_cert
|
||||
|
||||
# Set this to enable/disable debugging. When enabled (set to true), HTTP request/response
|
||||
# details will be logged with `logger.debug` (see the `logger` attribute).
|
||||
# Default to false.
|
||||
#
|
||||
# @return [true, false]
|
||||
attr_accessor :debugging
|
||||
|
||||
# Defines the logger used for debugging.
|
||||
# Default to `Rails.logger` (when in Rails) or logging to STDOUT.
|
||||
#
|
||||
# @return [#debug]
|
||||
attr_accessor :logger
|
||||
|
||||
# Defines the temporary folder to store downloaded files
|
||||
# (for API endpoints that have file response).
|
||||
# Default to use `Tempfile`.
|
||||
#
|
||||
# @return [String]
|
||||
attr_accessor :temp_folder_path
|
||||
attr_accessor :host
|
||||
|
||||
# Defines the headers to be used in HTTP requests of all API calls by default.
|
||||
#
|
||||
@ -79,59 +18,14 @@ module {{moduleName}}
|
||||
# Stores the HTTP response from the last API call using this API client.
|
||||
attr_accessor :last_response
|
||||
|
||||
# The constructor accepts a optional block to configure the API client.
|
||||
#
|
||||
# @example
|
||||
# {{moduleName}}::ApiClient.new do |client|
|
||||
# client.api_key['api_key'] = 'your key' # api key authentication
|
||||
# client.username = 'your username' # username for http basic authentication
|
||||
# client.password = 'your password' # password for http basic authentication
|
||||
# end
|
||||
def initialize(&block)
|
||||
def initialize(host = nil)
|
||||
@host = host || Configuration.base_url
|
||||
@format = 'json'
|
||||
@scheme = '{{scheme}}'
|
||||
@host = '{{host}}'
|
||||
@base_path = '{{contextPath}}'
|
||||
@user_agent = "ruby-swagger-#{VERSION}"
|
||||
@inject_format = false
|
||||
@force_ending_format = false
|
||||
|
||||
@default_headers = {
|
||||
'Content-Type' => "application/#{@format.downcase}",
|
||||
'User-Agent' => @user_agent
|
||||
}
|
||||
|
||||
# keys for API key authentication (param-name => api-key)
|
||||
@api_key = {}
|
||||
@api_key_prefix = {}
|
||||
|
||||
@verify_ssl = true
|
||||
|
||||
@debugging = false
|
||||
@logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
|
||||
|
||||
configure(&block)
|
||||
end
|
||||
|
||||
# Default API client.
|
||||
def self.default
|
||||
@@default ||= ApiClient.new
|
||||
end
|
||||
|
||||
# Call this method to modify defaults in your initializers.
|
||||
def configure
|
||||
yield(self) if block_given?
|
||||
|
||||
# remove :// from scheme
|
||||
@scheme.sub!(/:\/\//, '')
|
||||
|
||||
# remove http(s):// and anything after a slash
|
||||
@host.sub!(/https?:\/\//, '')
|
||||
@host = @host.split('/').first
|
||||
|
||||
# Add leading and trailing slashes to base_path
|
||||
@base_path = "/#{@base_path}".gsub(/\/+/, '/')
|
||||
@base_path = "" if @base_path == "/"
|
||||
end
|
||||
|
||||
def call_api(http_method, path, opts = {})
|
||||
@ -141,8 +35,8 @@ module {{moduleName}}
|
||||
# record as last response
|
||||
@last_response = response
|
||||
|
||||
if debugging
|
||||
logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
|
||||
end
|
||||
|
||||
unless response.success?
|
||||
@ -153,9 +47,9 @@ module {{moduleName}}
|
||||
end
|
||||
|
||||
if opts[:return_type]
|
||||
return deserialize(response, opts[:return_type])
|
||||
deserialize(response, opts[:return_type])
|
||||
else
|
||||
return nil
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
@ -183,8 +77,8 @@ module {{moduleName}}
|
||||
if [:post, :patch, :put, :delete].include?(http_method)
|
||||
req_body = build_request_body(header_params, form_params, opts[:body])
|
||||
req_opts.update :body => req_body
|
||||
if debugging
|
||||
logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
|
||||
end
|
||||
end
|
||||
|
||||
@ -279,13 +173,13 @@ module {{moduleName}}
|
||||
|
||||
File.open(path, 'w') { |file| file.write(response.body) }
|
||||
logger.info "File written to #{path}. Please move the file to a proper folder for further processing and delete the temp afterwards"
|
||||
return File.new(path)
|
||||
File.new(path)
|
||||
end
|
||||
|
||||
def build_request_url(path)
|
||||
url = [host, base_path, path].join('/').gsub(/\/+/, '/')
|
||||
url = "#{scheme}://#{url}"
|
||||
URI.encode(url)
|
||||
# Add leading and trailing slashes to path
|
||||
path = "/#{path}".gsub(/\/+/, '/')
|
||||
URI.encode(host + path)
|
||||
end
|
||||
|
||||
def build_request_body(header_params, form_params, body)
|
||||
@ -300,35 +194,22 @@ module {{moduleName}}
|
||||
else
|
||||
data = nil
|
||||
end
|
||||
return data
|
||||
data
|
||||
end
|
||||
|
||||
{{#hasAuthMethods}}
|
||||
# Update hearder and query params based on authentication settings.
|
||||
def update_params_for_auth!(header_params, query_params, auth_names)
|
||||
return unless auth_names
|
||||
auth_names.each do |auth_name|
|
||||
case auth_name
|
||||
{{#authMethods}}when '{{name}}'
|
||||
{{#isApiKey}}{{#isKeyInHeader}}header_params ||= {}
|
||||
header_params['{{keyParamName}}'] = get_api_key_with_prefix('{{keyParamName}}'){{/isKeyInHeader}}{{#isKeyInQuery}}query_params['{{keyParamName}}'] = get_api_key_with_prefix('{{keyParamName}}'){{/isKeyInQuery}}{{/isApiKey}}{{#isBasic}}http_auth_header = 'Basic ' + ["#{@username}:#{@password}"].pack('m').delete("\r\n")
|
||||
header_params['Authorization'] = http_auth_header{{/isBasic}}{{#isOAuth}}# TODO: support oauth{{/isOAuth}}
|
||||
{{/authMethods}}
|
||||
Array(auth_names).each do |auth_name|
|
||||
auth_setting = Configuration.auth_settings[auth_name]
|
||||
next unless auth_setting
|
||||
case auth_setting[:in]
|
||||
when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
|
||||
when 'query' then query_params[auth_setting[:key]] = auth_settings[:value]
|
||||
else fail ArgumentError, 'Authentication token must be in `query` of `header`'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Get API key (with prefix if set).
|
||||
# @param [String] param_name the parameter name of API key auth
|
||||
def get_api_key_with_prefix(param_name)
|
||||
if @api_key_prefix[param_name]
|
||||
"#{@api_key_prefix[param_name]} #{@api_key[param_name]}"
|
||||
else
|
||||
@api_key[param_name]
|
||||
end
|
||||
end
|
||||
{{/hasAuthMethods}}
|
||||
|
||||
def user_agent=(user_agent)
|
||||
@user_agent = user_agent
|
||||
@default_headers['User-Agent'] = @user_agent
|
||||
|
@ -0,0 +1,177 @@
|
||||
require 'uri'
|
||||
require 'singleton'
|
||||
|
||||
module {{moduleName}}
|
||||
class Configuration
|
||||
|
||||
include Singleton
|
||||
|
||||
# Default api client
|
||||
attr_accessor :api_client
|
||||
|
||||
# Defines url scheme
|
||||
attr_accessor :scheme
|
||||
|
||||
# Defines url host
|
||||
attr_accessor :host
|
||||
|
||||
# Defines url base path
|
||||
attr_accessor :base_path
|
||||
|
||||
# Defines API keys used with API Key authentications.
|
||||
#
|
||||
# @return [Hash] key: parameter name, value: parameter value (API key)
|
||||
#
|
||||
# @example parameter name is "api_key", API key is "xxx" (e.g. "api_key=xxx" in query string)
|
||||
# config.api_key['api_key'] = 'xxx'
|
||||
attr_accessor :api_key
|
||||
|
||||
# Defines API key prefixes used with API Key authentications.
|
||||
#
|
||||
# @return [Hash] key: parameter name, value: API key prefix
|
||||
#
|
||||
# @example parameter name is "Authorization", API key prefix is "Token" (e.g. "Authorization: Token xxx" in headers)
|
||||
# config.api_key_prefix['api_key'] = 'Token'
|
||||
attr_accessor :api_key_prefix
|
||||
|
||||
# Defines the username used with HTTP basic authentication.
|
||||
#
|
||||
# @return [String]
|
||||
attr_accessor :username
|
||||
|
||||
# Defines the password used with HTTP basic authentication.
|
||||
#
|
||||
# @return [String]
|
||||
attr_accessor :password
|
||||
|
||||
# Set this to enable/disable debugging. When enabled (set to true), HTTP request/response
|
||||
# details will be logged with `logger.debug` (see the `logger` attribute).
|
||||
# Default to false.
|
||||
#
|
||||
# @return [true, false]
|
||||
attr_accessor :debugging
|
||||
|
||||
# Defines the logger used for debugging.
|
||||
# Default to `Rails.logger` (when in Rails) or logging to STDOUT.
|
||||
#
|
||||
# @return [#debug]
|
||||
attr_accessor :logger
|
||||
|
||||
# Defines the temporary folder to store downloaded files
|
||||
# (for API endpoints that have file response).
|
||||
# Default to use `Tempfile`.
|
||||
#
|
||||
# @return [String]
|
||||
attr_accessor :temp_folder_path
|
||||
|
||||
# Set this to false to skip verifying SSL certificate when calling API from https server.
|
||||
# Default to true.
|
||||
#
|
||||
# @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
|
||||
#
|
||||
# @return [true, false]
|
||||
attr_accessor :verify_ssl
|
||||
|
||||
# Set this to customize the certificate file to verify the peer.
|
||||
#
|
||||
# @return [String] the path to the certificate file
|
||||
#
|
||||
# @see The `cainfo` option of Typhoeus, `--cert` option of libcurl. Related source code:
|
||||
# https://github.com/typhoeus/typhoeus/blob/master/lib/typhoeus/easy_factory.rb#L145
|
||||
attr_accessor :ssl_ca_cert
|
||||
|
||||
attr_accessor :inject_format
|
||||
|
||||
attr_accessor :force_ending_format
|
||||
|
||||
class << self
|
||||
def method_missing(method_name, *args, &block)
|
||||
config = Configuration.instance
|
||||
if config.respond_to?(method_name)
|
||||
config.send(method_name, *args, &block)
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def initialize
|
||||
@scheme = '{{scheme}}'
|
||||
@host = '{{host}}'
|
||||
@base_path = '{{contextPath}}'
|
||||
@api_key = {}
|
||||
@api_key_prefix = {}
|
||||
@verify_ssl = true
|
||||
@debugging = false
|
||||
@inject_format = false
|
||||
@force_ending_format = false
|
||||
@logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
|
||||
end
|
||||
|
||||
def api_client
|
||||
@api_client ||= ApiClient.new
|
||||
end
|
||||
|
||||
def scheme=(scheme)
|
||||
# remove :// from scheme
|
||||
@scheme = scheme.sub(/:\/\//, '')
|
||||
end
|
||||
|
||||
def host=(host)
|
||||
# remove http(s):// and anything after a slash
|
||||
@host = host.sub(/https?:\/\//, '').split('/').first
|
||||
end
|
||||
|
||||
def base_path=(base_path)
|
||||
# Add leading and trailing slashes to base_path
|
||||
@base_path = "/#{base_path}".gsub(/\/+/, '/')
|
||||
@base_path = "" if @base_path == "/"
|
||||
end
|
||||
|
||||
def base_url
|
||||
url = "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}"
|
||||
URI.encode(url)
|
||||
end
|
||||
|
||||
# Gets API key (with prefix if set).
|
||||
# @param [String] param_name the parameter name of API key auth
|
||||
def api_key_with_prefix(param_name)
|
||||
if @api_key_prefix[param_name]
|
||||
"#{@api_key_prefix[param_name]} #{@api_key[param_name]}"
|
||||
else
|
||||
@api_key[param_name]
|
||||
end
|
||||
end
|
||||
|
||||
# Gets Basic Auth token string
|
||||
def basic_auth_token
|
||||
'Basic ' + ["#{username}:#{password}"].pack('m').delete("\r\n")
|
||||
end
|
||||
|
||||
# Returns Auth Settings hash for api client.
|
||||
def auth_settings
|
||||
{
|
||||
{{#authMethods}}
|
||||
{{#isApiKey}}
|
||||
'{{name}}' =>
|
||||
{
|
||||
type: 'api_key',
|
||||
in: {{#isKeyInHeader}}'header'{{/isKeyInHeader}}{{#isKeyInQuery}}'query'{{/isKeyInQuery}},
|
||||
key: '{{keyParamName}}',
|
||||
value: api_key_with_prefix('{{keyParamName}}')
|
||||
},
|
||||
{{/isApiKey}}
|
||||
{{#isBasic}}
|
||||
'{{name}}' =>
|
||||
{
|
||||
type: 'basic',
|
||||
in: 'header',
|
||||
key: 'Authorization',
|
||||
value: basic_auth_token
|
||||
},
|
||||
{{/isBasic}}
|
||||
{{/authMethods}}
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
@ -2,6 +2,7 @@
|
||||
require '{{gemName}}/api_client'
|
||||
require '{{gemName}}/api_error'
|
||||
require '{{gemName}}/version'
|
||||
require '{{gemName}}/configuration'
|
||||
|
||||
# Models
|
||||
require '{{gemName}}/{{modelPackage}}/base_object'
|
||||
@ -17,4 +18,19 @@ require '{{importPath}}'
|
||||
{{/apiInfo}}
|
||||
|
||||
module {{moduleName}}
|
||||
class << self
|
||||
# Configure sdk using block.
|
||||
# {{moduleName}}.configure do |config|
|
||||
# config.username = "xxx"
|
||||
# config.password = "xxx"
|
||||
# end
|
||||
# If no block given, return the configuration singleton instance.
|
||||
def configure
|
||||
if block_given?
|
||||
yield Configuration.instance
|
||||
else
|
||||
Configuration.instance
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -2,6 +2,7 @@
|
||||
require 'petstore/api_client'
|
||||
require 'petstore/api_error'
|
||||
require 'petstore/version'
|
||||
require 'petstore/configuration'
|
||||
|
||||
# Models
|
||||
require 'petstore/models/base_object'
|
||||
@ -17,4 +18,19 @@ require 'petstore/api/pet_api'
|
||||
require 'petstore/api/store_api'
|
||||
|
||||
module Petstore
|
||||
class << self
|
||||
# Configure sdk using block.
|
||||
# Petstore.configure do |config|
|
||||
# config.username = "xxx"
|
||||
# config.password = "xxx"
|
||||
# end
|
||||
# If no block given, return the configuration singleton instance.
|
||||
def configure
|
||||
if block_given?
|
||||
yield Configuration.instance
|
||||
else
|
||||
Configuration.instance
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -5,18 +5,17 @@ module Petstore
|
||||
attr_accessor :api_client
|
||||
|
||||
def initialize(api_client = nil)
|
||||
@api_client = api_client || ApiClient.default
|
||||
@api_client = api_client || Configuration.api_client
|
||||
end
|
||||
|
||||
|
||||
# Update an existing pet
|
||||
#
|
||||
# @param [Hash] opts the optional parameters
|
||||
# @option opts [Pet] :body Pet object that needs to be added to the store
|
||||
# @return [nil]
|
||||
def update_pet(opts = {})
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "Calling API: PetApi#update_pet ..."
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "Calling API: PetApi#update_pet ..."
|
||||
end
|
||||
|
||||
# resource path
|
||||
@ -50,8 +49,8 @@ module Petstore
|
||||
:form_params => form_params,
|
||||
:body => post_body,
|
||||
:auth_names => auth_names)
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "API called: PetApi#update_pet"
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "API called: PetApi#update_pet"
|
||||
end
|
||||
return nil
|
||||
end
|
||||
@ -62,8 +61,8 @@ module Petstore
|
||||
# @option opts [Pet] :body Pet object that needs to be added to the store
|
||||
# @return [nil]
|
||||
def add_pet(opts = {})
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "Calling API: PetApi#add_pet ..."
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "Calling API: PetApi#add_pet ..."
|
||||
end
|
||||
|
||||
# resource path
|
||||
@ -97,8 +96,8 @@ module Petstore
|
||||
:form_params => form_params,
|
||||
:body => post_body,
|
||||
:auth_names => auth_names)
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "API called: PetApi#add_pet"
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "API called: PetApi#add_pet"
|
||||
end
|
||||
return nil
|
||||
end
|
||||
@ -109,8 +108,8 @@ module Petstore
|
||||
# @option opts [Array<String>] :status Status values that need to be considered for filter
|
||||
# @return [Array<Pet>]
|
||||
def find_pets_by_status(opts = {})
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "Calling API: PetApi#find_pets_by_status ..."
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "Calling API: PetApi#find_pets_by_status ..."
|
||||
end
|
||||
|
||||
# resource path
|
||||
@ -146,8 +145,8 @@ module Petstore
|
||||
:body => post_body,
|
||||
:auth_names => auth_names,
|
||||
:return_type => 'Array<Pet>')
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "API called: PetApi#find_pets_by_status. Result: #{result.inspect}"
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "API called: PetApi#find_pets_by_status. Result: #{result.inspect}"
|
||||
end
|
||||
return result
|
||||
end
|
||||
@ -158,8 +157,8 @@ module Petstore
|
||||
# @option opts [Array<String>] :tags Tags to filter by
|
||||
# @return [Array<Pet>]
|
||||
def find_pets_by_tags(opts = {})
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "Calling API: PetApi#find_pets_by_tags ..."
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "Calling API: PetApi#find_pets_by_tags ..."
|
||||
end
|
||||
|
||||
# resource path
|
||||
@ -195,8 +194,8 @@ module Petstore
|
||||
:body => post_body,
|
||||
:auth_names => auth_names,
|
||||
:return_type => 'Array<Pet>')
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "API called: PetApi#find_pets_by_tags. Result: #{result.inspect}"
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "API called: PetApi#find_pets_by_tags. Result: #{result.inspect}"
|
||||
end
|
||||
return result
|
||||
end
|
||||
@ -207,8 +206,8 @@ module Petstore
|
||||
# @param [Hash] opts the optional parameters
|
||||
# @return [Pet]
|
||||
def get_pet_by_id(pet_id, opts = {})
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "Calling API: PetApi#get_pet_by_id ..."
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "Calling API: PetApi#get_pet_by_id ..."
|
||||
end
|
||||
|
||||
# verify the required parameter 'pet_id' is set
|
||||
@ -246,8 +245,8 @@ module Petstore
|
||||
:body => post_body,
|
||||
:auth_names => auth_names,
|
||||
:return_type => 'Pet')
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "API called: PetApi#get_pet_by_id. Result: #{result.inspect}"
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "API called: PetApi#get_pet_by_id. Result: #{result.inspect}"
|
||||
end
|
||||
return result
|
||||
end
|
||||
@ -260,8 +259,8 @@ module Petstore
|
||||
# @option opts [String] :status Updated status of the pet
|
||||
# @return [nil]
|
||||
def update_pet_with_form(pet_id, opts = {})
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "Calling API: PetApi#update_pet_with_form ..."
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "Calling API: PetApi#update_pet_with_form ..."
|
||||
end
|
||||
|
||||
# verify the required parameter 'pet_id' is set
|
||||
@ -300,8 +299,8 @@ module Petstore
|
||||
:form_params => form_params,
|
||||
:body => post_body,
|
||||
:auth_names => auth_names)
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "API called: PetApi#update_pet_with_form"
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "API called: PetApi#update_pet_with_form"
|
||||
end
|
||||
return nil
|
||||
end
|
||||
@ -313,8 +312,8 @@ module Petstore
|
||||
# @option opts [String] :api_key
|
||||
# @return [nil]
|
||||
def delete_pet(pet_id, opts = {})
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "Calling API: PetApi#delete_pet ..."
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "Calling API: PetApi#delete_pet ..."
|
||||
end
|
||||
|
||||
# verify the required parameter 'pet_id' is set
|
||||
@ -352,8 +351,8 @@ module Petstore
|
||||
:form_params => form_params,
|
||||
:body => post_body,
|
||||
:auth_names => auth_names)
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "API called: PetApi#delete_pet"
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "API called: PetApi#delete_pet"
|
||||
end
|
||||
return nil
|
||||
end
|
||||
@ -366,8 +365,8 @@ module Petstore
|
||||
# @option opts [File] :file file to upload
|
||||
# @return [nil]
|
||||
def upload_file(pet_id, opts = {})
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "Calling API: PetApi#upload_file ..."
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "Calling API: PetApi#upload_file ..."
|
||||
end
|
||||
|
||||
# verify the required parameter 'pet_id' is set
|
||||
@ -406,10 +405,14 @@ module Petstore
|
||||
:form_params => form_params,
|
||||
:body => post_body,
|
||||
:auth_names => auth_names)
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "API called: PetApi#upload_file"
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "API called: PetApi#upload_file"
|
||||
end
|
||||
return nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -5,17 +5,16 @@ module Petstore
|
||||
attr_accessor :api_client
|
||||
|
||||
def initialize(api_client = nil)
|
||||
@api_client = api_client || ApiClient.default
|
||||
@api_client = api_client || Configuration.api_client
|
||||
end
|
||||
|
||||
|
||||
# Returns pet inventories by status
|
||||
# Returns a map of status codes to quantities
|
||||
# @param [Hash] opts the optional parameters
|
||||
# @return [Hash<String, Integer>]
|
||||
def get_inventory(opts = {})
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "Calling API: StoreApi#get_inventory ..."
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "Calling API: StoreApi#get_inventory ..."
|
||||
end
|
||||
|
||||
# resource path
|
||||
@ -50,8 +49,8 @@ module Petstore
|
||||
:body => post_body,
|
||||
:auth_names => auth_names,
|
||||
:return_type => 'Hash<String, Integer>')
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "API called: StoreApi#get_inventory. Result: #{result.inspect}"
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "API called: StoreApi#get_inventory. Result: #{result.inspect}"
|
||||
end
|
||||
return result
|
||||
end
|
||||
@ -62,8 +61,8 @@ module Petstore
|
||||
# @option opts [Order] :body order placed for purchasing the pet
|
||||
# @return [Order]
|
||||
def place_order(opts = {})
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "Calling API: StoreApi#place_order ..."
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "Calling API: StoreApi#place_order ..."
|
||||
end
|
||||
|
||||
# resource path
|
||||
@ -98,8 +97,8 @@ module Petstore
|
||||
:body => post_body,
|
||||
:auth_names => auth_names,
|
||||
:return_type => 'Order')
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "API called: StoreApi#place_order. Result: #{result.inspect}"
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "API called: StoreApi#place_order. Result: #{result.inspect}"
|
||||
end
|
||||
return result
|
||||
end
|
||||
@ -110,8 +109,8 @@ module Petstore
|
||||
# @param [Hash] opts the optional parameters
|
||||
# @return [Order]
|
||||
def get_order_by_id(order_id, opts = {})
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "Calling API: StoreApi#get_order_by_id ..."
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "Calling API: StoreApi#get_order_by_id ..."
|
||||
end
|
||||
|
||||
# verify the required parameter 'order_id' is set
|
||||
@ -149,8 +148,8 @@ module Petstore
|
||||
:body => post_body,
|
||||
:auth_names => auth_names,
|
||||
:return_type => 'Order')
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "API called: StoreApi#get_order_by_id. Result: #{result.inspect}"
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "API called: StoreApi#get_order_by_id. Result: #{result.inspect}"
|
||||
end
|
||||
return result
|
||||
end
|
||||
@ -161,8 +160,8 @@ module Petstore
|
||||
# @param [Hash] opts the optional parameters
|
||||
# @return [nil]
|
||||
def delete_order(order_id, opts = {})
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "Calling API: StoreApi#delete_order ..."
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "Calling API: StoreApi#delete_order ..."
|
||||
end
|
||||
|
||||
# verify the required parameter 'order_id' is set
|
||||
@ -199,10 +198,14 @@ module Petstore
|
||||
:form_params => form_params,
|
||||
:body => post_body,
|
||||
:auth_names => auth_names)
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "API called: StoreApi#delete_order"
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "API called: StoreApi#delete_order"
|
||||
end
|
||||
return nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -5,18 +5,17 @@ module Petstore
|
||||
attr_accessor :api_client
|
||||
|
||||
def initialize(api_client = nil)
|
||||
@api_client = api_client || ApiClient.default
|
||||
@api_client = api_client || Configuration.api_client
|
||||
end
|
||||
|
||||
|
||||
# Create user
|
||||
# This can only be done by the logged in user.
|
||||
# @param [Hash] opts the optional parameters
|
||||
# @option opts [User] :body Created user object
|
||||
# @return [nil]
|
||||
def create_user(opts = {})
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "Calling API: UserApi#create_user ..."
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "Calling API: UserApi#create_user ..."
|
||||
end
|
||||
|
||||
# resource path
|
||||
@ -50,8 +49,8 @@ module Petstore
|
||||
:form_params => form_params,
|
||||
:body => post_body,
|
||||
:auth_names => auth_names)
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "API called: UserApi#create_user"
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "API called: UserApi#create_user"
|
||||
end
|
||||
return nil
|
||||
end
|
||||
@ -62,8 +61,8 @@ module Petstore
|
||||
# @option opts [Array<User>] :body List of user object
|
||||
# @return [nil]
|
||||
def create_users_with_array_input(opts = {})
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "Calling API: UserApi#create_users_with_array_input ..."
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "Calling API: UserApi#create_users_with_array_input ..."
|
||||
end
|
||||
|
||||
# resource path
|
||||
@ -97,8 +96,8 @@ module Petstore
|
||||
:form_params => form_params,
|
||||
:body => post_body,
|
||||
:auth_names => auth_names)
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "API called: UserApi#create_users_with_array_input"
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "API called: UserApi#create_users_with_array_input"
|
||||
end
|
||||
return nil
|
||||
end
|
||||
@ -109,8 +108,8 @@ module Petstore
|
||||
# @option opts [Array<User>] :body List of user object
|
||||
# @return [nil]
|
||||
def create_users_with_list_input(opts = {})
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "Calling API: UserApi#create_users_with_list_input ..."
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "Calling API: UserApi#create_users_with_list_input ..."
|
||||
end
|
||||
|
||||
# resource path
|
||||
@ -144,8 +143,8 @@ module Petstore
|
||||
:form_params => form_params,
|
||||
:body => post_body,
|
||||
:auth_names => auth_names)
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "API called: UserApi#create_users_with_list_input"
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "API called: UserApi#create_users_with_list_input"
|
||||
end
|
||||
return nil
|
||||
end
|
||||
@ -157,8 +156,8 @@ module Petstore
|
||||
# @option opts [String] :password The password for login in clear text
|
||||
# @return [String]
|
||||
def login_user(opts = {})
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "Calling API: UserApi#login_user ..."
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "Calling API: UserApi#login_user ..."
|
||||
end
|
||||
|
||||
# resource path
|
||||
@ -195,8 +194,8 @@ module Petstore
|
||||
:body => post_body,
|
||||
:auth_names => auth_names,
|
||||
:return_type => 'String')
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "API called: UserApi#login_user. Result: #{result.inspect}"
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "API called: UserApi#login_user. Result: #{result.inspect}"
|
||||
end
|
||||
return result
|
||||
end
|
||||
@ -206,8 +205,8 @@ module Petstore
|
||||
# @param [Hash] opts the optional parameters
|
||||
# @return [nil]
|
||||
def logout_user(opts = {})
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "Calling API: UserApi#logout_user ..."
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "Calling API: UserApi#logout_user ..."
|
||||
end
|
||||
|
||||
# resource path
|
||||
@ -241,8 +240,8 @@ module Petstore
|
||||
:form_params => form_params,
|
||||
:body => post_body,
|
||||
:auth_names => auth_names)
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "API called: UserApi#logout_user"
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "API called: UserApi#logout_user"
|
||||
end
|
||||
return nil
|
||||
end
|
||||
@ -253,8 +252,8 @@ module Petstore
|
||||
# @param [Hash] opts the optional parameters
|
||||
# @return [User]
|
||||
def get_user_by_name(username, opts = {})
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "Calling API: UserApi#get_user_by_name ..."
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "Calling API: UserApi#get_user_by_name ..."
|
||||
end
|
||||
|
||||
# verify the required parameter 'username' is set
|
||||
@ -292,8 +291,8 @@ module Petstore
|
||||
:body => post_body,
|
||||
:auth_names => auth_names,
|
||||
:return_type => 'User')
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "API called: UserApi#get_user_by_name. Result: #{result.inspect}"
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "API called: UserApi#get_user_by_name. Result: #{result.inspect}"
|
||||
end
|
||||
return result
|
||||
end
|
||||
@ -305,8 +304,8 @@ module Petstore
|
||||
# @option opts [User] :body Updated user object
|
||||
# @return [nil]
|
||||
def update_user(username, opts = {})
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "Calling API: UserApi#update_user ..."
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "Calling API: UserApi#update_user ..."
|
||||
end
|
||||
|
||||
# verify the required parameter 'username' is set
|
||||
@ -343,8 +342,8 @@ module Petstore
|
||||
:form_params => form_params,
|
||||
:body => post_body,
|
||||
:auth_names => auth_names)
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "API called: UserApi#update_user"
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "API called: UserApi#update_user"
|
||||
end
|
||||
return nil
|
||||
end
|
||||
@ -355,8 +354,8 @@ module Petstore
|
||||
# @param [Hash] opts the optional parameters
|
||||
# @return [nil]
|
||||
def delete_user(username, opts = {})
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "Calling API: UserApi#delete_user ..."
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "Calling API: UserApi#delete_user ..."
|
||||
end
|
||||
|
||||
# verify the required parameter 'username' is set
|
||||
@ -393,10 +392,14 @@ module Petstore
|
||||
:form_params => form_params,
|
||||
:body => post_body,
|
||||
:auth_names => auth_names)
|
||||
if @api_client.debugging
|
||||
@api_client.logger.debug "API called: UserApi#delete_user"
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "API called: UserApi#delete_user"
|
||||
end
|
||||
return nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -7,69 +7,8 @@ require 'uri'
|
||||
|
||||
module Petstore
|
||||
class ApiClient
|
||||
attr_accessor :scheme, :host, :base_path, :user_agent
|
||||
|
||||
# Defines the username used with HTTP basic authentication.
|
||||
#
|
||||
# @return [String]
|
||||
attr_accessor :username
|
||||
|
||||
# Defines the password used with HTTP basic authentication.
|
||||
#
|
||||
# @return [String]
|
||||
attr_accessor :password
|
||||
|
||||
# Defines API keys used with API Key authentications.
|
||||
#
|
||||
# @return [Hash] key: parameter name, value: parameter value (API key)
|
||||
#
|
||||
# @example parameter name is "api_key", API key is "xxx" (e.g. "api_key=xxx" in query string)
|
||||
# config.api_key['api_key'] = 'xxx'
|
||||
attr_accessor :api_key
|
||||
|
||||
# Defines API key prefixes used with API Key authentications.
|
||||
#
|
||||
# @return [Hash] key: parameter name, value: API key prefix
|
||||
#
|
||||
# @example parameter name is "Authorization", API key prefix is "Token" (e.g. "Authorization: Token xxx" in headers)
|
||||
# config.api_key_prefix['api_key'] = 'Token'
|
||||
attr_accessor :api_key_prefix
|
||||
|
||||
# Set this to false to skip verifying SSL certificate when calling API from https server.
|
||||
# Default to true.
|
||||
#
|
||||
# @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
|
||||
#
|
||||
# @return [true, false]
|
||||
attr_accessor :verify_ssl
|
||||
|
||||
# Set this to customize the certificate file to verify the peer.
|
||||
#
|
||||
# @return [String] the path to the certificate file
|
||||
#
|
||||
# @see The `cainfo` option of Typhoeus, `--cert` option of libcurl. Related source code:
|
||||
# https://github.com/typhoeus/typhoeus/blob/master/lib/typhoeus/easy_factory.rb#L145
|
||||
attr_accessor :ssl_ca_cert
|
||||
|
||||
# Set this to enable/disable debugging. When enabled (set to true), HTTP request/response
|
||||
# details will be logged with `logger.debug` (see the `logger` attribute).
|
||||
# Default to false.
|
||||
#
|
||||
# @return [true, false]
|
||||
attr_accessor :debugging
|
||||
|
||||
# Defines the logger used for debugging.
|
||||
# Default to `Rails.logger` (when in Rails) or logging to STDOUT.
|
||||
#
|
||||
# @return [#debug]
|
||||
attr_accessor :logger
|
||||
|
||||
# Defines the temporary folder to store downloaded files
|
||||
# (for API endpoints that have file response).
|
||||
# Default to use `Tempfile`.
|
||||
#
|
||||
# @return [String]
|
||||
attr_accessor :temp_folder_path
|
||||
attr_accessor :host
|
||||
|
||||
# Defines the headers to be used in HTTP requests of all API calls by default.
|
||||
#
|
||||
@ -79,59 +18,14 @@ module Petstore
|
||||
# Stores the HTTP response from the last API call using this API client.
|
||||
attr_accessor :last_response
|
||||
|
||||
# The constructor accepts a optional block to configure the API client.
|
||||
#
|
||||
# @example
|
||||
# Petstore::ApiClient.new do |client|
|
||||
# client.api_key['api_key'] = 'your key' # api key authentication
|
||||
# client.username = 'your username' # username for http basic authentication
|
||||
# client.password = 'your password' # password for http basic authentication
|
||||
# end
|
||||
def initialize(&block)
|
||||
def initialize(host = nil)
|
||||
@host = host || Configuration.base_url
|
||||
@format = 'json'
|
||||
@scheme = 'http'
|
||||
@host = 'petstore.swagger.io'
|
||||
@base_path = '/v2'
|
||||
@user_agent = "ruby-swagger-#{VERSION}"
|
||||
@inject_format = false
|
||||
@force_ending_format = false
|
||||
|
||||
@default_headers = {
|
||||
'Content-Type' => "application/#{@format.downcase}",
|
||||
'User-Agent' => @user_agent
|
||||
}
|
||||
|
||||
# keys for API key authentication (param-name => api-key)
|
||||
@api_key = {}
|
||||
@api_key_prefix = {}
|
||||
|
||||
@verify_ssl = true
|
||||
|
||||
@debugging = false
|
||||
@logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
|
||||
|
||||
configure(&block)
|
||||
end
|
||||
|
||||
# Default API client.
|
||||
def self.default
|
||||
@@default ||= ApiClient.new
|
||||
end
|
||||
|
||||
# Call this method to modify defaults in your initializers.
|
||||
def configure
|
||||
yield(self) if block_given?
|
||||
|
||||
# remove :// from scheme
|
||||
@scheme.sub!(/:\/\//, '')
|
||||
|
||||
# remove http(s):// and anything after a slash
|
||||
@host.sub!(/https?:\/\//, '')
|
||||
@host = @host.split('/').first
|
||||
|
||||
# Add leading and trailing slashes to base_path
|
||||
@base_path = "/#{@base_path}".gsub(/\/+/, '/')
|
||||
@base_path = "" if @base_path == "/"
|
||||
end
|
||||
|
||||
def call_api(http_method, path, opts = {})
|
||||
@ -141,8 +35,8 @@ module Petstore
|
||||
# record as last response
|
||||
@last_response = response
|
||||
|
||||
if debugging
|
||||
logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
|
||||
end
|
||||
|
||||
unless response.success?
|
||||
@ -153,9 +47,9 @@ module Petstore
|
||||
end
|
||||
|
||||
if opts[:return_type]
|
||||
return deserialize(response, opts[:return_type])
|
||||
deserialize(response, opts[:return_type])
|
||||
else
|
||||
return nil
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
@ -183,8 +77,8 @@ module Petstore
|
||||
if [:post, :patch, :put, :delete].include?(http_method)
|
||||
req_body = build_request_body(header_params, form_params, opts[:body])
|
||||
req_opts.update :body => req_body
|
||||
if debugging
|
||||
logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
|
||||
end
|
||||
end
|
||||
|
||||
@ -279,13 +173,13 @@ module Petstore
|
||||
|
||||
File.open(path, 'w') { |file| file.write(response.body) }
|
||||
logger.info "File written to #{path}. Please move the file to a proper folder for further processing and delete the temp afterwards"
|
||||
return File.new(path)
|
||||
File.new(path)
|
||||
end
|
||||
|
||||
def build_request_url(path)
|
||||
url = [host, base_path, path].join('/').gsub(/\/+/, '/')
|
||||
url = "#{scheme}://#{url}"
|
||||
URI.encode(url)
|
||||
# Add leading and trailing slashes to path
|
||||
path = "/#{path}".gsub(/\/+/, '/')
|
||||
URI.encode(host + path)
|
||||
end
|
||||
|
||||
def build_request_body(header_params, form_params, body)
|
||||
@ -300,36 +194,22 @@ module Petstore
|
||||
else
|
||||
data = nil
|
||||
end
|
||||
return data
|
||||
data
|
||||
end
|
||||
|
||||
|
||||
# Update hearder and query params based on authentication settings.
|
||||
def update_params_for_auth!(header_params, query_params, auth_names)
|
||||
return unless auth_names
|
||||
auth_names.each do |auth_name|
|
||||
case auth_name
|
||||
when 'api_key'
|
||||
header_params ||= {}
|
||||
header_params['api_key'] = get_api_key_with_prefix('api_key')
|
||||
when 'petstore_auth'
|
||||
# TODO: support oauth
|
||||
|
||||
Array(auth_names).each do |auth_name|
|
||||
auth_setting = Configuration.auth_settings[auth_name]
|
||||
next unless auth_setting
|
||||
case auth_setting[:in]
|
||||
when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
|
||||
when 'query' then query_params[auth_setting[:key]] = auth_settings[:value]
|
||||
else fail ArgumentError, 'Authentication token must be in `query` of `header`'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Get API key (with prefix if set).
|
||||
# @param [String] param_name the parameter name of API key auth
|
||||
def get_api_key_with_prefix(param_name)
|
||||
if @api_key_prefix[param_name]
|
||||
"#{@api_key_prefix[param_name]} #{@api_key[param_name]}"
|
||||
else
|
||||
@api_key[param_name]
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def user_agent=(user_agent)
|
||||
@user_agent = user_agent
|
||||
@default_headers['User-Agent'] = @user_agent
|
||||
|
164
samples/client/petstore/ruby/lib/petstore/configuration.rb
Normal file
164
samples/client/petstore/ruby/lib/petstore/configuration.rb
Normal file
@ -0,0 +1,164 @@
|
||||
require 'uri'
|
||||
require 'singleton'
|
||||
|
||||
module Petstore
|
||||
class Configuration
|
||||
|
||||
include Singleton
|
||||
|
||||
# Default api client
|
||||
attr_accessor :api_client
|
||||
|
||||
# Defines url scheme
|
||||
attr_accessor :scheme
|
||||
|
||||
# Defines url host
|
||||
attr_accessor :host
|
||||
|
||||
# Defines url base path
|
||||
attr_accessor :base_path
|
||||
|
||||
# Defines API keys used with API Key authentications.
|
||||
#
|
||||
# @return [Hash] key: parameter name, value: parameter value (API key)
|
||||
#
|
||||
# @example parameter name is "api_key", API key is "xxx" (e.g. "api_key=xxx" in query string)
|
||||
# config.api_key['api_key'] = 'xxx'
|
||||
attr_accessor :api_key
|
||||
|
||||
# Defines API key prefixes used with API Key authentications.
|
||||
#
|
||||
# @return [Hash] key: parameter name, value: API key prefix
|
||||
#
|
||||
# @example parameter name is "Authorization", API key prefix is "Token" (e.g. "Authorization: Token xxx" in headers)
|
||||
# config.api_key_prefix['api_key'] = 'Token'
|
||||
attr_accessor :api_key_prefix
|
||||
|
||||
# Defines the username used with HTTP basic authentication.
|
||||
#
|
||||
# @return [String]
|
||||
attr_accessor :username
|
||||
|
||||
# Defines the password used with HTTP basic authentication.
|
||||
#
|
||||
# @return [String]
|
||||
attr_accessor :password
|
||||
|
||||
# Set this to enable/disable debugging. When enabled (set to true), HTTP request/response
|
||||
# details will be logged with `logger.debug` (see the `logger` attribute).
|
||||
# Default to false.
|
||||
#
|
||||
# @return [true, false]
|
||||
attr_accessor :debugging
|
||||
|
||||
# Defines the logger used for debugging.
|
||||
# Default to `Rails.logger` (when in Rails) or logging to STDOUT.
|
||||
#
|
||||
# @return [#debug]
|
||||
attr_accessor :logger
|
||||
|
||||
# Defines the temporary folder to store downloaded files
|
||||
# (for API endpoints that have file response).
|
||||
# Default to use `Tempfile`.
|
||||
#
|
||||
# @return [String]
|
||||
attr_accessor :temp_folder_path
|
||||
|
||||
# Set this to false to skip verifying SSL certificate when calling API from https server.
|
||||
# Default to true.
|
||||
#
|
||||
# @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
|
||||
#
|
||||
# @return [true, false]
|
||||
attr_accessor :verify_ssl
|
||||
|
||||
# Set this to customize the certificate file to verify the peer.
|
||||
#
|
||||
# @return [String] the path to the certificate file
|
||||
#
|
||||
# @see The `cainfo` option of Typhoeus, `--cert` option of libcurl. Related source code:
|
||||
# https://github.com/typhoeus/typhoeus/blob/master/lib/typhoeus/easy_factory.rb#L145
|
||||
attr_accessor :ssl_ca_cert
|
||||
|
||||
attr_accessor :inject_format
|
||||
|
||||
attr_accessor :force_ending_format
|
||||
|
||||
class << self
|
||||
def method_missing(method_name, *args, &block)
|
||||
config = Configuration.instance
|
||||
if config.respond_to?(method_name)
|
||||
config.send(method_name, *args, &block)
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def initialize
|
||||
@scheme = 'http'
|
||||
@host = 'petstore.swagger.io'
|
||||
@base_path = '/v2'
|
||||
@api_key = {}
|
||||
@api_key_prefix = {}
|
||||
@verify_ssl = true
|
||||
@debugging = false
|
||||
@inject_format = false
|
||||
@force_ending_format = false
|
||||
@logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
|
||||
end
|
||||
|
||||
def api_client
|
||||
@api_client ||= ApiClient.new
|
||||
end
|
||||
|
||||
def scheme=(scheme)
|
||||
# remove :// from scheme
|
||||
@scheme = scheme.sub(/:\/\//, '')
|
||||
end
|
||||
|
||||
def host=(host)
|
||||
# remove http(s):// and anything after a slash
|
||||
@host = host.sub(/https?:\/\//, '').split('/').first
|
||||
end
|
||||
|
||||
def base_path=(base_path)
|
||||
# Add leading and trailing slashes to base_path
|
||||
@base_path = "/#{base_path}".gsub(/\/+/, '/')
|
||||
@base_path = "" if @base_path == "/"
|
||||
end
|
||||
|
||||
def base_url
|
||||
url = "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}"
|
||||
URI.encode(url)
|
||||
end
|
||||
|
||||
# Gets API key (with prefix if set).
|
||||
# @param [String] param_name the parameter name of API key auth
|
||||
def api_key_with_prefix(param_name)
|
||||
if @api_key_prefix[param_name]
|
||||
"#{@api_key_prefix[param_name]} #{@api_key[param_name]}"
|
||||
else
|
||||
@api_key[param_name]
|
||||
end
|
||||
end
|
||||
|
||||
# Gets Basic Auth token string
|
||||
def basic_auth_token
|
||||
'Basic ' + ["#{username}:#{password}"].pack('m').delete("\r\n")
|
||||
end
|
||||
|
||||
# Returns Auth Settings hash for api client.
|
||||
def auth_settings
|
||||
{
|
||||
'api_key' =>
|
||||
{
|
||||
type: 'api_key',
|
||||
in: 'header',
|
||||
key: 'api_key',
|
||||
value: api_key_with_prefix('api_key')
|
||||
},
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
@ -9,40 +9,35 @@ describe Petstore::ApiClient do
|
||||
|
||||
context 'host' do
|
||||
it 'removes http from host' do
|
||||
c = Petstore::ApiClient.new
|
||||
c.configure {|c| c.host = 'http://example.com' }
|
||||
c.host.should == 'example.com'
|
||||
Petstore.configure { |c| c.host = 'http://example.com' }
|
||||
Petstore.configure.host.should == 'example.com'
|
||||
end
|
||||
|
||||
it 'removes https from host' do
|
||||
c = Petstore::ApiClient.new {|c| c.host = 'https://wookiee.com' }
|
||||
c.host.should == 'wookiee.com'
|
||||
Petstore.configure { |c| c.host = 'https://wookiee.com' }
|
||||
Petstore.configure.host.should == 'wookiee.com'
|
||||
end
|
||||
|
||||
it 'removes trailing path from host' do
|
||||
c = Petstore::ApiClient.new
|
||||
c.configure {|c| c.host = 'hobo.com/v4' }
|
||||
c.host.should == 'hobo.com'
|
||||
Petstore.configure { |c| c.host = 'hobo.com/v4' }
|
||||
Petstore.configure.host.should == 'hobo.com'
|
||||
end
|
||||
end
|
||||
|
||||
context 'base_path' do
|
||||
it "prepends a slash to base_path" do
|
||||
c = Petstore::ApiClient.new
|
||||
c.configure {|c| c.base_path = 'v4/dog' }
|
||||
c.base_path.should == '/v4/dog'
|
||||
Petstore.configure { |c| c.base_path = 'v4/dog' }
|
||||
Petstore.configure.base_path.should == '/v4/dog'
|
||||
end
|
||||
|
||||
it "doesn't prepend a slash if one is already there" do
|
||||
c = Petstore::ApiClient.new
|
||||
c.configure {|c| c.base_path = '/v4/dog' }
|
||||
c.base_path.should == '/v4/dog'
|
||||
Petstore.configure { |c| c.base_path = '/v4/dog' }
|
||||
Petstore.configure.base_path.should == '/v4/dog'
|
||||
end
|
||||
|
||||
it "ends up as a blank string if nil" do
|
||||
c = Petstore::ApiClient.new
|
||||
c.configure {|c| c.base_path = nil }
|
||||
c.base_path.should == ''
|
||||
Petstore.configure { |c| c.base_path = nil }
|
||||
Petstore.configure.base_path.should == ''
|
||||
end
|
||||
end
|
||||
|
||||
@ -52,10 +47,13 @@ describe Petstore::ApiClient do
|
||||
|
||||
describe "#update_params_for_auth!" do
|
||||
it "sets header api-key parameter with prefix" do
|
||||
api_client = Petstore::ApiClient.new do |c|
|
||||
Petstore.configure do |c|
|
||||
c.api_key_prefix['api_key'] = 'PREFIX'
|
||||
c.api_key['api_key'] = 'special-key'
|
||||
end
|
||||
|
||||
api_client = Petstore::ApiClient.new
|
||||
|
||||
header_params = {}
|
||||
query_params = {}
|
||||
auth_names = ['api_key', 'unknown']
|
||||
@ -65,10 +63,13 @@ describe Petstore::ApiClient do
|
||||
end
|
||||
|
||||
it "sets header api-key parameter without prefix" do
|
||||
api_client = Petstore::ApiClient.new do |c|
|
||||
Petstore.configure do |c|
|
||||
c.api_key_prefix['api_key'] = nil
|
||||
c.api_key['api_key'] = 'special-key'
|
||||
end
|
||||
|
||||
api_client = Petstore::ApiClient.new
|
||||
|
||||
header_params = {}
|
||||
query_params = {}
|
||||
auth_names = ['api_key', 'unknown']
|
||||
|
Loading…
Reference in New Issue
Block a user