Merge pull request #1698 from xhh/ruby-config

Ruby client: allow setting Configuration in ApiClient
This commit is contained in:
wing328 2015-12-14 15:14:08 +08:00
commit 2a5b96dbcf
13 changed files with 194 additions and 197 deletions

View File

@ -5,8 +5,8 @@ module {{moduleName}}
class {{classname}} class {{classname}}
attr_accessor :api_client attr_accessor :api_client
def initialize(api_client = nil) def initialize(api_client = ApiClient.default)
@api_client = api_client || Configuration.api_client @api_client = api_client
end end
{{#operation}} {{#operation}}
{{newline}} {{newline}}
@ -28,8 +28,8 @@ module {{moduleName}}
{{#allParams}}{{^required}} # @option opts [{{{dataType}}}] :{{paramName}} {{description}} {{#allParams}}{{^required}} # @option opts [{{{dataType}}}] :{{paramName}} {{description}}
{{/required}}{{/allParams}} # @return [Array<({{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}nil{{/returnType}}, Fixnum, Hash)>] {{#returnType}}{{{returnType}}} data{{/returnType}}{{^returnType}}nil{{/returnType}}, response status code and response headers {{/required}}{{/allParams}} # @return [Array<({{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}nil{{/returnType}}, Fixnum, Hash)>] {{#returnType}}{{{returnType}}} data{{/returnType}}{{^returnType}}nil{{/returnType}}, response status code and response headers
def {{operationId}}_with_http_info({{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}opts = {}) def {{operationId}}_with_http_info({{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}opts = {})
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "Calling API: {{classname}}#{{operationId}} ..." @api_client.config.logger.debug "Calling API: {{classname}}#{{operationId}} ..."
end end
{{#allParams}}{{#required}} {{#allParams}}{{#required}}
# verify the required parameter '{{paramName}}' is set # verify the required parameter '{{paramName}}' is set
@ -81,8 +81,8 @@ module {{moduleName}}
:body => post_body, :body => post_body,
:auth_names => auth_names{{#returnType}}, :auth_names => auth_names{{#returnType}},
:return_type => '{{{returnType}}}'{{/returnType}}) :return_type => '{{{returnType}}}'{{/returnType}})
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "API called: {{classname}}#{{operationId}}\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}" @api_client.config.logger.debug "API called: {{classname}}#{{operationId}}\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
end end
return data, status_code, headers return data, status_code, headers
end end

View File

@ -7,24 +7,27 @@ require 'uri'
module {{moduleName}} module {{moduleName}}
class ApiClient class ApiClient
# The Configuration object holding settings to be used in the API client.
attr_accessor :host attr_accessor :config
# Defines the headers to be used in HTTP requests of all API calls by default. # Defines the headers to be used in HTTP requests of all API calls by default.
# #
# @return [Hash] # @return [Hash]
attr_accessor :default_headers attr_accessor :default_headers
def initialize(host = nil) def initialize(config = Configuration.default)
@host = host || Configuration.base_url @config = config
@format = 'json'
@user_agent = "ruby-swagger-#{VERSION}" @user_agent = "ruby-swagger-#{VERSION}"
@default_headers = { @default_headers = {
'Content-Type' => "application/#{@format.downcase}", 'Content-Type' => "application/json",
'User-Agent' => @user_agent 'User-Agent' => @user_agent
} }
end end
def self.default
@@default ||= ApiClient.new
end
# Call an API with given options. # Call an API with given options.
# #
# @return [Array<(Object, Fixnum, Hash)>] an array of 3 elements: # @return [Array<(Object, Fixnum, Hash)>] an array of 3 elements:
@ -33,8 +36,8 @@ module {{moduleName}}
request = build_request(http_method, path, opts) request = build_request(http_method, path, opts)
response = request.run response = request.run
if Configuration.debugging if @config.debugging
Configuration.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n" @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
end end
unless response.success? unless response.success?
@ -68,18 +71,18 @@ module {{moduleName}}
:method => http_method, :method => http_method,
:headers => header_params, :headers => header_params,
:params => query_params, :params => query_params,
:ssl_verifypeer => Configuration.verify_ssl, :ssl_verifypeer => @config.verify_ssl,
:sslcert => Configuration.cert_file, :sslcert => @config.cert_file,
:sslkey => Configuration.key_file, :sslkey => @config.key_file,
:cainfo => Configuration.ssl_ca_cert, :cainfo => @config.ssl_ca_cert,
:verbose => Configuration.debugging :verbose => @config.debugging
} }
if [:post, :patch, :put, :delete].include?(http_method) if [:post, :patch, :put, :delete].include?(http_method)
req_body = build_request_body(header_params, form_params, opts[:body]) req_body = build_request_body(header_params, form_params, opts[:body])
req_opts.update :body => req_body req_opts.update :body => req_body
if Configuration.debugging if @config.debugging
Configuration.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n" @config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
end end
end end
@ -168,7 +171,7 @@ module {{moduleName}}
# @see Configuration#temp_folder_path # @see Configuration#temp_folder_path
# @return [File] the file downloaded # @return [File] the file downloaded
def download_file(response) def download_file(response)
tmp_file = Tempfile.new '', Configuration.temp_folder_path tmp_file = Tempfile.new '', @config.temp_folder_path
content_disposition = response.headers['Content-Disposition'] content_disposition = response.headers['Content-Disposition']
if content_disposition if content_disposition
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1] filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
@ -180,15 +183,15 @@ module {{moduleName}}
tmp_file.close! tmp_file.close!
File.open(path, 'w') { |file| file.write(response.body) } File.open(path, 'w') { |file| file.write(response.body) }
Configuration.logger.info "File written to #{path}. Please move the file to a proper "\ @config.logger.info "File written to #{path}. Please move the file to a proper folder "\
"folder for further processing and delete the temp afterwards" "for further processing and delete the temp afterwards"
File.new(path) File.new(path)
end end
def build_request_url(path) def build_request_url(path)
# Add leading and trailing slashes to path # Add leading and trailing slashes to path
path = "/#{path}".gsub(/\/+/, '/') path = "/#{path}".gsub(/\/+/, '/')
URI.encode(host + path) URI.encode(@config.base_url + path)
end end
def build_request_body(header_params, form_params, body) def build_request_body(header_params, form_params, body)
@ -216,7 +219,7 @@ module {{moduleName}}
# Update hearder and query params based on authentication settings. # Update hearder and query params based on authentication settings.
def update_params_for_auth!(header_params, query_params, auth_names) def update_params_for_auth!(header_params, query_params, auth_names)
Array(auth_names).each do |auth_name| Array(auth_names).each do |auth_name|
auth_setting = Configuration.auth_settings[auth_name] auth_setting = @config.auth_settings[auth_name]
next unless auth_setting next unless auth_setting
case auth_setting[:in] case auth_setting[:in]
when 'header' then header_params[auth_setting[:key]] = auth_setting[:value] when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]

View File

@ -1,14 +1,7 @@
require 'uri' require 'uri'
require 'singleton'
module {{moduleName}} module {{moduleName}}
class Configuration class Configuration
include Singleton
# Default api client
attr_accessor :api_client
# Defines url scheme # Defines url scheme
attr_accessor :scheme attr_accessor :scheme
@ -94,17 +87,6 @@ module {{moduleName}}
attr_accessor :force_ending_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 def initialize
@scheme = '{{scheme}}' @scheme = '{{scheme}}'
@host = '{{host}}' @host = '{{host}}'
@ -118,10 +100,17 @@ module {{moduleName}}
@inject_format = false @inject_format = false
@force_ending_format = false @force_ending_format = false
@logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT) @logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
yield(self) if block_given?
end end
def api_client # The default Configuration object.
@api_client ||= ApiClient.new def self.default
@@default ||= Configuration.new
end
def configure
yield(self) if block_given?
end end
def scheme=(scheme) def scheme=(scheme)

View File

@ -19,17 +19,17 @@ require '{{importPath}}'
module {{moduleName}} module {{moduleName}}
class << self class << self
# Configure sdk using block. # Customize default settings for the SDK using block.
# {{moduleName}}.configure do |config| # {{moduleName}}.configure do |config|
# config.username = "xxx" # config.username = "xxx"
# config.password = "xxx" # config.password = "xxx"
# end # end
# If no block given, return the configuration singleton instance. # If no block given, return the default Configuration object.
def configure def configure
if block_given? if block_given?
yield Configuration.instance yield(Configuration.default)
else else
Configuration.instance Configuration.default
end end
end end
end end

View File

@ -19,17 +19,17 @@ require 'petstore/api/pet_api'
module Petstore module Petstore
class << self class << self
# Configure sdk using block. # Customize default settings for the SDK using block.
# Petstore.configure do |config| # Petstore.configure do |config|
# config.username = "xxx" # config.username = "xxx"
# config.password = "xxx" # config.password = "xxx"
# end # end
# If no block given, return the configuration singleton instance. # If no block given, return the default Configuration object.
def configure def configure
if block_given? if block_given?
yield Configuration.instance yield(Configuration.default)
else else
Configuration.instance Configuration.default
end end
end end
end end

View File

@ -4,8 +4,8 @@ module Petstore
class PetApi class PetApi
attr_accessor :api_client attr_accessor :api_client
def initialize(api_client = nil) def initialize(api_client = ApiClient.default)
@api_client = api_client || Configuration.api_client @api_client = api_client
end end
# Update an existing pet # Update an existing pet
@ -24,8 +24,8 @@ module Petstore
# @option opts [Pet] :body Pet object that needs to be added to the store # @option opts [Pet] :body Pet object that needs to be added to the store
# @return [Array<(nil, Fixnum, Hash)>] nil, response status code and response headers # @return [Array<(nil, Fixnum, Hash)>] nil, response status code and response headers
def update_pet_with_http_info(opts = {}) def update_pet_with_http_info(opts = {})
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "Calling API: PetApi#update_pet ..." @api_client.config.logger.debug "Calling API: PetApi#update_pet ..."
end end
# resource path # resource path
@ -59,8 +59,8 @@ module Petstore
:form_params => form_params, :form_params => form_params,
:body => post_body, :body => post_body,
:auth_names => auth_names) :auth_names => auth_names)
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "API called: PetApi#update_pet\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}" @api_client.config.logger.debug "API called: PetApi#update_pet\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
end end
return data, status_code, headers return data, status_code, headers
end end
@ -81,8 +81,8 @@ module Petstore
# @option opts [Pet] :body Pet object that needs to be added to the store # @option opts [Pet] :body Pet object that needs to be added to the store
# @return [Array<(nil, Fixnum, Hash)>] nil, response status code and response headers # @return [Array<(nil, Fixnum, Hash)>] nil, response status code and response headers
def add_pet_with_http_info(opts = {}) def add_pet_with_http_info(opts = {})
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "Calling API: PetApi#add_pet ..." @api_client.config.logger.debug "Calling API: PetApi#add_pet ..."
end end
# resource path # resource path
@ -116,8 +116,8 @@ module Petstore
:form_params => form_params, :form_params => form_params,
:body => post_body, :body => post_body,
:auth_names => auth_names) :auth_names => auth_names)
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "API called: PetApi#add_pet\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}" @api_client.config.logger.debug "API called: PetApi#add_pet\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
end end
return data, status_code, headers return data, status_code, headers
end end
@ -138,8 +138,8 @@ module Petstore
# @option opts [Array<String>] :status Status values that need to be considered for filter # @option opts [Array<String>] :status Status values that need to be considered for filter
# @return [Array<(Array<Pet>, Fixnum, Hash)>] Array<Pet> data, response status code and response headers # @return [Array<(Array<Pet>, Fixnum, Hash)>] Array<Pet> data, response status code and response headers
def find_pets_by_status_with_http_info(opts = {}) def find_pets_by_status_with_http_info(opts = {})
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "Calling API: PetApi#find_pets_by_status ..." @api_client.config.logger.debug "Calling API: PetApi#find_pets_by_status ..."
end end
# resource path # resource path
@ -175,8 +175,8 @@ module Petstore
:body => post_body, :body => post_body,
:auth_names => auth_names, :auth_names => auth_names,
:return_type => 'Array<Pet>') :return_type => 'Array<Pet>')
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "API called: PetApi#find_pets_by_status\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}" @api_client.config.logger.debug "API called: PetApi#find_pets_by_status\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
end end
return data, status_code, headers return data, status_code, headers
end end
@ -197,8 +197,8 @@ module Petstore
# @option opts [Array<String>] :tags Tags to filter by # @option opts [Array<String>] :tags Tags to filter by
# @return [Array<(Array<Pet>, Fixnum, Hash)>] Array<Pet> data, response status code and response headers # @return [Array<(Array<Pet>, Fixnum, Hash)>] Array<Pet> data, response status code and response headers
def find_pets_by_tags_with_http_info(opts = {}) def find_pets_by_tags_with_http_info(opts = {})
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "Calling API: PetApi#find_pets_by_tags ..." @api_client.config.logger.debug "Calling API: PetApi#find_pets_by_tags ..."
end end
# resource path # resource path
@ -234,8 +234,8 @@ module Petstore
:body => post_body, :body => post_body,
:auth_names => auth_names, :auth_names => auth_names,
:return_type => 'Array<Pet>') :return_type => 'Array<Pet>')
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "API called: PetApi#find_pets_by_tags\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}" @api_client.config.logger.debug "API called: PetApi#find_pets_by_tags\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
end end
return data, status_code, headers return data, status_code, headers
end end
@ -256,8 +256,8 @@ module Petstore
# @param [Hash] opts the optional parameters # @param [Hash] opts the optional parameters
# @return [Array<(Pet, Fixnum, Hash)>] Pet data, response status code and response headers # @return [Array<(Pet, Fixnum, Hash)>] Pet data, response status code and response headers
def get_pet_by_id_with_http_info(pet_id, opts = {}) def get_pet_by_id_with_http_info(pet_id, opts = {})
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "Calling API: PetApi#get_pet_by_id ..." @api_client.config.logger.debug "Calling API: PetApi#get_pet_by_id ..."
end end
# verify the required parameter 'pet_id' is set # verify the required parameter 'pet_id' is set
@ -295,8 +295,8 @@ module Petstore
:body => post_body, :body => post_body,
:auth_names => auth_names, :auth_names => auth_names,
:return_type => 'Pet') :return_type => 'Pet')
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "API called: PetApi#get_pet_by_id\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}" @api_client.config.logger.debug "API called: PetApi#get_pet_by_id\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
end end
return data, status_code, headers return data, status_code, headers
end end
@ -321,8 +321,8 @@ module Petstore
# @option opts [String] :status Updated status of the pet # @option opts [String] :status Updated status of the pet
# @return [Array<(nil, Fixnum, Hash)>] nil, response status code and response headers # @return [Array<(nil, Fixnum, Hash)>] nil, response status code and response headers
def update_pet_with_form_with_http_info(pet_id, opts = {}) def update_pet_with_form_with_http_info(pet_id, opts = {})
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "Calling API: PetApi#update_pet_with_form ..." @api_client.config.logger.debug "Calling API: PetApi#update_pet_with_form ..."
end end
# verify the required parameter 'pet_id' is set # verify the required parameter 'pet_id' is set
@ -361,8 +361,8 @@ module Petstore
:form_params => form_params, :form_params => form_params,
:body => post_body, :body => post_body,
:auth_names => auth_names) :auth_names => auth_names)
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "API called: PetApi#update_pet_with_form\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}" @api_client.config.logger.debug "API called: PetApi#update_pet_with_form\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
end end
return data, status_code, headers return data, status_code, headers
end end
@ -385,8 +385,8 @@ module Petstore
# @option opts [String] :api_key # @option opts [String] :api_key
# @return [Array<(nil, Fixnum, Hash)>] nil, response status code and response headers # @return [Array<(nil, Fixnum, Hash)>] nil, response status code and response headers
def delete_pet_with_http_info(pet_id, opts = {}) def delete_pet_with_http_info(pet_id, opts = {})
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "Calling API: PetApi#delete_pet ..." @api_client.config.logger.debug "Calling API: PetApi#delete_pet ..."
end end
# verify the required parameter 'pet_id' is set # verify the required parameter 'pet_id' is set
@ -424,8 +424,8 @@ module Petstore
:form_params => form_params, :form_params => form_params,
:body => post_body, :body => post_body,
:auth_names => auth_names) :auth_names => auth_names)
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "API called: PetApi#delete_pet\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}" @api_client.config.logger.debug "API called: PetApi#delete_pet\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
end end
return data, status_code, headers return data, status_code, headers
end end
@ -450,8 +450,8 @@ module Petstore
# @option opts [File] :file file to upload # @option opts [File] :file file to upload
# @return [Array<(nil, Fixnum, Hash)>] nil, response status code and response headers # @return [Array<(nil, Fixnum, Hash)>] nil, response status code and response headers
def upload_file_with_http_info(pet_id, opts = {}) def upload_file_with_http_info(pet_id, opts = {})
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "Calling API: PetApi#upload_file ..." @api_client.config.logger.debug "Calling API: PetApi#upload_file ..."
end end
# verify the required parameter 'pet_id' is set # verify the required parameter 'pet_id' is set
@ -490,8 +490,8 @@ module Petstore
:form_params => form_params, :form_params => form_params,
:body => post_body, :body => post_body,
:auth_names => auth_names) :auth_names => auth_names)
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "API called: PetApi#upload_file\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}" @api_client.config.logger.debug "API called: PetApi#upload_file\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
end end
return data, status_code, headers return data, status_code, headers
end end

