Make HTTP response accessible by storing the last response

so that users are able to retrieve, for example, rate-limit headers
from the response
This commit is contained in:
xhh
2015-06-23 15:02:40 +08:00
parent 3b6a3b4a38
commit 46869df631
6 changed files with 46 additions and 30 deletions

View File

@@ -1,7 +1,7 @@
module {{moduleName}}
module Swagger
class << self
attr_accessor :logger
attr_accessor :logger, :last_response
# A Swagger configuration object. Must act like a hash and return sensible
# values for all Swagger configuration options. See Swagger::Configuration.

View File

@@ -5,7 +5,7 @@ module {{moduleName}}
require 'addressable/uri'
require 'typhoeus'
attr_accessor :host, :path, :format, :params, :body, :http_method, :headers, :form_params, :auth_names
attr_accessor :host, :path, :format, :params, :body, :http_method, :headers, :form_params, :auth_names, :response
# All requests must have an HTTP method and a path
# Optionals parameters are :params, :headers, :body, :format, :host
@@ -169,7 +169,7 @@ module {{moduleName}}
:headers => self.headers.stringify_keys,
:verbose => Swagger.configuration.debug
}
response = case self.http_method.to_sym
raw = case self.http_method.to_sym
when :get,:GET
Typhoeus::Request.get(
self.url,
@@ -201,15 +201,22 @@ module {{moduleName}}
)
end
@response = Response.new(raw)
if Swagger.configuration.debug
Swagger.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
Swagger.logger.debug "HTTP response body ~BEGIN~\n#{@response.body}\n~END~\n"
end
Response.new(response)
end
# record as last response
Swagger.last_response = @response
def response
self.make
unless @response.success?
fail ApiError.new(:code => @response.code,
:response_headers => @response.headers,
:response_body => @response.body),
@response.status_message
end
@response
end
def response_code_pretty

View File

@@ -8,23 +8,24 @@ module {{moduleName}}
def initialize(raw)
self.raw = raw
unless raw.success?
fail ApiError.new(:code => code,
:response_headers => headers,
:response_body => body),
raw.status_message
end
end
def code
raw.code
end
def status_message
raw.status_message
end
def body
raw.body
end
def success?
raw.success?
end
# Deserialize the raw response body to the given return type.
#
# @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]"

View File

@@ -1,7 +1,7 @@
module SwaggerClient
module Swagger
class << self
attr_accessor :logger
attr_accessor :logger, :last_response
# A Swagger configuration object. Must act like a hash and return sensible
# values for all Swagger configuration options. See Swagger::Configuration.

View File

@@ -5,7 +5,7 @@ module SwaggerClient
require 'addressable/uri'
require 'typhoeus'
attr_accessor :host, :path, :format, :params, :body, :http_method, :headers, :form_params, :auth_names
attr_accessor :host, :path, :format, :params, :body, :http_method, :headers, :form_params, :auth_names, :response
# All requests must have an HTTP method and a path
# Optionals parameters are :params, :headers, :body, :format, :host
@@ -168,7 +168,7 @@ module SwaggerClient
:headers => self.headers.stringify_keys,
:verbose => Swagger.configuration.debug
}
response = case self.http_method.to_sym
raw = case self.http_method.to_sym
when :get,:GET
Typhoeus::Request.get(
self.url,
@@ -200,15 +200,22 @@ module SwaggerClient
)
end
@response = Response.new(raw)
if Swagger.configuration.debug
Swagger.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
Swagger.logger.debug "HTTP response body ~BEGIN~\n#{@response.body}\n~END~\n"
end
Response.new(response)
end
# record as last response
Swagger.last_response = @response
def response
self.make
unless @response.success?
fail ApiError.new(:code => @response.code,
:response_headers => @response.headers,
:response_body => @response.body),
@response.status_message
end
@response
end
def response_code_pretty

View File

@@ -8,23 +8,24 @@ module SwaggerClient
def initialize(raw)
self.raw = raw
unless raw.success?
fail ApiError.new(:code => code,
:response_headers => headers,
:response_body => body),
raw.status_message
end
end
def code
raw.code
end
def status_message
raw.status_message
end
def body
raw.body
end
def success?
raw.success?
end
# Deserialize the raw response body to the given return type.
#
# @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]"