Skip to content

Commit 2a5b96d

Browse files
committed
Merge pull request #1698 from xhh/ruby-config
Ruby client: allow setting Configuration in ApiClient
2 parents 99c40ae + e9ef143 commit 2a5b96d

File tree

13 files changed

+194
-197
lines changed

13 files changed

+194
-197
lines changed

modules/swagger-codegen/src/main/resources/ruby/api.mustache

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ module {{moduleName}}
55
class {{classname}}
66
attr_accessor :api_client
77

8-
def initialize(api_client = nil)
9-
@api_client = api_client || Configuration.api_client
8+
def initialize(api_client = ApiClient.default)
9+
@api_client = api_client
1010
end
1111
{{#operation}}
1212
{{newline}}
@@ -28,8 +28,8 @@ module {{moduleName}}
2828
{{#allParams}}{{^required}} # @option opts [{{{dataType}}}] :{{paramName}} {{description}}
2929
{{/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
3030
def {{operationId}}_with_http_info({{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}opts = {})
31-
if Configuration.debugging
32-
Configuration.logger.debug "Calling API: {{classname}}#{{operationId}} ..."
31+
if @api_client.config.debugging
32+
@api_client.config.logger.debug "Calling API: {{classname}}#{{operationId}} ..."
3333
end
3434
{{#allParams}}{{#required}}
3535
# verify the required parameter '{{paramName}}' is set
@@ -81,8 +81,8 @@ module {{moduleName}}
8181
:body => post_body,
8282
:auth_names => auth_names{{#returnType}},
8383
:return_type => '{{{returnType}}}'{{/returnType}})
84-
if Configuration.debugging
85-
Configuration.logger.debug "API called: {{classname}}#{{operationId}}\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
84+
if @api_client.config.debugging
85+
@api_client.config.logger.debug "API called: {{classname}}#{{operationId}}\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
8686
end
8787
return data, status_code, headers
8888
end

modules/swagger-codegen/src/main/resources/ruby/api_client.mustache

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,27 @@ require 'uri'
77

88
module {{moduleName}}
99
class ApiClient
10-
11-
attr_accessor :host
10+
# The Configuration object holding settings to be used in the API client.
11+
attr_accessor :config
1212

1313
# Defines the headers to be used in HTTP requests of all API calls by default.
1414
#
1515
# @return [Hash]
1616
attr_accessor :default_headers
1717

18-
def initialize(host = nil)
19-
@host = host || Configuration.base_url
20-
@format = 'json'
18+
def initialize(config = Configuration.default)
19+
@config = config
2120
@user_agent = "ruby-swagger-#{VERSION}"
2221
@default_headers = {
23-
'Content-Type' => "application/#{@format.downcase}",
22+
'Content-Type' => "application/json",
2423
'User-Agent' => @user_agent
2524
}
2625
end
2726

27+
def self.default
28+
@@default ||= ApiClient.new
29+
end
30+
2831
# Call an API with given options.
2932
#
3033
# @return [Array<(Object, Fixnum, Hash)>] an array of 3 elements:
@@ -33,8 +36,8 @@ module {{moduleName}}
3336
request = build_request(http_method, path, opts)
3437
response = request.run
3538

36-
if Configuration.debugging
37-
Configuration.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
39+
if @config.debugging
40+
@config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
3841
end
3942

4043
unless response.success?
@@ -68,18 +71,18 @@ module {{moduleName}}
6871
:method => http_method,
6972
:headers => header_params,
7073
:params => query_params,
71-
:ssl_verifypeer => Configuration.verify_ssl,
72-
:sslcert => Configuration.cert_file,
73-
:sslkey => Configuration.key_file,
74-
:cainfo => Configuration.ssl_ca_cert,
75-
:verbose => Configuration.debugging
74+
:ssl_verifypeer => @config.verify_ssl,
75+
:sslcert => @config.cert_file,
76+
:sslkey => @config.key_file,
77+
:cainfo => @config.ssl_ca_cert,
78+
:verbose => @config.debugging
7679
}
7780

7881
if [:post, :patch, :put, :delete].include?(http_method)
7982
req_body = build_request_body(header_params, form_params, opts[:body])
8083
req_opts.update :body => req_body
81-
if Configuration.debugging
82-
Configuration.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
84+
if @config.debugging
85+
@config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
8386
end
8487
end
8588

@@ -168,7 +171,7 @@ module {{moduleName}}
168171
# @see Configuration#temp_folder_path
169172
# @return [File] the file downloaded
170173
def download_file(response)
171-
tmp_file = Tempfile.new '', Configuration.temp_folder_path
174+
tmp_file = Tempfile.new '', @config.temp_folder_path
172175
content_disposition = response.headers['Content-Disposition']
173176
if content_disposition
174177
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
@@ -180,15 +183,15 @@ module {{moduleName}}
180183
tmp_file.close!
181184

182185
File.open(path, 'w') { |file| file.write(response.body) }
183-
Configuration.logger.info "File written to #{path}. Please move the file to a proper "\
184-
"folder for further processing and delete the temp afterwards"
186+
@config.logger.info "File written to #{path}. Please move the file to a proper folder "\
187+
"for further processing and delete the temp afterwards"
185188
File.new(path)
186189
end
187190

188191
def build_request_url(path)
189192
# Add leading and trailing slashes to path
190193
path = "/#{path}".gsub(/\/+/, '/')
191-
URI.encode(host + path)
194+
URI.encode(@config.base_url + path)
192195
end
193196

194197
def build_request_body(header_params, form_params, body)
@@ -216,7 +219,7 @@ module {{moduleName}}
216219
# Update hearder and query params based on authentication settings.
217220
def update_params_for_auth!(header_params, query_params, auth_names)
218221
Array(auth_names).each do |auth_name|
219-
auth_setting = Configuration.auth_settings[auth_name]
222+
auth_setting = @config.auth_settings[auth_name]
220223
next unless auth_setting
221224
case auth_setting[:in]
222225
when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]

modules/swagger-codegen/src/main/resources/ruby/configuration.mustache

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
require 'uri'
2-
require 'singleton'
32

43
module {{moduleName}}
54
class Configuration
6-
7-
include Singleton
8-
9-
# Default api client
10-
attr_accessor :api_client
11-
125
# Defines url scheme
136
attr_accessor :scheme
147

@@ -94,17 +87,6 @@ module {{moduleName}}
9487

9588
attr_accessor :force_ending_format
9689

97-
class << self
98-
def method_missing(method_name, *args, &block)
99-
config = Configuration.instance
100-
if config.respond_to?(method_name)
101-
config.send(method_name, *args, &block)
102-
else
103-
super
104-
end
105-
end
106-
end
107-
10890
def initialize
10991
@scheme = '{{scheme}}'
11092
@host = '{{host}}'
@@ -118,10 +100,17 @@ module {{moduleName}}
118100
@inject_format = false
119101
@force_ending_format = false
120102
@logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
103+
104+
yield(self) if block_given?
105+
end
106+
107+
# The default Configuration object.
108+
def self.default
109+
@@default ||= Configuration.new
121110
end
122111

123-
def api_client
124-
@api_client ||= ApiClient.new
112+
def configure
113+
yield(self) if block_given?
125114
end
126115

127116
def scheme=(scheme)

modules/swagger-codegen/src/main/resources/ruby/gem.mustache

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ require '{{importPath}}'
1919

2020
module {{moduleName}}
2121
class << self
22-
# Configure sdk using block.
23-
# {{moduleName}}.configure do |config|
24-
# config.username = "xxx"
25-
# config.password = "xxx"
26-
# end
27-
# If no block given, return the configuration singleton instance.
22+
# Customize default settings for the SDK using block.
23+
# {{moduleName}}.configure do |config|
24+
# config.username = "xxx"
25+
# config.password = "xxx"
26+
# end
27+
# If no block given, return the default Configuration object.
2828
def configure
2929
if block_given?
30-
yield Configuration.instance
30+
yield(Configuration.default)
3131
else
32-
Configuration.instance
32+
Configuration.default
3333
end
3434
end
3535
end

samples/client/petstore/ruby/lib/petstore.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@
1919

2020
module Petstore
2121
class << self
22-
# Configure sdk using block.
23-
# Petstore.configure do |config|
24-
# config.username = "xxx"
25-
# config.password = "xxx"
26-
# end
27-
# If no block given, return the configuration singleton instance.
22+
# Customize default settings for the SDK using block.
23+
# Petstore.configure do |config|
24+
# config.username = "xxx"
25+
# config.password = "xxx"
26+
# end
27+
# If no block given, return the default Configuration object.
2828
def configure
2929
if block_given?
30-
yield Configuration.instance
30+
yield(Configuration.default)
3131
else
32-
Configuration.instance
32+
Configuration.default
3333
end
3434
end
3535
end

0 commit comments

Comments
 (0)