View File

@ -4,8 +4,8 @@ module Petstore
class StoreApi class StoreApi
attr_accessor :api_client attr_accessor :api_client
def initialize(api_client = nil) def initialize(api_client = ApiClient.default)
@api_client = api_client || Configuration.api_client @api_client = api_client
end end
# Returns pet inventories by status # Returns pet inventories by status
@ -22,8 +22,8 @@ module Petstore
# @param [Hash] opts the optional parameters # @param [Hash] opts the optional parameters
# @return [Array<(Hash<String, Integer>, Fixnum, Hash)>] Hash<String, Integer> data, response status code and response headers # @return [Array<(Hash<String, Integer>, Fixnum, Hash)>] Hash<String, Integer> data, response status code and response headers
def get_inventory_with_http_info(opts = {}) def get_inventory_with_http_info(opts = {})
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "Calling API: StoreApi#get_inventory ..." @api_client.config.logger.debug "Calling API: StoreApi#get_inventory ..."
end end
# resource path # resource path
@ -58,8 +58,8 @@ module Petstore
:body => post_body, :body => post_body,
:auth_names => auth_names, :auth_names => auth_names,
:return_type => 'Hash<String, Integer>') :return_type => 'Hash<String, Integer>')
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "API called: StoreApi#get_inventory\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}" @api_client.config.logger.debug "API called: StoreApi#get_inventory\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
end end
return data, status_code, headers return data, status_code, headers
end end
@ -80,8 +80,8 @@ module Petstore
# @option opts [Order] :body order placed for purchasing the pet # @option opts [Order] :body order placed for purchasing the pet
# @return [Array<(Order, Fixnum, Hash)>] Order data, response status code and response headers # @return [Array<(Order, Fixnum, Hash)>] Order data, response status code and response headers
def place_order_with_http_info(opts = {}) def place_order_with_http_info(opts = {})
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "Calling API: StoreApi#place_order ..." @api_client.config.logger.debug "Calling API: StoreApi#place_order ..."
end end
# resource path # resource path
@ -116,8 +116,8 @@ module Petstore
:body => post_body, :body => post_body,
:auth_names => auth_names, :auth_names => auth_names,
:return_type => 'Order') :return_type => 'Order')
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "API called: StoreApi#place_order\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}" @api_client.config.logger.debug "API called: StoreApi#place_order\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
end end
return data, status_code, headers return data, status_code, headers
end end
@ -138,8 +138,8 @@ module Petstore
# @param [Hash] opts the optional parameters # @param [Hash] opts the optional parameters
# @return [Array<(Order, Fixnum, Hash)>] Order data, response status code and response headers # @return [Array<(Order, Fixnum, Hash)>] Order data, response status code and response headers
def get_order_by_id_with_http_info(order_id, opts = {}) def get_order_by_id_with_http_info(order_id, opts = {})
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "Calling API: StoreApi#get_order_by_id ..." @api_client.config.logger.debug "Calling API: StoreApi#get_order_by_id ..."
end end
# verify the required parameter 'order_id' is set # verify the required parameter 'order_id' is set
@ -177,8 +177,8 @@ module Petstore
:body => post_body, :body => post_body,
:auth_names => auth_names, :auth_names => auth_names,
:return_type => 'Order') :return_type => 'Order')
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "API called: StoreApi#get_order_by_id\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}" @api_client.config.logger.debug "API called: StoreApi#get_order_by_id\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
end end
return data, status_code, headers return data, status_code, headers
end end
@ -199,8 +199,8 @@ module Petstore
# @param [Hash] opts the optional parameters # @param [Hash] opts the optional parameters
# @return [Array<(nil, Fixnum, Hash)>] nil, response status code and response headers # @return [Array<(nil, Fixnum, Hash)>] nil, response status code and response headers
def delete_order_with_http_info(order_id, opts = {}) def delete_order_with_http_info(order_id, opts = {})
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "Calling API: StoreApi#delete_order ..." @api_client.config.logger.debug "Calling API: StoreApi#delete_order ..."
end end
# verify the required parameter 'order_id' is set # verify the required parameter 'order_id' is set
@ -237,8 +237,8 @@ module Petstore
:form_params => form_params, :form_params => form_params,
:body => post_body, :body => post_body,
:auth_names => auth_names) :auth_names => auth_names)
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "API called: StoreApi#delete_order\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}" @api_client.config.logger.debug "API called: StoreApi#delete_order\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
end end
return data, status_code, headers return data, status_code, headers
end end

