diff --git a/samples/client/petstore/ruby/lib/swagger_client/swagger.rb b/samples/client/petstore/ruby/lib/swagger_client/swagger.rb index 278db36e4ca..2e2632c169d 100644 --- a/samples/client/petstore/ruby/lib/swagger_client/swagger.rb +++ b/samples/client/petstore/ruby/lib/swagger_client/swagger.rb @@ -16,9 +16,9 @@ module SwaggerClient # # @example # Swagger.configure do |config| - # config.api_key = '1234567890abcdef' # required - # config.username = 'wordlover' # optional, but needed for user-related functions - # config.password = 'i<3words' # optional, but needed for user-related functions + # config.api_key['api_key'] = '1234567890abcdef' # api key authentication + # config.username = 'wordlover' # http basic authentication + # config.password = 'i<3words' # http basic authentication # config.format = 'json' # optional, defaults to 'json' # end # diff --git a/samples/client/petstore/ruby/lib/swagger_client/swagger/configuration.rb b/samples/client/petstore/ruby/lib/swagger_client/swagger/configuration.rb index 51f9539a9ab..91cdb66e099 100644 --- a/samples/client/petstore/ruby/lib/swagger_client/swagger/configuration.rb +++ b/samples/client/petstore/ruby/lib/swagger_client/swagger/configuration.rb @@ -1,8 +1,8 @@ module SwaggerClient module Swagger 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.. def initialize @format = 'json' @@ -13,6 +13,8 @@ module SwaggerClient @inject_format = false @force_ending_format = false @camelize_params = true + @api_key = {} + @api_key_prefix = {} end end end diff --git a/samples/client/petstore/ruby/lib/swagger_client/swagger/request.rb b/samples/client/petstore/ruby/lib/swagger_client/swagger/request.rb index d5f266267da..0dd1757167c 100644 --- a/samples/client/petstore/ruby/lib/swagger_client/swagger/request.rb +++ b/samples/client/petstore/ruby/lib/swagger_client/swagger/request.rb @@ -13,24 +13,14 @@ module SwaggerClient attributes[:format] ||= Swagger.configuration.format attributes[:params] ||= {} + update_params_for_auth(attributes) + # Set default headers default_headers = { 'Content-Type' => "application/#{attributes[:format].downcase}", - :api_key => Swagger.configuration.api_key, '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 attributes[:headers] = default_headers.merge(attributes[:headers] || {}) @@ -46,6 +36,33 @@ module SwaggerClient 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 def url(options = {}) u = Addressable::URI.new( @@ -58,9 +75,6 @@ module SwaggerClient # Drop trailing question mark, if present u.sub! /\?$/, '' - # Obfuscate API key? - u.sub! /api\_key=\w+/, 'api_key=YOUR_API_KEY' if options[:obfuscated] - u end @@ -129,7 +143,7 @@ module SwaggerClient next if self.path.include? "{#{key}}" # skip path params next if value.blank? && value.class != FalseClass # skip empties 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 query_values[key] = value.to_s end