Class: Algolia::Http::HttpRequester

Inherits:
Object
  • Object
show all
Defined in:
lib/algolia/transport/http/http_requester.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter, logger) ⇒ HttpRequester



10
11
12
13
14
# File 'lib/algolia/transport/http/http_requester.rb', line 10

def initialize(adapter, logger)
  @adapter = adapter
  @logger = logger
  @connections = {}
end

Instance Attribute Details

#adapterObject

Returns the value of attribute adapter.



4
5
6
# File 'lib/algolia/transport/http/http_requester.rb', line 4

def adapter
  @adapter
end

#loggerObject

Returns the value of attribute logger.



4
5
6
# File 'lib/algolia/transport/http/http_requester.rb', line 4

def logger
  @logger
end

Instance Method Details

#build_url(host) ⇒ String

Build url from host, path and parameters



87
88
89
# File 'lib/algolia/transport/http/http_requester.rb', line 87

def build_url(host)
  host.protocol + host.url + (host.port.nil? ? "" : ":#{host.port}")
end

#connection(host) ⇒ Faraday::Connection

Retrieve the connection from the @connections



74
75
76
77
78
79
# File 'lib/algolia/transport/http/http_requester.rb', line 74

def connection(host)
  url = build_url(host)
  @connections[url] ||= Faraday.new(url) do |f|
    f.adapter(@adapter.to_sym)
  end
end

#handle_query_params(query_params) ⇒ Object

Convert query_params to a full query string



93
94
95
# File 'lib/algolia/transport/http/http_requester.rb', line 93

def handle_query_params(query_params)
  query_params.nil? || query_params.empty? ? "" : "?#{to_query_string(query_params)}"
end

#send_request(host, method, path, body, query_params, headers, timeout, connect_timeout) ⇒ Http::Response

Sends request to the engine



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/algolia/transport/http/http_requester.rb', line 27

def send_request(host, method, path, body, query_params, headers, timeout, connect_timeout)
  connection = connection(host)
  connection.options.timeout = timeout / 1000
  connection.options.open_timeout = connect_timeout / 1000
  path += handle_query_params(query_params)

  @logger.info("Sending #{method.to_s.upcase!} request to #{path} with body #{body}") if ENV["ALGOLIA_DEBUG"]

  response = connection.run_request(method, path, body, headers)

  if response.success?
    if ENV["ALGOLIA_DEBUG"]
      @logger.info("Request succeeded. Response status: #{response.status}, body: #{response.body}")
    end

    return Http::Response.new(
      status: response.status,
      reason_phrase: response.reason_phrase,
      body: response.body,
      headers: response.headers
    )
  end

  if ENV["ALGOLIA_DEBUG"]
    @logger.info("Request failed. Response status: #{response.status}, error: #{response.body}")
  end

  Http::Response.new(
    status: response.status,
    reason_phrase: response.reason_phrase,
    error: response.body,
    headers: response.headers
  )
rescue Faraday::TimeoutError => e
  @logger.info("Request timed out. Error: #{e.message}") if ENV["ALGOLIA_DEBUG"]
  Http::Response.new(error: e.message, has_timed_out: true)
rescue ::StandardError => e
  @logger.info("Request failed. Error: #{e.message}") if ENV["ALGOLIA_DEBUG"]
  Http::Response.new(error: e.message, network_failure: true)
end

#to_query_string(query_params) ⇒ Object

Create a query string from query_params



99
100
101
102
103
104
105
# File 'lib/algolia/transport/http/http_requester.rb', line 99

def to_query_string(query_params)
  query_params
    .map do |key, value|
      "#{key}=#{value}"
    end
    .join("&")
end