def request(uri, method, data = nil, type = :write, request_options = {})
      exceptions = []

      connect_timeout = @connect_timeout
      send_timeout = if type == :search
        @search_timeout
      elsif type == :batch
        type = :write
        @batch_timeout
      else
        @send_timeout
      end
      receive_timeout = type == :search ? @search_timeout : @receive_timeout

      thread_local_hosts(type != :write).each_with_index do |host, i|
        connect_timeout += 2 if i == 2
        send_timeout += 10 if i == 2
        receive_timeout += 10 if i == 2

        thread_index_key = type != :write ? "algolia_search_host_index_#{application_id}" : "algolia_host_index_#{application_id}"
        Thread.current[thread_index_key] = host[:index]
        host[:last_call] = Time.now.to_i

        host[:session].connect_timeout = connect_timeout
        host[:session].send_timeout = send_timeout
        host[:session].receive_timeout = receive_timeout
        begin
          return perform_request(host[:session], host[:base_url] + uri, method, data, request_options)
        rescue AlgoliaProtocolError => e
          raise if e.code / 100 == 4
          exceptions << e
        rescue => e
          exceptions << e
        end
        host[:session].reset_all
      end
      raise AlgoliaProtocolError.new(0, "Cannot reach any host: #{exceptions.map { |e| e.to_s }.join(', ')}")
    end
def request(uri, method, data = nil, type = :write, request_options = {})
      exceptions = []

      connect_timeout = @connect_timeout
      send_timeout = if type == :search
        @search_timeout
      elsif type == :batch
        type = :write
        @batch_timeout
      else
        @send_timeout
      end
      receive_timeout = type == :search ? @search_timeout : @receive_timeout

      thread_local_hosts(type != :write).each_with_index do |host, i|
        connect_timeout += 2 if i == 2
        send_timeout += 10 if i == 2
        receive_timeout += 10 if i == 2

        thread_index_key = type != :write ? "algolia_search_host_index_#{application_id}" : "algolia_host_index_#{application_id}"
        Thread.current[thread_index_key] = host[:index]
        host[:last_call] = Time.now.to_i

        host[:session].connect_timeout = connect_timeout
        host[:session].send_timeout = send_timeout
        host[:session].receive_timeout = receive_timeout
        begin
          return perform_request(host[:session], host[:base_url] + uri, method, data, request_options)
        rescue AlgoliaProtocolError => e
          raise if e.code / 100 == 4
          exceptions << e
        rescue => e
          exceptions << e
        end
        host[:session].reset_all
      end
      raise AlgoliaProtocolError.new(0, "Cannot reach any host: #{exceptions.map { |e| e.to_s }.join(', ')}")
    end
Perform an HTTP request for the given uri and method
 with common basic response handling. Will raise a
 AlgoliaProtocolError if the response has an error status code,
 and will return the parsed JSON body on success, if there is one.