Added with-http-info API methods to Ruby client

to allow accessing response status code and headers, and removed the methods of
recording last response info from ApiClient.
This commit is contained in:
xhh
2015-12-07 15:48:59 +08:00
parent d1361ab529
commit 66112d9eb5
7 changed files with 302 additions and 90 deletions

View File

@@ -17,6 +17,17 @@ module {{moduleName}}
{{#allParams}}{{^required}} # @option opts [{{{dataType}}}] :{{paramName}} {{description}}
{{/required}}{{/allParams}} # @return [{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}nil{{/returnType}}]
def {{operationId}}({{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}opts = {})
{{#returnType}}status_code, headers, data = {{/returnType}}{{operationId}}_with_http_info({{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}opts)
{{#returnType}}return data{{/returnType}}{{^returnType}}return nil{{/returnType}}
end
# {{summary}}
# {{notes}}
{{#allParams}}{{#required}} # @param {{paramName}} {{description}}
{{/required}}{{/allParams}} # @param [Hash] opts the optional parameters
{{#allParams}}{{^required}} # @option opts [{{{dataType}}}] :{{paramName}} {{description}}
{{/required}}{{/allParams}} # @return [Array<Fixnum, Hash, {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}nil{{/returnType}}>] response status code, response headers and {{#returnType}}{{{returnType}}} data{{/returnType}}{{^returnType}}nil{{/returnType}}
def {{operationId}}_with_http_info({{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}opts = {})
if Configuration.debugging
Configuration.logger.debug "Calling API: {{classname}}#{{operationId}} ..."
end
@@ -63,26 +74,17 @@ module {{moduleName}}
{{/bodyParam}}
auth_names = [{{#authMethods}}'{{name}}'{{#hasMore}}, {{/hasMore}}{{/authMethods}}]
{{#returnType}}result = @api_client.call_api(:{{httpMethod}}, path,
status_code, headers, data = @api_client.call_api(:{{httpMethod}}, path,
:header_params => header_params,
:query_params => query_params,
:form_params => form_params,
:body => post_body,
:auth_names => auth_names,
:return_type => '{{{returnType}}}')
:auth_names => auth_names{{#returnType}},
:return_type => '{{{returnType}}}'{{/returnType}})
if Configuration.debugging
Configuration.logger.debug "API called: {{classname}}#{{operationId}}. Result: #{result.inspect}"
Configuration.logger.debug "API called: {{classname}}#{{operationId}}\nStatus code: #{status_code}\nHeaders: #{headers}\nData: #{data.inspect}"
end
return result{{/returnType}}{{^returnType}}@api_client.call_api(:{{httpMethod}}, path,
:header_params => header_params,
:query_params => query_params,
:form_params => form_params,
:body => post_body,
:auth_names => auth_names)
if Configuration.debugging
Configuration.logger.debug "API called: {{classname}}#{{operationId}}"
end
return nil{{/returnType}}
return status_code, headers, data
end
{{/operation}}
end

View File

@@ -15,9 +15,6 @@ module {{moduleName}}
# @return [Hash]
attr_accessor :default_headers
# Stores the HTTP response from the last API call using this API client.
attr_accessor :last_response
def initialize(host = nil)
@host = host || Configuration.base_url
@format = 'json'
@@ -28,13 +25,12 @@ module {{moduleName}}
}
end
# Call an API with given options and an array of 3 elements:
# response status code, response headers and the data deserialized from response body (could be nil).
def call_api(http_method, path, opts = {})
request = build_request(http_method, path, opts)
response = request.run
# record as last response
@last_response = response
if Configuration.debugging
Configuration.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
end
@@ -47,10 +43,11 @@ module {{moduleName}}
end
if opts[:return_type]
deserialize(response, opts[:return_type])
data = deserialize(response, opts[:return_type])
else
nil
data = nil
end
return response.code, response.headers, data
end
def build_request(http_method, path, opts = {})