mirror of
https://github.com/jlengrand/openapi-generator.git
synced 2026-05-12 08:31:21 +00:00
Ruby client: allow setting Configuration in ApiClient
Removed the singleton design from the Configuration class. Added a `config` field to ApiClient to hold the settings the ApiClient uses.
This commit is contained in:
@@ -5,8 +5,8 @@ module {{moduleName}}
|
||||
class {{classname}}
|
||||
attr_accessor :api_client
|
||||
|
||||
def initialize(api_client = nil)
|
||||
@api_client = api_client || Configuration.api_client
|
||||
def initialize(api_client = ApiClient.default)
|
||||
@api_client = api_client
|
||||
end
|
||||
{{#operation}}
|
||||
{{newline}}
|
||||
@@ -28,8 +28,8 @@ module {{moduleName}}
|
||||
{{#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
|
||||
def {{operationId}}_with_http_info({{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}opts = {})
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "Calling API: {{classname}}#{{operationId}} ..."
|
||||
if @api_client.config.debugging
|
||||
@api_client.config.logger.debug "Calling API: {{classname}}#{{operationId}} ..."
|
||||
end
|
||||
{{#allParams}}{{#required}}
|
||||
# verify the required parameter '{{paramName}}' is set
|
||||
@@ -81,8 +81,8 @@ module {{moduleName}}
|
||||
:body => post_body,
|
||||
:auth_names => auth_names{{#returnType}},
|
||||
:return_type => '{{{returnType}}}'{{/returnType}})
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "API called: {{classname}}#{{operationId}}\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
|
||||
if @api_client.config.debugging
|
||||
@api_client.config.logger.debug "API called: {{classname}}#{{operationId}}\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
|
||||
end
|
||||
return data, status_code, headers
|
||||
end
|
||||
|
||||
@@ -7,24 +7,27 @@ require 'uri'
|
||||
|
||||
module {{moduleName}}
|
||||
class ApiClient
|
||||
|
||||
attr_accessor :host
|
||||
# The Configuration object holding settings to be used in the API client.
|
||||
attr_accessor :config
|
||||
|
||||
# Defines the headers to be used in HTTP requests of all API calls by default.
|
||||
#
|
||||
# @return [Hash]
|
||||
attr_accessor :default_headers
|
||||
|
||||
def initialize(host = nil)
|
||||
@host = host || Configuration.base_url
|
||||
@format = 'json'
|
||||
def initialize(config = Configuration.default)
|
||||
@config = config
|
||||
@user_agent = "ruby-swagger-#{VERSION}"
|
||||
@default_headers = {
|
||||
'Content-Type' => "application/#{@format.downcase}",
|
||||
'Content-Type' => "application/json",
|
||||
'User-Agent' => @user_agent
|
||||
}
|
||||
end
|
||||
|
||||
def self.default
|
||||
@@default ||= ApiClient.new
|
||||
end
|
||||
|
||||
# Call an API with given options.
|
||||
#
|
||||
# @return [Array<(Object, Fixnum, Hash)>] an array of 3 elements:
|
||||
@@ -33,8 +36,8 @@ module {{moduleName}}
|
||||
request = build_request(http_method, path, opts)
|
||||
response = request.run
|
||||
|
||||
if Configuration.debugging
|
||||
Configuration.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
|
||||
if @config.debugging
|
||||
@config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
|
||||
end
|
||||
|
||||
unless response.success?
|
||||
@@ -68,18 +71,18 @@ module {{moduleName}}
|
||||
:method => http_method,
|
||||
:headers => header_params,
|
||||
:params => query_params,
|
||||
:ssl_verifypeer => Configuration.verify_ssl,
|
||||
:sslcert => Configuration.cert_file,
|
||||
:sslkey => Configuration.key_file,
|
||||
:cainfo => Configuration.ssl_ca_cert,
|
||||
:verbose => Configuration.debugging
|
||||
:ssl_verifypeer => @config.verify_ssl,
|
||||
:sslcert => @config.cert_file,
|
||||
:sslkey => @config.key_file,
|
||||
:cainfo => @config.ssl_ca_cert,
|
||||
:verbose => @config.debugging
|
||||
}
|
||||
|
||||
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 Configuration.debugging
|
||||
Configuration.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
|
||||
if @config.debugging
|
||||
@config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -168,7 +171,7 @@ module {{moduleName}}
|
||||
# @see Configuration#temp_folder_path
|
||||
# @return [File] the file downloaded
|
||||
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']
|
||||
if content_disposition
|
||||
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
|
||||
@@ -180,15 +183,15 @@ module {{moduleName}}
|
||||
tmp_file.close!
|
||||
|
||||
File.open(path, 'w') { |file| file.write(response.body) }
|
||||
Configuration.logger.info "File written to #{path}. Please move the file to a proper "\
|
||||
"folder for further processing and delete the temp afterwards"
|
||||
@config.logger.info "File written to #{path}. Please move the file to a proper folder "\
|
||||
"for further processing and delete the temp afterwards"
|
||||
File.new(path)
|
||||
end
|
||||
|
||||
def build_request_url(path)
|
||||
# Add leading and trailing slashes to path
|
||||
path = "/#{path}".gsub(/\/+/, '/')
|
||||
URI.encode(host + path)
|
||||
URI.encode(@config.base_url + path)
|
||||
end
|
||||
|
||||
def build_request_body(header_params, form_params, body)
|
||||
@@ -216,7 +219,7 @@ module {{moduleName}}
|
||||
# Update hearder and query params based on authentication settings.
|
||||
def update_params_for_auth!(header_params, query_params, auth_names)
|
||||
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
|
||||
case auth_setting[:in]
|
||||
when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
|
||||
|
||||
@@ -1,14 +1,7 @@
|
||||
require 'uri'
|
||||
require 'singleton'
|
||||
|
||||
module {{moduleName}}
|
||||
class Configuration
|
||||
|
||||
include Singleton
|
||||
|
||||
# Default api client
|
||||
attr_accessor :api_client
|
||||
|
||||
# Defines url scheme
|
||||
attr_accessor :scheme
|
||||
|
||||
@@ -94,17 +87,6 @@ module {{moduleName}}
|
||||
|
||||
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}}'
|
||||
@@ -118,10 +100,17 @@ module {{moduleName}}
|
||||
@inject_format = false
|
||||
@force_ending_format = false
|
||||
@logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
|
||||
|
||||
yield(self) if block_given?
|
||||
end
|
||||
|
||||
def api_client
|
||||
@api_client ||= ApiClient.new
|
||||
# The default Configuration object.
|
||||
def self.default
|
||||
@@default ||= Configuration.new
|
||||
end
|
||||
|
||||
def configure
|
||||
yield(self) if block_given?
|
||||
end
|
||||
|
||||
def scheme=(scheme)
|
||||
|
||||
@@ -19,17 +19,17 @@ require '{{importPath}}'
|
||||
|
||||
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.
|
||||
# Customize default settings for the SDK using block.
|
||||
# {{moduleName}}.configure do |config|
|
||||
# config.username = "xxx"
|
||||
# config.password = "xxx"
|
||||
# end
|
||||
# If no block given, return the default Configuration object.
|
||||
def configure
|
||||
if block_given?
|
||||
yield Configuration.instance
|
||||
yield(Configuration.default)
|
||||
else
|
||||
Configuration.instance
|
||||
Configuration.default
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user