Regenerate Ruby Petstore sample

This commit is contained in:
xhh 2015-05-23 09:41:03 +08:00
parent 95813714a5
commit e52694cde0
3 changed files with 37 additions and 21 deletions

View File

@ -16,9 +16,9 @@ module SwaggerClient
# #
# @example # @example
# Swagger.configure do |config| # Swagger.configure do |config|
# config.api_key = '1234567890abcdef' # required # config.api_key['api_key'] = '1234567890abcdef' # api key authentication
# config.username = 'wordlover' # optional, but needed for user-related functions # config.username = 'wordlover' # http basic authentication
# config.password = 'i<3words' # optional, but needed for user-related functions # config.password = 'i<3words' # http basic authentication
# config.format = 'json' # optional, defaults to 'json' # config.format = 'json' # optional, defaults to 'json'
# end # end
# #

View File

@ -1,7 +1,7 @@
module SwaggerClient module SwaggerClient
module Swagger module Swagger
class Configuration class Configuration
attr_accessor :format, :api_key, :username, :password, :auth_token, :scheme, :host, :base_path, :user_agent, :logger, :inject_format, :force_ending_format, :camelize_params, :user_agent attr_accessor :format, :api_key, :api_key_prefix, :username, :password, :auth_token, :scheme, :host, :base_path, :user_agent, :logger, :inject_format, :force_ending_format, :camelize_params, :user_agent
# Defaults go in here.. # Defaults go in here..
def initialize def initialize
@ -13,6 +13,8 @@ module SwaggerClient
@inject_format = false @inject_format = false
@force_ending_format = false @force_ending_format = false
@camelize_params = true @camelize_params = true
@api_key = {}
@api_key_prefix = {}
end end
end end
end end

View File

@ -13,24 +13,14 @@ module SwaggerClient
attributes[:format] ||= Swagger.configuration.format attributes[:format] ||= Swagger.configuration.format
attributes[:params] ||= {} attributes[:params] ||= {}
update_params_for_auth(attributes)
# Set default headers # Set default headers
default_headers = { default_headers = {
'Content-Type' => "application/#{attributes[:format].downcase}", 'Content-Type' => "application/#{attributes[:format].downcase}",
:api_key => Swagger.configuration.api_key,
'User-Agent' => Swagger.configuration.user_agent 'User-Agent' => Swagger.configuration.user_agent
} }
# api_key from headers hash trumps the default, even if its value is blank
if attributes[:headers].present? && attributes[:headers].has_key?(:api_key)
default_headers.delete(:api_key)
end
# api_key from params hash trumps all others (headers and default_headers)
if attributes[:params].present? && attributes[:params].has_key?(:api_key)
default_headers.delete(:api_key)
attributes[:headers].delete(:api_key) if attributes[:headers].present?
end
# Merge argument headers into defaults # Merge argument headers into defaults
attributes[:headers] = default_headers.merge(attributes[:headers] || {}) attributes[:headers] = default_headers.merge(attributes[:headers] || {})
@ -46,6 +36,33 @@ module SwaggerClient
end end
end end
def update_params_for_auth(attributes)
(attributes[:auth_names] || []).each do |auth_name|
case auth_name
when 'api_key'
attributes[:headers] ||= {}
attributes[:headers]['api_key'] = get_api_key_with_prefix('api_key')
when 'petstore_auth'
# TODO: support oauth
end
end
end
def get_api_key_with_prefix(param_name)
if Swagger.configuration.api_key_prefix[param_name].present?
"#{Swagger.configuration.api_key_prefix[param_name]} #{Swagger.configuration.api_key[param_name]}"
else
Swagger.configuration.api_key[param_name]
end
end
# Construct a base URL # Construct a base URL
def url(options = {}) def url(options = {})
u = Addressable::URI.new( u = Addressable::URI.new(
@ -58,9 +75,6 @@ module SwaggerClient
# Drop trailing question mark, if present # Drop trailing question mark, if present
u.sub! /\?$/, '' u.sub! /\?$/, ''
# Obfuscate API key?
u.sub! /api\_key=\w+/, 'api_key=YOUR_API_KEY' if options[:obfuscated]
u u
end end
@ -129,7 +143,7 @@ module SwaggerClient
next if self.path.include? "{#{key}}" # skip path params next if self.path.include? "{#{key}}" # skip path params
next if value.blank? && value.class != FalseClass # skip empties next if value.blank? && value.class != FalseClass # skip empties
if Swagger.configuration.camelize_params if Swagger.configuration.camelize_params
key = key.to_s.camelize(:lower).to_sym unless key.to_sym == :api_key # api_key is not a camelCased param key = key.to_s.camelize(:lower).to_sym
end end
query_values[key] = value.to_s query_values[key] = value.to_s
end end