Skip to content

Commit 7f36f26

Browse files
authored
using partials in ruby api_client (#3564)
1 parent 411199b commit 7f36f26

7 files changed

Lines changed: 319 additions & 573 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RubyClientCodegen.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,12 @@ public void processOpts() {
234234
supportingFiles.add(new SupportingFile("travis.mustache", "", ".travis.yml"));
235235
supportingFiles.add(new SupportingFile("gemspec.mustache", "", gemName + ".gemspec"));
236236
supportingFiles.add(new SupportingFile("configuration.mustache", gemFolder, "configuration.rb"));
237+
supportingFiles.add(new SupportingFile("api_client.mustache", gemFolder, "api_client.rb"));
237238

238239
if (TYPHOEUS.equals(getLibrary())) {
239-
supportingFiles.add(new SupportingFile("api_client.mustache", gemFolder, "api_client.rb"));
240+
// for Typhoeus
240241
} else if (FARADAY.equals(getLibrary())) {
241-
supportingFiles.add(new SupportingFile("faraday_api_client.mustache", gemFolder, "api_client.rb"));
242+
// for Faraday
242243
additionalProperties.put("isFaraday", Boolean.TRUE);
243244
} else {
244245
throw new RuntimeException("Invalid HTTP library " + getLibrary() + ". Only faraday, typhoeus are supported.");

modules/openapi-generator/src/main/resources/ruby-client/api_client.mustache

Lines changed: 11 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ require 'date'
66
require 'json'
77
require 'logger'
88
require 'tempfile'
9+
{{^isFaraday}}
910
require 'typhoeus'
11+
{{/isFaraday}}
12+
{{#isFaraday}}
13+
require 'faraday'
14+
{{/isFaraday}}
1015

1116
module {{moduleName}}
1217
class ApiClient
@@ -33,94 +38,12 @@ module {{moduleName}}
3338
@@default ||= ApiClient.new
3439
end
3540

36-
# Call an API with given options.
37-
#
38-
# @return [Array<(Object, Integer, Hash)>] an array of 3 elements:
39-
# the data deserialized from response body (could be nil), response status code and response headers.
40-
def call_api(http_method, path, opts = {})
41-
request = build_request(http_method, path, opts)
42-
response = request.run
43-
44-
if @config.debugging
45-
@config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
46-
end
47-
48-
unless response.success?
49-
if response.timed_out?
50-
fail ApiError.new('Connection timed out')
51-
elsif response.code == 0
52-
# Errors from libcurl will be made visible here
53-
fail ApiError.new(:code => 0,
54-
:message => response.return_message)
55-
else
56-
fail ApiError.new(:code => response.code,
57-
:response_headers => response.headers,
58-
:response_body => response.body),
59-
response.status_message
60-
end
61-
end
62-
63-
if opts[:return_type]
64-
data = deserialize(response, opts[:return_type])
65-
else
66-
data = nil
67-
end
68-
return data, response.code, response.headers
69-
end
70-
71-
# Builds the HTTP request
72-
#
73-
# @param [String] http_method HTTP method/verb (e.g. POST)
74-
# @param [String] path URL path (e.g. /account/new)
75-
# @option opts [Hash] :header_params Header parameters
76-
# @option opts [Hash] :query_params Query parameters
77-
# @option opts [Hash] :form_params Query parameters
78-
# @option opts [Object] :body HTTP body (JSON/XML)
79-
# @return [Typhoeus::Request] A Typhoeus Request
80-
def build_request(http_method, path, opts = {})
81-
url = build_request_url(path)
82-
http_method = http_method.to_sym.downcase
83-
84-
header_params = @default_headers.merge(opts[:header_params] || {})
85-
query_params = opts[:query_params] || {}
86-
form_params = opts[:form_params] || {}
87-
88-
{{#hasAuthMethods}}
89-
update_params_for_auth! header_params, query_params, opts[:auth_names]
90-
{{/hasAuthMethods}}
91-
92-
# set ssl_verifyhosts option based on @config.verify_ssl_host (true/false)
93-
_verify_ssl_host = @config.verify_ssl_host ? 2 : 0
94-
95-
req_opts = {
96-
:method => http_method,
97-
:headers => header_params,
98-
:params => query_params,
99-
:params_encoding => @config.params_encoding,
100-
:timeout => @config.timeout,
101-
:ssl_verifypeer => @config.verify_ssl,
102-
:ssl_verifyhost => _verify_ssl_host,
103-
:sslcert => @config.cert_file,
104-
:sslkey => @config.key_file,
105-
:verbose => @config.debugging
106-
}
107-
108-
# set custom cert, if provided
109-
req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert
110-
111-
if [:post, :patch, :put, :delete].include?(http_method)
112-
req_body = build_request_body(header_params, form_params, opts[:body])
113-
req_opts.update :body => req_body
114-
if @config.debugging
115-
@config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
116-
end
117-
end
118-
119-
request = Typhoeus::Request.new(url, req_opts)
120-
download_file(request) if opts[:return_type] == 'File'
121-
request
122-
end
123-
41+
{{^isFaraday}}
42+
{{> api_client_typhoeus_partial}}
43+
{{/isFaraday}}
44+
{{#isFaraday}}
45+
{{> api_client_faraday_partial}}
46+
{{/isFaraday}}
12447
# Check if the given MIME is a JSON MIME.
12548
# JSON MIME examples:
12649
# application/json
@@ -258,34 +181,6 @@ module {{moduleName}}
258181
@config.base_url + path
259182
end
260183

261-
# Builds the HTTP request body
262-
#
263-
# @param [Hash] header_params Header parameters
264-
# @param [Hash] form_params Query parameters
265-
# @param [Object] body HTTP body (JSON/XML)
266-
# @return [String] HTTP body data in the form of string
267-
def build_request_body(header_params, form_params, body)
268-
# http form
269-
if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
270-
header_params['Content-Type'] == 'multipart/form-data'
271-
data = {}
272-
form_params.each do |key, value|
273-
case value
274-
when ::File, ::Array, nil
275-
# let typhoeus handle File, Array and nil parameters
276-
data[key] = value
277-
else
278-
data[key] = value.to_s
279-
end
280-
end
281-
elsif body
282-
data = body.is_a?(String) ? body : body.to_json
283-
else
284-
data = nil
285-
end
286-
data
287-
end
288-
289184
# Update hearder and query params based on authentication settings.
290185
#
291186
# @param [Hash] header_params Header parameters
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Call an API with given options.
2+
#
3+
# @return [Array<(Object, Integer, Hash)>] an array of 3 elements:
4+
# the data deserialized from response body (could be nil), response status code and response headers.
5+
def call_api(http_method, path, opts = {})
6+
ssl_options = {
7+
:ca_file => @config.ssl_ca_file,
8+
:verify => @config.ssl_verify,
9+
:verify => @config.ssl_verify_mode,
10+
:client_cert => @config.ssl_client_cert,
11+
:client_key => @config.ssl_client_key
12+
}
13+
14+
connection = Faraday.new(:url => config.base_url, :ssl => ssl_options) do |conn|
15+
conn.basic_auth(config.username, config.password)
16+
if opts[:header_params]["Content-Type"] == "multipart/form-data"
17+
conn.request :multipart
18+
conn.request :url_encoded
19+
end
20+
conn.adapter(Faraday.default_adapter)
21+
end
22+
23+
begin
24+
response = connection.public_send(http_method.to_sym.downcase) do |req|
25+
build_request(http_method, path, req, opts)
26+
end
27+
28+
if @config.debugging
29+
@config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
30+
end
31+
32+
unless response.success?
33+
if response.status == 0
34+
# Errors from libcurl will be made visible here
35+
fail ApiError.new(:code => 0,
36+
:message => response.return_message)
37+
else
38+
fail ApiError.new(:code => response.status,
39+
:response_headers => response.headers,
40+
:response_body => response.body),
41+
response.reason_phrase
42+
end
43+
end
44+
rescue Faraday::TimeoutError
45+
fail ApiError.new('Connection timed out')
46+
end
47+
48+
if opts[:return_type]
49+
data = deserialize(response, opts[:return_type])
50+
else
51+
data = nil
52+
end
53+
return data, response.status, response.headers
54+
end
55+
56+
# Builds the HTTP request
57+
#
58+
# @param [String] http_method HTTP method/verb (e.g. POST)
59+
# @param [String] path URL path (e.g. /account/new)
60+
# @option opts [Hash] :header_params Header parameters
61+
# @option opts [Hash] :query_params Query parameters
62+
# @option opts [Hash] :form_params Query parameters
63+
# @option opts [Object] :body HTTP body (JSON/XML)
64+
# @return [Typhoeus::Request] A Typhoeus Request
65+
def build_request(http_method, path, request, opts = {})
66+
url = build_request_url(path)
67+
http_method = http_method.to_sym.downcase
68+
69+
header_params = @default_headers.merge(opts[:header_params] || {})
70+
query_params = opts[:query_params] || {}
71+
form_params = opts[:form_params] || {}
72+
73+
update_params_for_auth! header_params, query_params, opts[:auth_names]
74+
75+
req_opts = {
76+
:method => http_method,
77+
:headers => header_params,
78+
:params => query_params,
79+
:params_encoding => @config.params_encoding,
80+
:timeout => @config.timeout,
81+
:verbose => @config.debugging
82+
}
83+
84+
if [:post, :patch, :put, :delete].include?(http_method)
85+
req_body = build_request_body(header_params, form_params, opts[:body])
86+
req_opts.update :body => req_body
87+
if @config.debugging
88+
@config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
89+
end
90+
end
91+
request.headers = header_params
92+
request.body = req_body
93+
request.url url
94+
request.params = query_params
95+
download_file(request) if opts[:return_type] == 'File'
96+
request
97+
end
98+
99+
# Builds the HTTP request body
100+
#
101+
# @param [Hash] header_params Header parameters
102+
# @param [Hash] form_params Query parameters
103+
# @param [Object] body HTTP body (JSON/XML)
104+
# @return [String] HTTP body data in the form of string
105+
def build_request_body(header_params, form_params, body)
106+
# http form
107+
if header_params['Content-Type'] == 'application/x-www-form-urlencoded'
108+
data = URI.encode_www_form(form_params)
109+
elsif header_params['Content-Type'] == 'multipart/form-data'
110+
data = {}
111+
form_params.each do |key, value|
112+
case value
113+
when ::File, ::Tempfile
114+
# TODO hardcode to application/octet-stream, need better way to detect content type
115+
data[key] = Faraday::UploadIO.new(value.path, 'application/octet-stream', value.path)
116+
when ::Array, nil
117+
# let Faraday handle Array and nil parameters
118+
data[key] = value
119+
else
120+
data[key] = value.to_s
121+
end
122+
end
123+
elsif body
124+
data = body.is_a?(String) ? body : body.to_json
125+
else
126+
data = nil
127+
end
128+
data
129+
end

0 commit comments

Comments
 (0)