lib/lvs/json_service/request.rb in LVS-JSONService-0.5.3 vs lib/lvs/json_service/request.rb in LVS-JSONService-0.5.4
- old
+ new
@@ -17,16 +17,24 @@
end
def http_standard_request_with_timeout(service, args, options)
uri = URI.parse(service)
- req = Net::HTTP::Post.new(uri.path)
+ Rails.logger.debug("Options are #{options.inspect}")
+ if options[:http_method] == :get
+ path = uri.path
+ path += "?#{uri.query}" unless uri.query.blank?
+ req = Net::HTTP::Get.new(path)
+ Rails.logger.debug("Getting '#{path}'")
+ else
+ req = Net::HTTP::Post.new(uri.path)
+ req.form_data = { "object_request" => args.to_json }
+ Rails.logger.debug("Posting to #{uri.path}")
+ end
req.basic_auth(uri.user, uri.password) unless uri.user.to_s == ""
req.add_field("X-LVS-Request-ID", options[:request_id])
- req.form_data = { "object_request" => args.to_json }
-
options[:encrypted] ||= require_ssl?
retries = options[:retries] || 0
hard_retries = 1 # For persistent connection failures
begin
@@ -34,10 +42,12 @@
http = LVS::JsonService::ConnectionManager.get_connection(uri.host, uri.port, options)
http.open_timeout = options[:timeout] || 1
http.read_timeout = options[:timeout] || 1
response = http.request(req)
+ Rails.logger.debug("Request is #{req.inspect}")
+ Rails.logger.debug("Response is #{response.body.inspect}")
rescue Errno::EPIPE, EOFError, Errno::ECONNRESET, Errno::ECONNABORTED
hard_retries -= 1
if hard_retries >= 0
sleep(1)
@@ -95,10 +105,14 @@
start = Time.now
http_standard_request_with_timeout(service, args, options) do |response|
verify_request_id(response["X-LVS-Request-ID"], options[:request_id])
net_timing = ("%.1f" % ((Time.now - start) * 1000)) + "ms"
start = Time.now
- result = JSON.parse(response.body)
+ if options[:raw]
+ result = response.body.force_encoding("UTF-8")
+ else
+ result = JSON.parse(response.body.force_encoding("UTF-8"))
+ end
parse_timing = ("%.1f" % ((Time.now - start) * 1000)) + "ms"
timing = "Net: #{net_timing}, Parse: #{parse_timing}"
if options[:cached_for]
Rails.cache.write([service, args].cache_key, [response, result], :expires_in => options[:cached_for])
end