require 'timeout' require 'net/http' module RailsConnector class ContentService DEFAULT_PROTOCOL = 'https'.freeze class Configuration attr_accessor :url attr_accessor :login attr_accessor :api_key attr_accessor :http_host def url if @url.nil? raise RailsConnectorError, 'ContentService configuration key "url" is missing. '\ 'Set "RailsConnector::Configuration.content_service_url = "' end @url end end cattr_accessor :configuration do Configuration.new end class RateLimitExceeded < StandardError def initialize(retry_after) @retry_after = retry_after end attr_reader :retry_after end class MaxSleepTimeReached < StandardError end class Delay def initialize @sleep_count = 0 @total_sleep_time = 0 end def next_sleep_time(min_sleep = 0) sleep_time = [2 ** @sleep_count * 0.5, min_sleep.to_f].max @total_sleep_time += sleep_time raise MaxSleepTimeReached if @total_sleep_time > 40 @sleep_count += 1 sleep_time end end class << self @next_request_not_before = nil def query(path, payload, options={}) timeout = options.fetch(:timeout, ConnectionManager::DEFAULT_TIMEOUT) retry_until_timeout_on_rate_limit_exceeded do request = build_request(path, payload) if @next_request_not_before && @next_request_not_before > (now = Time.now) sleep @next_request_not_before - now end handle_response(connection_manager.request(request, timeout)) end end # for tests only def forget_retry_after @next_request_not_before = nil end attr_writer :connection_manager def connection_manager @connection_manager ||= ConnectionManager.new(uri) end private def retry_until_timeout_on_rate_limit_exceeded delay = Delay.new begin yield rescue RateLimitExceeded => e begin sleep delay.next_sleep_time(e.retry_after) rescue MaxSleepTimeReached raise ::RailsConnector::RateLimitExceeded.new('rate limit exceeded', 429) end retry end end def build_request(path, payload) request = Net::HTTP::Post.new("#{uri.path}/#{path}".squeeze('/'), request_headers) request.basic_auth(configuration.login, configuration.api_key) request.body = MultiJson.dump(payload) request end def handle_response(response) if response.code.first == '2' retry_after = response["Retry-After"] @next_request_not_before = retry_after ? Time.now + retry_after.to_f : nil MultiJson.load(response.body) elsif response.code == "429" raise RateLimitExceeded.new(response["Retry-After"]) else raise NetworkError.new("Server responded with status code #{response.code}", response.code) end end def uri url = configuration.url url = "#{DEFAULT_PROTOCOL}://#{url}" unless url.match /^http/ URI.parse(url) end def request_headers headers = { 'Content-Type' => 'application/json', 'Accept' => 'application/json', } if configuration.http_host headers['Host'] = configuration.http_host end headers end end end end