View File

@ -4,8 +4,8 @@ module Petstore
class UserApi class UserApi
attr_accessor :api_client attr_accessor :api_client
def initialize(api_client = nil) def initialize(api_client = ApiClient.default)
@api_client = api_client || Configuration.api_client @api_client = api_client
end end
# Create user # Create user
@ -24,8 +24,8 @@ module Petstore
# @option opts [User] :body Created user object # @option opts [User] :body Created user object
# @return [Array<(nil, Fixnum, Hash)>] nil, response status code and response headers # @return [Array<(nil, Fixnum, Hash)>] nil, response status code and response headers
def create_user_with_http_info(opts = {}) def create_user_with_http_info(opts = {})
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "Calling API: UserApi#create_user ..." @api_client.config.logger.debug "Calling API: UserApi#create_user ..."
end end
# resource path # resource path
@ -59,8 +59,8 @@ module Petstore
:form_params => form_params, :form_params => form_params,
:body => post_body, :body => post_body,
:auth_names => auth_names) :auth_names => auth_names)
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "API called: UserApi#create_user\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}" @api_client.config.logger.debug "API called: UserApi#create_user\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
end end
return data, status_code, headers return data, status_code, headers
end end
@ -81,8 +81,8 @@ module Petstore
# @option opts [Array<User>] :body List of user object # @option opts [Array<User>] :body List of user object
# @return [Array<(nil, Fixnum, Hash)>] nil, response status code and response headers # @return [Array<(nil, Fixnum, Hash)>] nil, response status code and response headers
def create_users_with_array_input_with_http_info(opts = {}) def create_users_with_array_input_with_http_info(opts = {})
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "Calling API: UserApi#create_users_with_array_input ..." @api_client.config.logger.debug "Calling API: UserApi#create_users_with_array_input ..."
end end
# resource path # resource path
@ -116,8 +116,8 @@ module Petstore
:form_params => form_params, :form_params => form_params,
:body => post_body, :body => post_body,
:auth_names => auth_names) :auth_names => auth_names)
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "API called: UserApi#create_users_with_array_input\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}" @api_client.config.logger.debug "API called: UserApi#create_users_with_array_input\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
end end
return data, status_code, headers return data, status_code, headers
end end
@ -138,8 +138,8 @@ module Petstore
# @option opts [Array<User>] :body List of user object # @option opts [Array<User>] :body List of user object
# @return [Array<(nil, Fixnum, Hash)>] nil, response status code and response headers # @return [Array<(nil, Fixnum, Hash)>] nil, response status code and response headers
def create_users_with_list_input_with_http_info(opts = {}) def create_users_with_list_input_with_http_info(opts = {})
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "Calling API: UserApi#create_users_with_list_input ..." @api_client.config.logger.debug "Calling API: UserApi#create_users_with_list_input ..."
end end
# resource path # resource path
@ -173,8 +173,8 @@ module Petstore
:form_params => form_params, :form_params => form_params,
:body => post_body, :body => post_body,
:auth_names => auth_names) :auth_names => auth_names)
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "API called: UserApi#create_users_with_list_input\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}" @api_client.config.logger.debug "API called: UserApi#create_users_with_list_input\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
end end
return data, status_code, headers return data, status_code, headers
end end
@ -197,8 +197,8 @@ module Petstore
# @option opts [String] :password The password for login in clear text # @option opts [String] :password The password for login in clear text
# @return [Array<(String, Fixnum, Hash)>] String data, response status code and response headers # @return [Array<(String, Fixnum, Hash)>] String data, response status code and response headers
def login_user_with_http_info(opts = {}) def login_user_with_http_info(opts = {})
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "Calling API: UserApi#login_user ..." @api_client.config.logger.debug "Calling API: UserApi#login_user ..."
end end
# resource path # resource path
@ -235,8 +235,8 @@ module Petstore
:body => post_body, :body => post_body,
:auth_names => auth_names, :auth_names => auth_names,
:return_type => 'String') :return_type => 'String')
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "API called: UserApi#login_user\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}" @api_client.config.logger.debug "API called: UserApi#login_user\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
end end
return data, status_code, headers return data, status_code, headers
end end
@ -255,8 +255,8 @@ module Petstore
# @param [Hash] opts the optional parameters # @param [Hash] opts the optional parameters
# @return [Array<(nil, Fixnum, Hash)>] nil, response status code and response headers # @return [Array<(nil, Fixnum, Hash)>] nil, response status code and response headers
def logout_user_with_http_info(opts = {}) def logout_user_with_http_info(opts = {})
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "Calling API: UserApi#logout_user ..." @api_client.config.logger.debug "Calling API: UserApi#logout_user ..."
end end
# resource path # resource path
@ -290,8 +290,8 @@ module Petstore
:form_params => form_params, :form_params => form_params,
:body => post_body, :body => post_body,
:auth_names => auth_names) :auth_names => auth_names)
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "API called: UserApi#logout_user\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}" @api_client.config.logger.debug "API called: UserApi#logout_user\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
end end
return data, status_code, headers return data, status_code, headers
end end
@ -312,8 +312,8 @@ module Petstore
# @param [Hash] opts the optional parameters # @param [Hash] opts the optional parameters
# @return [Array<(User, Fixnum, Hash)>] User data, response status code and response headers # @return [Array<(User, Fixnum, Hash)>] User data, response status code and response headers
def get_user_by_name_with_http_info(username, opts = {}) def get_user_by_name_with_http_info(username, opts = {})
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "Calling API: UserApi#get_user_by_name ..." @api_client.config.logger.debug "Calling API: UserApi#get_user_by_name ..."
end end
# verify the required parameter 'username' is set # verify the required parameter 'username' is set
@ -351,8 +351,8 @@ module Petstore
:body => post_body, :body => post_body,
:auth_names => auth_names, :auth_names => auth_names,
:return_type => 'User') :return_type => 'User')
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "API called: UserApi#get_user_by_name\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}" @api_client.config.logger.debug "API called: UserApi#get_user_by_name\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
end end
return data, status_code, headers return data, status_code, headers
end end
@ -375,8 +375,8 @@ module Petstore
# @option opts [User] :body Updated user object # @option opts [User] :body Updated user object
# @return [Array<(nil, Fixnum, Hash)>] nil, response status code and response headers # @return [Array<(nil, Fixnum, Hash)>] nil, response status code and response headers
def update_user_with_http_info(username, opts = {}) def update_user_with_http_info(username, opts = {})
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "Calling API: UserApi#update_user ..." @api_client.config.logger.debug "Calling API: UserApi#update_user ..."
end end
# verify the required parameter 'username' is set # verify the required parameter 'username' is set
@ -413,8 +413,8 @@ module Petstore
:form_params => form_params, :form_params => form_params,
:body => post_body, :body => post_body,
:auth_names => auth_names) :auth_names => auth_names)
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "API called: UserApi#update_user\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}" @api_client.config.logger.debug "API called: UserApi#update_user\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
end end
return data, status_code, headers return data, status_code, headers
end end
@ -435,8 +435,8 @@ module Petstore
# @param [Hash] opts the optional parameters # @param [Hash] opts the optional parameters
# @return [Array<(nil, Fixnum, Hash)>] nil, response status code and response headers # @return [Array<(nil, Fixnum, Hash)>] nil, response status code and response headers
def delete_user_with_http_info(username, opts = {}) def delete_user_with_http_info(username, opts = {})
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "Calling API: UserApi#delete_user ..." @api_client.config.logger.debug "Calling API: UserApi#delete_user ..."
end end
# verify the required parameter 'username' is set # verify the required parameter 'username' is set
@ -473,8 +473,8 @@ module Petstore
:form_params => form_params, :form_params => form_params,
:body => post_body, :body => post_body,
:auth_names => auth_names) :auth_names => auth_names)
if Configuration.debugging if @api_client.config.debugging
Configuration.logger.debug "API called: UserApi#delete_user\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}" @api_client.config.logger.debug "API called: UserApi#delete_user\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
end end
return data, status_code, headers return data, status_code, headers
end end

