Skip to content

Commit 044961f

Browse files
ota42yackintosh
authored andcommitted
Pass opts argument to api client in ruby-client (#2754)
* Pass opts argument to api client in ruby-client We want to change the option when debugging, changing timeout to specific api, etc... So we want to merge and pass options to api client. * fix ruby-client * fix openapi ruby-client * fix typo * fix template
1 parent 0ebc714 commit 044961f

File tree

14 files changed

+1222
-524
lines changed

14 files changed

+1222
-524
lines changed

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

+17-13
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ module {{moduleName}}
126126
local_var_path = '{{{path}}}'{{#pathParams}}.sub('{' + '{{baseName}}' + '}', {{paramName}}.to_s){{/pathParams}}
127127

128128
# query parameters
129-
query_params = {}
129+
query_params = opts[:query_params] || {}
130130
{{#queryParams}}
131131
{{#required}}
132132
query_params[:'{{{baseName}}}'] = {{#collectionFormat}}@api_client.build_collection_param({{{paramName}}}, :{{{collectionFormat}}}){{/collectionFormat}}{{^collectionFormat}}{{{paramName}}}{{/collectionFormat}}
@@ -139,7 +139,7 @@ module {{moduleName}}
139139
{{/queryParams}}
140140

141141
# header parameters
142-
header_params = {}
142+
header_params = opts[:header_params] || {}
143143
{{#hasProduces}}
144144
# HTTP header 'Accept' (if needed)
145145
header_params['Accept'] = @api_client.select_header_accept([{{#produces}}'{{{mediaType}}}'{{#hasMore}}, {{/hasMore}}{{/produces}}])
@@ -160,7 +160,7 @@ module {{moduleName}}
160160
{{/headerParams}}
161161

162162
# form parameters
163-
form_params = {}
163+
form_params = opts[:form_params] || {}
164164
{{#formParams}}
165165
{{#required}}
166166
form_params['{{baseName}}'] = {{#collectionFormat}}@api_client.build_collection_param({{{paramName}}}, :{{{collectionFormat}}}){{/collectionFormat}}{{^collectionFormat}}{{{paramName}}}{{/collectionFormat}}
@@ -173,20 +173,24 @@ module {{moduleName}}
173173
{{/formParams}}
174174

175175
# http body (model)
176-
{{^bodyParam}}
177-
post_body = nil
178-
{{/bodyParam}}
179-
{{#bodyParam}}
180-
post_body = @api_client.object_to_http_body({{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}})
181-
{{/bodyParam}}
182-
auth_names = [{{#authMethods}}'{{name}}'{{#hasMore}}, {{/hasMore}}{{/authMethods}}]
183-
data, status_code, headers = @api_client.call_api(:{{httpMethod}}, local_var_path,
176+
post_body = opts[:body] {{#bodyParam}}|| @api_client.object_to_http_body({{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}) {{/bodyParam}}
177+
178+
# return_type
179+
return_type = opts[:return_type] {{#returnType}}|| '{{{returnType}}}' {{/returnType}}
180+
181+
# auth_names
182+
auth_names = opts[:auth_names] || [{{#authMethods}}'{{name}}'{{#hasMore}}, {{/hasMore}}{{/authMethods}}]
183+
184+
new_options = opts.merge(
184185
:header_params => header_params,
185186
:query_params => query_params,
186187
:form_params => form_params,
187188
:body => post_body,
188-
:auth_names => auth_names{{#returnType}},
189-
:return_type => '{{{returnType}}}'{{/returnType}})
189+
:auth_names => auth_names,
190+
:return_type => return_type
191+
)
192+
193+
data, status_code, headers = @api_client.call_api(:{{httpMethod}}, local_var_path, new_options)
190194
if @api_client.config.debugging
191195
@api_client.config.logger.debug "API called: {{classname}}#{{operationId}}\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
192196
end

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

+16-7
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,37 @@ def call_123_test_special_tags_with_http_info(body, opts = {})
4646
local_var_path = '/another-fake/dummy'
4747

4848
# query parameters
49-
query_params = {}
49+
query_params = opts[:query_params] || {}
5050

5151
# header parameters
52-
header_params = {}
52+
header_params = opts[:header_params] || {}
5353
# HTTP header 'Accept' (if needed)
5454
header_params['Accept'] = @api_client.select_header_accept(['application/json'])
5555
# HTTP header 'Content-Type'
5656
header_params['Content-Type'] = @api_client.select_header_content_type(['application/json'])
5757

5858
# form parameters
59-
form_params = {}
59+
form_params = opts[:form_params] || {}
6060

6161
# http body (model)
62-
post_body = @api_client.object_to_http_body(body)
63-
auth_names = []
64-
data, status_code, headers = @api_client.call_api(:PATCH, local_var_path,
62+
post_body = opts[:body] || @api_client.object_to_http_body(body)
63+
64+
# return_type
65+
return_type = opts[:return_type] || 'Client'
66+
67+
# auth_names
68+
auth_names = opts[:auth_names] || []
69+
70+
new_options = opts.merge(
6571
:header_params => header_params,
6672
:query_params => query_params,
6773
:form_params => form_params,
6874
:body => post_body,
6975
:auth_names => auth_names,
70-
:return_type => 'Client')
76+
:return_type => return_type
77+
)
78+
79+
data, status_code, headers = @api_client.call_api(:PATCH, local_var_path, new_options)
7180
if @api_client.config.debugging
7281
@api_client.config.logger.debug "API called: AnotherFakeApi#call_123_test_special_tags\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
7382
end

0 commit comments

Comments
 (0)