lib/tickethub/request.rb in tickethub-0.3.4 vs lib/tickethub/request.rb in tickethub-0.3.6

- old
+ new

@@ -1,22 +1,31 @@ +require 'securerandom' + module Tickethub class Request + + SAFE_TO_RETRY = [408, 502, 503, 504].freeze + MAXIMUM_RETRIES = 6.freeze + DEFAULT_TIMEOUT = 5.freeze + attr_reader :options, :format, :url attr_accessor :params, :body, :method, :headers # Connection options attr_accessor :proxy, :user, :password, :auth_type, :timeout, :ssl_options def initialize(url, options = {}) - @url = url.to_s + @url = url.to_s + @id = SecureRandom.uuid @options = options @options.each do |key, val| method = "#{key}=" send(method, val) if respond_to?(method) end + self.timeout ||= DEFAULT_TIMEOUT self.method ||= :get self.params ||= {} self.headers ||= {} self.format ||= :form end @@ -59,10 +68,16 @@ result = connection.send(method, query_path, build_headers) end Response.new(result) + rescue TimeoutError => err + raise if retries == MAXIMUM_RETRIES - 1 + execute retries + 1 + rescue ConnectionError => err + raise if ! SAFE_TO_RETRY.member?(err.response.code) || retries == MAXIMUM_RETRIES - 1 + execute retries + 1 rescue Redirection => error raise error unless error.response['Location'] location = URI.parse(error.response['Location']) @@ -105,9 +120,10 @@ def build_headers auth_headers .merge(content_type_headers) .merge(headers) + .merge('X-Request-ID' => @id) end def query_path query_path = path \ No newline at end of file