View File

@ -7,24 +7,27 @@ require 'uri'
module Petstore module Petstore
class ApiClient class ApiClient
# The Configuration object holding settings to be used in the API client.
attr_accessor :host attr_accessor :config
# Defines the headers to be used in HTTP requests of all API calls by default. # Defines the headers to be used in HTTP requests of all API calls by default.
# #
# @return [Hash] # @return [Hash]
attr_accessor :default_headers attr_accessor :default_headers
def initialize(host = nil) def initialize(config = Configuration.default)
@host = host || Configuration.base_url @config = config
@format = 'json'
@user_agent = "ruby-swagger-#{VERSION}" @user_agent = "ruby-swagger-#{VERSION}"
@default_headers = { @default_headers = {
'Content-Type' => "application/#{@format.downcase}", 'Content-Type' => "application/json",
'User-Agent' => @user_agent 'User-Agent' => @user_agent
} }
end end
def self.default
@@default ||= ApiClient.new
end
# Call an API with given options. # Call an API with given options.
# #
# @return [Array<(Object, Fixnum, Hash)>] an array of 3 elements: # @return [Array<(Object, Fixnum, Hash)>] an array of 3 elements:
@ -33,8 +36,8 @@ module Petstore
request = build_request(http_method, path, opts) request = build_request(http_method, path, opts)
response = request.run response = request.run
if Configuration.debugging if @config.debugging
Configuration.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n" @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
end end
unless response.success? unless response.success?
@ -68,18 +71,18 @@ module Petstore
:method => http_method, :method => http_method,
:headers => header_params, :headers => header_params,
:params => query_params, :params => query_params,
:ssl_verifypeer => Configuration.verify_ssl, :ssl_verifypeer => @config.verify_ssl,
:sslcert => Configuration.cert_file, :sslcert => @config.cert_file,
:sslkey => Configuration.key_file, :sslkey => @config.key_file,
:cainfo => Configuration.ssl_ca_cert, :cainfo => @config.ssl_ca_cert,
:verbose => Configuration.debugging :verbose => @config.debugging
} }
if [:post, :patch, :put, :delete].include?(http_method) if [:post, :patch, :put, :delete].include?(http_method)
req_body = build_request_body(header_params, form_params, opts[:body]) req_body = build_request_body(header_params, form_params, opts[:body])
req_opts.update :body => req_body req_opts.update :body => req_body
if Configuration.debugging if @config.debugging
Configuration.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n" @config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
end end
end end
@ -168,7 +171,7 @@ module Petstore
# @see Configuration#temp_folder_path # @see Configuration#temp_folder_path
# @return [File] the file downloaded # @return [File] the file downloaded
def download_file(response) def download_file(response)
tmp_file = Tempfile.new '', Configuration.temp_folder_path tmp_file = Tempfile.new '', @config.temp_folder_path
content_disposition = response.headers['Content-Disposition'] content_disposition = response.headers['Content-Disposition']
if content_disposition if content_disposition
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1] filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
@ -180,15 +183,15 @@ module Petstore
tmp_file.close! tmp_file.close!
File.open(path, 'w') { |file| file.write(response.body) } File.open(path, 'w') { |file| file.write(response.body) }
Configuration.logger.info "File written to #{path}. Please move the file to a proper "\ @config.logger.info "File written to #{path}. Please move the file to a proper folder "\
"folder for further processing and delete the temp afterwards" "for further processing and delete the temp afterwards"
File.new(path) File.new(path)
end end
def build_request_url(path) def build_request_url(path)
# Add leading and trailing slashes to path # Add leading and trailing slashes to path
path = "/#{path}".gsub(/\/+/, '/') path = "/#{path}".gsub(/\/+/, '/')
URI.encode(host + path) URI.encode(@config.base_url + path)
end end
def build_request_body(header_params, form_params, body) def build_request_body(header_params, form_params, body)
@ -216,7 +219,7 @@ module Petstore
# Update hearder and query params based on authentication settings. # Update hearder and query params based on authentication settings.
def update_params_for_auth!(header_params, query_params, auth_names) def update_params_for_auth!(header_params, query_params, auth_names)
Array(auth_names).each do |auth_name| Array(auth_names).each do |auth_name|
auth_setting = Configuration.auth_settings[auth_name] auth_setting = @config.auth_settings[auth_name]
next unless auth_setting next unless auth_setting
case auth_setting[:in] case auth_setting[:in]
when 'header' then header_params[auth_setting[:key]] = auth_setting[:value] when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]

