lib/lvs/json_service/request.rb in LVS-JSONService-0.3.6 vs lib/lvs/json_service/request.rb in LVS-JSONService-0.3.7
- old
+ new
@@ -17,11 +17,12 @@
def http_request_with_timeout(service, args, options)
uri = URI.parse(service)
req = Net::HTTP::Post.new(uri.path)
- args[:requestId] = unique_request_id
+ 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
@@ -76,25 +77,28 @@
end
def run_remote_request(service, args, options = {})
LVS::JsonService::Logger.debug "Requesting '#{service}' with #{args.to_json}"
+ options[:request_id] = unique_request_id
if options[:cached_for]
timing = "CACHED"
response, result = Rails.cache.fetch([service, args].cache_key, :expires_in => options[:cached_for]) do
start = Time.now
response = http_request_with_timeout(service, args, options)
+ verify_request_id(response, options[:request_id])
net_timing = ("%.1f" % ((Time.now - start) * 1000)) + "ms"
start = Time.now
result = JSON.parse(response.body)
parse_timing = ("%.1f" % ((Time.now - start) * 1000)) + "ms"
timing = "Net: #{net_timing}, Parse: #{parse_timing}"
[response, result]
end
else
start = Time.now
response = http_request_with_timeout(service, args, options)
+ verify_request_id(response, options[:request_id])
net_timing = ("%.1f" % ((Time.now - start) * 1000)) + "ms"
start = Time.now
result = JSON.parse(response.body)
parse_timing = ("%.1f" % ((Time.now - start) * 1000)) + "ms"
timing = "Net: #{net_timing}, Parse: #{parse_timing}"
@@ -107,9 +111,17 @@
end
if result.is_a?(Hash) && result.has_key?("PCode")
raise LVS::JsonService::Error.new(result["message"], result["PCode"], service, args, result)
end
result
+ end
+
+ def verify_request_id(response, request_id)
+ returned_request_id = response["X-LVS-Request-ID"]
+ if returned_request_id != request_id && !returned_request_id.blank?
+ raise LVS::JsonService::RequestMismatchError.new("The sent Request ID (#{request_id}) didn't " +
+ "match the returned Request ID (#{returned_request_id}) ")
+ end
end
end
end
end
end