lib/xeroizer/http.rb in xeroizer-2.15.9 vs lib/xeroizer/http.rb in xeroizer-2.16.0
- old
+ new
@@ -13,10 +13,11 @@
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
module Xeroizer
module Http
class BadResponse < StandardError; end
+ RequestInfo = Struct.new(:url, :headers, :params, :body)
ACCEPT_MIME_MAP = {
:pdf => 'application/pdf',
:json => 'application/json'
}
@@ -55,13 +56,13 @@
def http_request(client, method, url, body, params = {})
# headers = {'Accept-Encoding' => 'gzip, deflate'}
headers = self.default_headers.merge({ 'charset' => 'utf-8' })
- # include the unitdp query string parameter
+ # include the unitdp query string parameter
params.merge!(unitdp_param(url))
-
+
if method != :get
headers['Content-Type'] ||= "application/x-www-form-urlencoded"
end
content_type = params.delete(:content_type)
@@ -82,14 +83,17 @@
if params.any?
url += "?" + params.map {|key,value| "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}"}.join("&")
end
- uri = URI.parse(url)
+ uri = URI.parse(url)
attempts = 0
+ request_info = RequestInfo.new(url, headers, params, body)
+ before_request.call(request_info) if before_request
+
begin
attempts += 1
logger.info("XeroGateway Request: #{method.to_s.upcase} #{uri.request_uri}") if self.logger
raw_body = params.delete(:raw_body) ? body : {:xml => body}
@@ -98,16 +102,12 @@
when :get then client.get(uri.request_uri, headers)
when :post then client.post(uri.request_uri, raw_body, headers)
when :put then client.put(uri.request_uri, raw_body, headers)
end
- if self.logger
- logger.info("XeroGateway Response (#{response.code})")
- unless response.code.to_i == 200
- logger.info("#{uri.request_uri}\n== Response Body\n\n#{response.plain_body}\n== End Response Body")
- end
- end
+ log_response(response, uri)
+ after_request.call(request_info, response) if after_request
case response.code.to_i
when 200
response.plain_body
when 400
@@ -131,10 +131,19 @@
raise
end
end
end
- def handle_oauth_error!(response)
+ def log_response(response, uri)
+ if self.logger
+ logger.info("XeroGateway Response (#{response.code})")
+ logger.add(response.code.to_i == 200 ? Logger::DEBUG : Logger::INFO) {
+ "#{uri.request_uri}\n== Response Body\n\n#{response.plain_body}\n== End Response Body"
+ }
+ end
+ end
+
+ def handle_oauth_error!(response)
error_details = CGI.parse(response.plain_body)
description = error_details["oauth_problem_advice"].first
problem = error_details["oauth_problem"].first
# see http://oauth.pbworks.com/ProblemReporting