forked from loafle/openapi-generator-original
Merge pull request #783 from xhh/ruby-auth
[Ruby] Add authentication support (API key, HTTP basic)
This commit is contained in:
@@ -36,7 +36,7 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
moduleName = generateModuleName();
|
||||
modelPackage = gemName + "/models";
|
||||
apiPackage = gemName + "/api";
|
||||
outputFolder = "generated-code/ruby";
|
||||
outputFolder = "generated-code" + File.separatorChar + "ruby";
|
||||
modelTemplateFiles.put("model.mustache", ".rb");
|
||||
apiTemplateFiles.put("api.mustache", ".rb");
|
||||
templateDir = "ruby";
|
||||
@@ -69,17 +69,17 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
typeMapping.put("List", "array");
|
||||
typeMapping.put("map", "map");
|
||||
|
||||
String baseFolder = "lib/" + gemName;
|
||||
String swaggerFolder = baseFolder + "/swagger";
|
||||
String modelFolder = baseFolder + "/models";
|
||||
String baseFolder = "lib" + File.separatorChar + gemName;
|
||||
String swaggerFolder = baseFolder + File.separatorChar + "swagger";
|
||||
String modelFolder = baseFolder + File.separatorChar + "models";
|
||||
supportingFiles.add(new SupportingFile("swagger_client.gemspec.mustache", "", gemName + ".gemspec"));
|
||||
supportingFiles.add(new SupportingFile("swagger_client.mustache", "lib", gemName + ".rb"));
|
||||
supportingFiles.add(new SupportingFile("monkey.mustache", baseFolder, "monkey.rb"));
|
||||
supportingFiles.add(new SupportingFile("swagger.mustache", baseFolder, "swagger.rb"));
|
||||
supportingFiles.add(new SupportingFile("swagger/request.mustache", swaggerFolder, "request.rb"));
|
||||
supportingFiles.add(new SupportingFile("swagger/response.mustache", swaggerFolder, "response.rb"));
|
||||
supportingFiles.add(new SupportingFile("swagger/version.mustache", swaggerFolder, "version.rb"));
|
||||
supportingFiles.add(new SupportingFile("swagger/configuration.mustache", swaggerFolder, "configuration.rb"));
|
||||
supportingFiles.add(new SupportingFile("swagger" + File.separatorChar + "request.mustache", swaggerFolder, "request.rb"));
|
||||
supportingFiles.add(new SupportingFile("swagger" + File.separatorChar + "response.mustache", swaggerFolder, "response.rb"));
|
||||
supportingFiles.add(new SupportingFile("swagger" + File.separatorChar + "version.mustache", swaggerFolder, "version.rb"));
|
||||
supportingFiles.add(new SupportingFile("swagger" + File.separatorChar + "configuration.mustache", swaggerFolder, "configuration.rb"));
|
||||
supportingFiles.add(new SupportingFile("base_object.mustache", modelFolder, "base_object.rb"));
|
||||
}
|
||||
|
||||
|
||||
@@ -50,8 +50,9 @@ module {{moduleName}}
|
||||
{{/bodyParam}}{{#bodyParam}}post_body = Swagger::Request.object_to_http_body({{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}})
|
||||
{{/bodyParam}}
|
||||
|
||||
{{#returnType}}response = Swagger::Request.new(:{{httpMethod}}, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body}).make.body
|
||||
{{#returnContainer}}response.map {|response| {{/returnContainer}}obj = {{returnBaseType}}.new() and obj.build_from_hash(response){{#returnContainer}} }{{/returnContainer}}{{/returnType}}{{^returnType}}Swagger::Request.new(:{{httpMethod}}, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body}).make
|
||||
auth_names = [{{#authMethods}}'{{name}}'{{#hasMore}}, {{/hasMore}}{{/authMethods}}]
|
||||
{{#returnType}}response = Swagger::Request.new(:{{httpMethod}}, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make.body
|
||||
{{#returnContainer}}response.map {|response| {{/returnContainer}}obj = {{returnBaseType}}.new() and obj.build_from_hash(response){{#returnContainer}} }{{/returnContainer}}{{/returnType}}{{^returnType}}Swagger::Request.new(:{{httpMethod}}, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make
|
||||
nil{{/returnType}}
|
||||
end
|
||||
{{/operation}}
|
||||
|
||||
@@ -16,9 +16,9 @@ module {{moduleName}}
|
||||
#
|
||||
# @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
|
||||
#
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
module {{moduleName}}
|
||||
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, :verify_ssl
|
||||
|
||||
# Defaults go in here..
|
||||
def initialize
|
||||
@format = 'json'
|
||||
@@ -13,6 +13,16 @@ module {{moduleName}}
|
||||
@inject_format = false
|
||||
@force_ending_format = false
|
||||
@camelize_params = true
|
||||
|
||||
# keys for API key authentication (param-name => api-key)
|
||||
@api_key = {}
|
||||
# api-key prefix for API key authentication, e.g. "Bearer" (param-name => api-key-prefix)
|
||||
@api_key_prefix = {}
|
||||
|
||||
# whether to verify SSL certificate, default to true
|
||||
# Note: do NOT set it to false in production code, otherwise you would
|
||||
# face multiple types of cryptographic attacks
|
||||
@verify_ssl = true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -5,7 +5,7 @@ module {{moduleName}}
|
||||
require 'addressable/uri'
|
||||
require 'typhoeus'
|
||||
|
||||
attr_accessor :host, :path, :format, :params, :body, :http_method, :headers, :form_params
|
||||
attr_accessor :host, :path, :format, :params, :body, :http_method, :headers, :form_params, :auth_names
|
||||
|
||||
# All requests must have an HTTP method and a path
|
||||
# Optionals parameters are :params, :headers, :body, :format, :host
|
||||
@@ -16,21 +16,9 @@ module {{moduleName}}
|
||||
# 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] || {})
|
||||
|
||||
@@ -44,6 +32,33 @@ module {{moduleName}}
|
||||
attributes.each do |name, value|
|
||||
send("#{name.to_s.underscore.to_sym}=", value)
|
||||
end
|
||||
|
||||
update_params_for_auth!
|
||||
end
|
||||
|
||||
# Update hearder and query params based on authentication settings.
|
||||
def update_params_for_auth!
|
||||
(@auth_names || []).each do |auth_name|
|
||||
case auth_name
|
||||
{{#authMethods}}when '{{name}}'
|
||||
{{#isApiKey}}{{#isKeyInHeader}}@headers ||= {}
|
||||
@headers['{{keyParamName}}'] = get_api_key_with_prefix('{{keyParamName}}'){{/isKeyInHeader}}{{#isKeyInQuery}}@params ||= {}
|
||||
@params['{{keyParamName}}'] = get_api_key_with_prefix('{{keyParamName}}'){{/isKeyInQuery}}{{/isApiKey}}{{#isBasic}}@headers ||= {}
|
||||
http_auth_header = 'Basic ' + ["#{Swagger.configuration.username}:#{Swagger.configuration.password}"].pack('m').delete("\r\n")
|
||||
@headers['Authorization'] = http_auth_header{{/isBasic}}{{#isOAuth}}# TODO: support oauth{{/isOAuth}}
|
||||
{{/authMethods}}
|
||||
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 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
|
||||
@@ -58,9 +73,6 @@ module {{moduleName}}
|
||||
# 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
|
||||
|
||||
@@ -108,14 +120,16 @@ module {{moduleName}}
|
||||
# For form parameters, remove empty value
|
||||
def outgoing_body
|
||||
# http form
|
||||
if @body.nil? && @form_params && !@form_params.empty?
|
||||
if headers['Content-Type'] == 'application/x-www-form-urlencoded'
|
||||
data = form_params.dup
|
||||
data.each do |key, value|
|
||||
data[key] = value.to_s if value && !value.is_a?(File) # remove emtpy form parameter
|
||||
end
|
||||
data
|
||||
else # http body is JSON
|
||||
elsif @body # http body is JSON
|
||||
@body.is_a?(String) ? @body : @body.to_json
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
@@ -129,7 +143,7 @@ module {{moduleName}}
|
||||
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
|
||||
@@ -148,39 +162,40 @@ module {{moduleName}}
|
||||
#TODO use configuration setting to determine if debugging
|
||||
#logger = Logger.new STDOUT
|
||||
#logger.debug self.url
|
||||
|
||||
request_options = {
|
||||
:ssl_verifypeer => Swagger.configuration.verify_ssl,
|
||||
:headers => self.headers.stringify_keys
|
||||
}
|
||||
response = case self.http_method.to_sym
|
||||
when :get,:GET
|
||||
Typhoeus::Request.get(
|
||||
self.url,
|
||||
:headers => self.headers.stringify_keys,
|
||||
request_options
|
||||
)
|
||||
|
||||
when :post,:POST
|
||||
Typhoeus::Request.post(
|
||||
self.url,
|
||||
:body => self.outgoing_body,
|
||||
:headers => self.headers.stringify_keys,
|
||||
request_options.merge(:body => self.outgoing_body)
|
||||
)
|
||||
|
||||
when :patch,:PATCH
|
||||
Typhoeus::Request.patch(
|
||||
self.url,
|
||||
:body => self.outgoing_body,
|
||||
:headers => self.headers.stringify_keys,
|
||||
request_options.merge(:body => self.outgoing_body)
|
||||
)
|
||||
|
||||
when :put,:PUT
|
||||
Typhoeus::Request.put(
|
||||
self.url,
|
||||
:body => self.outgoing_body,
|
||||
:headers => self.headers.stringify_keys,
|
||||
request_options.merge(:body => self.outgoing_body)
|
||||
)
|
||||
|
||||
when :delete,:DELETE
|
||||
Typhoeus::Request.delete(
|
||||
self.url,
|
||||
:body => self.outgoing_body,
|
||||
:headers => self.headers.stringify_keys,
|
||||
request_options.merge(:body => self.outgoing_body)
|
||||
)
|
||||
end
|
||||
Response.new(response)
|
||||
|
||||
@@ -37,7 +37,8 @@ module SwaggerClient
|
||||
post_body = Swagger::Request.object_to_http_body(opts[:'body'])
|
||||
|
||||
|
||||
Swagger::Request.new(:PUT, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body}).make
|
||||
auth_names = ['petstore_auth']
|
||||
Swagger::Request.new(:PUT, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make
|
||||
nil
|
||||
end
|
||||
|
||||
@@ -73,7 +74,8 @@ module SwaggerClient
|
||||
post_body = Swagger::Request.object_to_http_body(opts[:'body'])
|
||||
|
||||
|
||||
Swagger::Request.new(:POST, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body}).make
|
||||
auth_names = ['petstore_auth']
|
||||
Swagger::Request.new(:POST, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make
|
||||
nil
|
||||
end
|
||||
|
||||
@@ -110,7 +112,8 @@ module SwaggerClient
|
||||
post_body = nil
|
||||
|
||||
|
||||
response = Swagger::Request.new(:GET, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body}).make.body
|
||||
auth_names = ['petstore_auth']
|
||||
response = Swagger::Request.new(:GET, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make.body
|
||||
response.map {|response| obj = Pet.new() and obj.build_from_hash(response) }
|
||||
end
|
||||
|
||||
@@ -147,7 +150,8 @@ module SwaggerClient
|
||||
post_body = nil
|
||||
|
||||
|
||||
response = Swagger::Request.new(:GET, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body}).make.body
|
||||
auth_names = ['petstore_auth']
|
||||
response = Swagger::Request.new(:GET, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make.body
|
||||
response.map {|response| obj = Pet.new() and obj.build_from_hash(response) }
|
||||
end
|
||||
|
||||
@@ -186,7 +190,8 @@ module SwaggerClient
|
||||
post_body = nil
|
||||
|
||||
|
||||
response = Swagger::Request.new(:GET, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body}).make.body
|
||||
auth_names = ['api_key', 'petstore_auth']
|
||||
response = Swagger::Request.new(:GET, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make.body
|
||||
obj = Pet.new() and obj.build_from_hash(response)
|
||||
end
|
||||
|
||||
@@ -229,7 +234,8 @@ module SwaggerClient
|
||||
post_body = nil
|
||||
|
||||
|
||||
Swagger::Request.new(:POST, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body}).make
|
||||
auth_names = ['petstore_auth']
|
||||
Swagger::Request.new(:POST, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make
|
||||
nil
|
||||
end
|
||||
|
||||
@@ -270,7 +276,8 @@ module SwaggerClient
|
||||
post_body = nil
|
||||
|
||||
|
||||
Swagger::Request.new(:DELETE, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body}).make
|
||||
auth_names = ['petstore_auth']
|
||||
Swagger::Request.new(:DELETE, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make
|
||||
nil
|
||||
end
|
||||
|
||||
@@ -313,7 +320,8 @@ module SwaggerClient
|
||||
post_body = nil
|
||||
|
||||
|
||||
Swagger::Request.new(:POST, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body}).make
|
||||
auth_names = ['petstore_auth']
|
||||
Swagger::Request.new(:POST, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
@@ -36,7 +36,8 @@ module SwaggerClient
|
||||
post_body = nil
|
||||
|
||||
|
||||
response = Swagger::Request.new(:GET, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body}).make.body
|
||||
auth_names = ['api_key']
|
||||
response = Swagger::Request.new(:GET, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make.body
|
||||
response.map {|response| obj = map.new() and obj.build_from_hash(response) }
|
||||
end
|
||||
|
||||
@@ -72,7 +73,8 @@ module SwaggerClient
|
||||
post_body = Swagger::Request.object_to_http_body(opts[:'body'])
|
||||
|
||||
|
||||
response = Swagger::Request.new(:POST, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body}).make.body
|
||||
auth_names = []
|
||||
response = Swagger::Request.new(:POST, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make.body
|
||||
obj = Order.new() and obj.build_from_hash(response)
|
||||
end
|
||||
|
||||
@@ -111,7 +113,8 @@ module SwaggerClient
|
||||
post_body = nil
|
||||
|
||||
|
||||
response = Swagger::Request.new(:GET, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body}).make.body
|
||||
auth_names = []
|
||||
response = Swagger::Request.new(:GET, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make.body
|
||||
obj = Order.new() and obj.build_from_hash(response)
|
||||
end
|
||||
|
||||
@@ -150,7 +153,8 @@ module SwaggerClient
|
||||
post_body = nil
|
||||
|
||||
|
||||
Swagger::Request.new(:DELETE, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body}).make
|
||||
auth_names = []
|
||||
Swagger::Request.new(:DELETE, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
@@ -37,7 +37,8 @@ module SwaggerClient
|
||||
post_body = Swagger::Request.object_to_http_body(opts[:'body'])
|
||||
|
||||
|
||||
Swagger::Request.new(:POST, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body}).make
|
||||
auth_names = []
|
||||
Swagger::Request.new(:POST, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make
|
||||
nil
|
||||
end
|
||||
|
||||
@@ -73,7 +74,8 @@ module SwaggerClient
|
||||
post_body = Swagger::Request.object_to_http_body(opts[:'body'])
|
||||
|
||||
|
||||
Swagger::Request.new(:POST, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body}).make
|
||||
auth_names = []
|
||||
Swagger::Request.new(:POST, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make
|
||||
nil
|
||||
end
|
||||
|
||||
@@ -109,7 +111,8 @@ module SwaggerClient
|
||||
post_body = Swagger::Request.object_to_http_body(opts[:'body'])
|
||||
|
||||
|
||||
Swagger::Request.new(:POST, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body}).make
|
||||
auth_names = []
|
||||
Swagger::Request.new(:POST, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make
|
||||
nil
|
||||
end
|
||||
|
||||
@@ -148,7 +151,8 @@ module SwaggerClient
|
||||
post_body = nil
|
||||
|
||||
|
||||
response = Swagger::Request.new(:GET, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body}).make.body
|
||||
auth_names = []
|
||||
response = Swagger::Request.new(:GET, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make.body
|
||||
obj = string.new() and obj.build_from_hash(response)
|
||||
end
|
||||
|
||||
@@ -183,7 +187,8 @@ module SwaggerClient
|
||||
post_body = nil
|
||||
|
||||
|
||||
Swagger::Request.new(:GET, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body}).make
|
||||
auth_names = []
|
||||
Swagger::Request.new(:GET, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make
|
||||
nil
|
||||
end
|
||||
|
||||
@@ -222,7 +227,8 @@ module SwaggerClient
|
||||
post_body = nil
|
||||
|
||||
|
||||
response = Swagger::Request.new(:GET, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body}).make.body
|
||||
auth_names = []
|
||||
response = Swagger::Request.new(:GET, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make.body
|
||||
obj = User.new() and obj.build_from_hash(response)
|
||||
end
|
||||
|
||||
@@ -262,7 +268,8 @@ module SwaggerClient
|
||||
post_body = Swagger::Request.object_to_http_body(opts[:'body'])
|
||||
|
||||
|
||||
Swagger::Request.new(:PUT, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body}).make
|
||||
auth_names = []
|
||||
Swagger::Request.new(:PUT, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make
|
||||
nil
|
||||
end
|
||||
|
||||
@@ -301,7 +308,8 @@ module SwaggerClient
|
||||
post_body = nil
|
||||
|
||||
|
||||
Swagger::Request.new(:DELETE, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body}).make
|
||||
auth_names = []
|
||||
Swagger::Request.new(:DELETE, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
@@ -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
|
||||
#
|
||||
|
||||
@@ -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, :verify_ssl
|
||||
|
||||
# Defaults go in here..
|
||||
def initialize
|
||||
@format = 'json'
|
||||
@@ -13,6 +13,16 @@ module SwaggerClient
|
||||
@inject_format = false
|
||||
@force_ending_format = false
|
||||
@camelize_params = true
|
||||
|
||||
# keys for API key authentication (param-name => api-key)
|
||||
@api_key = {}
|
||||
# api-key prefix for API key authentication, e.g. "Bearer" (param-name => api-key-prefix)
|
||||
@api_key_prefix = {}
|
||||
|
||||
# whether to verify SSL certificate, default to true
|
||||
# Note: do NOT set it to false in production code, otherwise you would
|
||||
# face multiple types of cryptographic attacks
|
||||
@verify_ssl = true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -5,7 +5,7 @@ module SwaggerClient
|
||||
require 'addressable/uri'
|
||||
require 'typhoeus'
|
||||
|
||||
attr_accessor :host, :path, :format, :params, :body, :http_method, :headers, :form_params
|
||||
attr_accessor :host, :path, :format, :params, :body, :http_method, :headers, :form_params, :auth_names
|
||||
|
||||
# All requests must have an HTTP method and a path
|
||||
# Optionals parameters are :params, :headers, :body, :format, :host
|
||||
@@ -16,21 +16,9 @@ module SwaggerClient
|
||||
# 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] || {})
|
||||
|
||||
@@ -44,6 +32,32 @@ module SwaggerClient
|
||||
attributes.each do |name, value|
|
||||
send("#{name.to_s.underscore.to_sym}=", value)
|
||||
end
|
||||
|
||||
update_params_for_auth!
|
||||
end
|
||||
|
||||
# Update hearder and query params based on authentication settings.
|
||||
def update_params_for_auth!
|
||||
(@auth_names || []).each do |auth_name|
|
||||
case auth_name
|
||||
when 'api_key'
|
||||
@headers ||= {}
|
||||
@headers['api_key'] = get_api_key_with_prefix('api_key')
|
||||
when 'petstore_auth'
|
||||
# TODO: support oauth
|
||||
|
||||
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 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
|
||||
@@ -58,9 +72,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
|
||||
|
||||
@@ -108,14 +119,16 @@ module SwaggerClient
|
||||
# For form parameters, remove empty value
|
||||
def outgoing_body
|
||||
# http form
|
||||
if @body.nil? && @form_params && !@form_params.empty?
|
||||
if headers['Content-Type'] == 'application/x-www-form-urlencoded'
|
||||
data = form_params.dup
|
||||
data.each do |key, value|
|
||||
data[key] = value.to_s if value && !value.is_a?(File) # remove emtpy form parameter
|
||||
end
|
||||
data
|
||||
else # http body is JSON
|
||||
elsif @body # http body is JSON
|
||||
@body.is_a?(String) ? @body : @body.to_json
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
@@ -129,7 +142,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
|
||||
@@ -148,39 +161,40 @@ module SwaggerClient
|
||||
#TODO use configuration setting to determine if debugging
|
||||
#logger = Logger.new STDOUT
|
||||
#logger.debug self.url
|
||||
|
||||
request_options = {
|
||||
:ssl_verifypeer => Swagger.configuration.verify_ssl,
|
||||
:headers => self.headers.stringify_keys
|
||||
}
|
||||
response = case self.http_method.to_sym
|
||||
when :get,:GET
|
||||
Typhoeus::Request.get(
|
||||
self.url,
|
||||
:headers => self.headers.stringify_keys,
|
||||
request_options
|
||||
)
|
||||
|
||||
when :post,:POST
|
||||
Typhoeus::Request.post(
|
||||
self.url,
|
||||
:body => self.outgoing_body,
|
||||
:headers => self.headers.stringify_keys,
|
||||
request_options.merge(:body => self.outgoing_body)
|
||||
)
|
||||
|
||||
when :patch,:PATCH
|
||||
Typhoeus::Request.patch(
|
||||
self.url,
|
||||
:body => self.outgoing_body,
|
||||
:headers => self.headers.stringify_keys,
|
||||
request_options.merge(:body => self.outgoing_body)
|
||||
)
|
||||
|
||||
when :put,:PUT
|
||||
Typhoeus::Request.put(
|
||||
self.url,
|
||||
:body => self.outgoing_body,
|
||||
:headers => self.headers.stringify_keys,
|
||||
request_options.merge(:body => self.outgoing_body)
|
||||
)
|
||||
|
||||
when :delete,:DELETE
|
||||
Typhoeus::Request.delete(
|
||||
self.url,
|
||||
:body => self.outgoing_body,
|
||||
:headers => self.headers.stringify_keys,
|
||||
request_options.merge(:body => self.outgoing_body)
|
||||
)
|
||||
end
|
||||
Response.new(response)
|
||||
|
||||
@@ -2,10 +2,10 @@ require 'spec_helper'
|
||||
|
||||
describe SwaggerClient::Swagger::Request do
|
||||
|
||||
before(:each) do
|
||||
before(:each) do
|
||||
SwaggerClient::Swagger.configure do |config|
|
||||
inject_format = true
|
||||
config.api_key = 'special-key'
|
||||
config.api_key['api_key'] = 'special-key'
|
||||
config.host = 'petstore.swagger.io'
|
||||
config.base_path = '/v2'
|
||||
end
|
||||
@@ -22,7 +22,7 @@ describe SwaggerClient::Swagger::Request do
|
||||
it "sets default response format to json" do
|
||||
@request.format.should == 'json'
|
||||
end
|
||||
|
||||
|
||||
it "allows params to be nil" do
|
||||
@request = SwaggerClient::Swagger::Request.new(@default_http_method, @default_path, :params => nil)
|
||||
@request.query_string.should == ""
|
||||
@@ -55,7 +55,7 @@ describe SwaggerClient::Swagger::Request do
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
describe "body" do
|
||||
|
||||
it "camelCases parameters" do
|
||||
@@ -67,7 +67,7 @@ describe SwaggerClient::Swagger::Request do
|
||||
}))
|
||||
@request.body.keys.should == [:badDog, :goodDog]
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
describe "path" do
|
||||
@@ -140,7 +140,7 @@ describe SwaggerClient::Swagger::Request do
|
||||
@request.query_string.should =~ /\?limit=100/
|
||||
@request.url.should =~ /\?limit=100/
|
||||
end
|
||||
|
||||
|
||||
it "camelCases parameters" do
|
||||
@request = SwaggerClient::Swagger::Request.new(@default_http_method, @default_path, @default_params.merge({
|
||||
:params => {
|
||||
@@ -150,52 +150,35 @@ describe SwaggerClient::Swagger::Request do
|
||||
}))
|
||||
@request.query_string.should == "?badDog=bud&goodDog=dud"
|
||||
end
|
||||
|
||||
|
||||
it "converts boolean values to their string representation" do
|
||||
params = {:stringy => "fish", :truthy => true, :falsey => false}
|
||||
@request = SwaggerClient::Swagger::Request.new(:get, 'fakeMethod', :params => params)
|
||||
@request.query_string.should == "?falsey=false&stringy=fish&truthy=true"
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
describe "API key" do
|
||||
|
||||
it "is inferred from the Swagger base configuration by default" do
|
||||
SwaggerClient::Swagger.configure {|c| c.api_key = "xyz" }
|
||||
SwaggerClient::Swagger::Request.new(:get, "word/json").headers[:api_key].should == "xyz"
|
||||
end
|
||||
|
||||
it "can be obfuscated for public display" do
|
||||
@request = SwaggerClient::Swagger::Request.new(:get, "words/fancy", @default_params.merge({
|
||||
:params => {
|
||||
:word => "dog",
|
||||
:api_key => "123456"
|
||||
}
|
||||
}))
|
||||
|
||||
@request.url.should =~ /api\_key=123456/
|
||||
@request.url(:obfuscated => true).should =~ /api\_key=YOUR\_API\_KEY/
|
||||
describe "#update_params_for_auth!" do
|
||||
it "sets header api-key parameter with prefix" do
|
||||
SwaggerClient::Swagger.configure do |config|
|
||||
inject_format = true
|
||||
config.api_key_prefix['api_key'] = 'PREFIX'
|
||||
end
|
||||
@request.auth_names = ['api_key', 'unknown']
|
||||
@request.update_params_for_auth!
|
||||
@request.headers['api_key'].should == 'PREFIX special-key'
|
||||
end
|
||||
|
||||
it "allows a key in the params to override the configuration-level key, even if it's blank" do
|
||||
SwaggerClient::Swagger.configure {|c| c.api_key = "abc" }
|
||||
@request_with_key = SwaggerClient::Swagger::Request.new(:get, "word/json", :params => {:api_key => "jkl"})
|
||||
@request_with_key.headers[:api_key].should be_nil
|
||||
@request_with_key.params[:api_key].should == "jkl"
|
||||
|
||||
@request_without_key = SwaggerClient::Swagger::Request.new(:get, "word/json", :params => {:api_key => nil})
|
||||
@request_without_key.headers[:api_key].should be_nil
|
||||
@request_without_key.params[:api_key].should be_nil
|
||||
it "sets header api-key parameter without prefix" do
|
||||
SwaggerClient::Swagger.configure do |config|
|
||||
inject_format = true
|
||||
config.api_key_prefix['api_key'] = nil
|
||||
end
|
||||
@request.auth_names = ['api_key', 'unknown']
|
||||
@request.update_params_for_auth!
|
||||
@request.headers['api_key'].should == 'special-key'
|
||||
end
|
||||
|
||||
it "allows a key in the headers to override the configuration-level key, even if it's blank" do
|
||||
SwaggerClient::Swagger.configure {|c| c.api_key = "hij" }
|
||||
SwaggerClient::Swagger::Request.new(:get, "word/json").headers[:api_key].should == "hij"
|
||||
SwaggerClient::Swagger::Request.new(:get, "word/json", :headers => {:api_key => "jkl"}).headers[:api_key].should == "jkl"
|
||||
SwaggerClient::Swagger::Request.new(:get, "word/json", :headers => {:api_key => nil}).headers[:api_key].should be_nil
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -38,7 +38,7 @@ end
|
||||
|
||||
def configure_swagger
|
||||
SwaggerClient::Swagger.configure do |config|
|
||||
config.api_key = 'special-key'
|
||||
config.api_key['api_key'] = 'special-key'
|
||||
config.host = 'petstore.swagger.io'
|
||||
config.base_path = '/v2'
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user