View File

@ -1,14 +1,7 @@
require 'uri' require 'uri'
require 'singleton'
module Petstore module Petstore
class Configuration class Configuration
include Singleton
# Default api client
attr_accessor :api_client
# Defines url scheme # Defines url scheme
attr_accessor :scheme attr_accessor :scheme
@ -94,17 +87,6 @@ module Petstore
attr_accessor :force_ending_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 def initialize
@scheme = 'http' @scheme = 'http'
@host = 'petstore.swagger.io' @host = 'petstore.swagger.io'
@ -118,10 +100,17 @@ module Petstore
@inject_format = false @inject_format = false
@force_ending_format = false @force_ending_format = false
@logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT) @logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
yield(self) if block_given?
end end
def api_client # The default Configuration object.
@api_client ||= ApiClient.new def self.default
@@default ||= Configuration.new
end
def configure
yield(self) if block_given?
end end
def scheme=(scheme) def scheme=(scheme)

View File

@ -10,34 +10,34 @@ describe Petstore::ApiClient do
context 'host' do context 'host' do
it 'removes http from host' do it 'removes http from host' do
Petstore.configure { |c| c.host = 'http://example.com' } Petstore.configure { |c| c.host = 'http://example.com' }
Petstore.configure.host.should == 'example.com' Petstore::Configuration.default.host.should == 'example.com'
end end
it 'removes https from host' do it 'removes https from host' do
Petstore.configure { |c| c.host = 'https://wookiee.com' } Petstore.configure { |c| c.host = 'https://wookiee.com' }
Petstore.configure.host.should == 'wookiee.com' Petstore::ApiClient.default.config.host.should == 'wookiee.com'
end end
it 'removes trailing path from host' do it 'removes trailing path from host' do
Petstore.configure { |c| c.host = 'hobo.com/v4' } Petstore.configure { |c| c.host = 'hobo.com/v4' }
Petstore.configure.host.should == 'hobo.com' Petstore::Configuration.default.host.should == 'hobo.com'
end end
end end
context 'base_path' do context 'base_path' do
it "prepends a slash to base_path" do it "prepends a slash to base_path" do
Petstore.configure { |c| c.base_path = 'v4/dog' } Petstore.configure { |c| c.base_path = 'v4/dog' }
Petstore.configure.base_path.should == '/v4/dog' Petstore::Configuration.default.base_path.should == '/v4/dog'
end end
it "doesn't prepend a slash if one is already there" do it "doesn't prepend a slash if one is already there" do
Petstore.configure { |c| c.base_path = '/v4/dog' } Petstore.configure { |c| c.base_path = '/v4/dog' }
Petstore.configure.base_path.should == '/v4/dog' Petstore::Configuration.default.base_path.should == '/v4/dog'
end end
it "ends up as a blank string if nil" do it "ends up as a blank string if nil" do
Petstore.configure { |c| c.base_path = nil } Petstore.configure { |c| c.base_path = nil }
Petstore.configure.base_path.should == '' Petstore::Configuration.default.base_path.should == ''
end end
end end
@ -53,13 +53,26 @@ describe Petstore::ApiClient do
end end
api_client = Petstore::ApiClient.new api_client = Petstore::ApiClient.new
config2 = Petstore::Configuration.new do |c|
c.api_key_prefix['api_key'] = 'PREFIX2'
c.api_key['api_key'] = 'special-key2'
end
api_client2 = Petstore::ApiClient.new(config2)
auth_names = ['api_key', 'unknown']
header_params = {} header_params = {}
query_params = {} query_params = {}
auth_names = ['api_key', 'unknown']
api_client.update_params_for_auth! header_params, query_params, auth_names api_client.update_params_for_auth! header_params, query_params, auth_names
header_params.should == {'api_key' => 'PREFIX special-key'} header_params.should == {'api_key' => 'PREFIX special-key'}
query_params.should == {} query_params.should == {}
header_params = {}
query_params = {}
api_client2.update_params_for_auth! header_params, query_params, auth_names
header_params.should == {'api_key' => 'PREFIX2 special-key2'}
query_params.should == {}
end end
it "sets header api-key parameter without prefix" do it "sets header api-key parameter without prefix" do

View File

@ -1,7 +1,7 @@
require 'spec_helper' require 'spec_helper'
describe Petstore::Configuration do describe Petstore::Configuration do
let(:config) { Petstore::Configuration.instance } let(:config) { Petstore::Configuration.default }
before(:each) do before(:each) do
Petstore.configure do |c| Petstore.configure do |c|

View File

@ -36,7 +36,7 @@ end
# help # help
#end #end
API_CLIENT = Petstore::ApiClient.new API_CLIENT = Petstore::ApiClient.new(Petstore::Configuration.new)
# always delete and then re-create the pet object with 10002 # always delete and then re-create the pet object with 10002
def prepare_pet(pet_api) def prepare_pet(pet_api)