Class: Algolia::Http::HttpRequester
- Inherits:
-
Object
- Object
- Algolia::Http::HttpRequester
- Defined in:
- lib/algolia/transport/http/http_requester.rb
Instance Attribute Summary collapse
-
#adapter ⇒ Object
Returns the value of attribute adapter.
-
#logger ⇒ Object
Returns the value of attribute logger.
Instance Method Summary collapse
-
#build_url(host) ⇒ String
Build url from host, path and parameters.
-
#connection(host) ⇒ Faraday::Connection
Retrieve the connection from the @connections.
-
#handle_query_params(query_params) ⇒ Object
Convert query_params to a full query string.
-
#initialize(adapter, logger) ⇒ HttpRequester
constructor
A new instance of HttpRequester.
-
#send_request(host, method, path, body, query_params, headers, timeout, connect_timeout) ⇒ Http::Response
Sends request to the engine.
-
#to_query_string(query_params) ⇒ Object
Create a query string from query_params.
Constructor Details
#initialize(adapter, logger) ⇒ HttpRequester
Returns a new instance of 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
#adapter ⇒ Object
Returns the value of attribute adapter.
4 5 6 |
# File 'lib/algolia/transport/http/http_requester.rb', line 4 def adapter @adapter end |
#logger ⇒ Object
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
77 78 79 |
# File 'lib/algolia/transport/http/http_requester.rb', line 77 def build_url(host) host.protocol + host.url + (host.port.nil? ? "" : ":#{host.port}") end |
#connection(host) ⇒ Faraday::Connection
Retrieve the connection from the @connections
64 65 66 67 68 69 |
# File 'lib/algolia/transport/http/http_requester.rb', line 64 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
83 84 85 |
# File 'lib/algolia/transport/http/http_requester.rb', line 83 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 |
# 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..timeout = timeout / 1000 connection..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, 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, error: response.body, headers: response.headers) rescue Faraday::TimeoutError => e @logger.info("Request timed out. Error: #{e.}") if ENV["ALGOLIA_DEBUG"] Http::Response.new(error: e., has_timed_out: true) rescue ::StandardError => e @logger.info("Request failed. Error: #{e.}") if ENV["ALGOLIA_DEBUG"] Http::Response.new(error: e., network_failure: true) end |
#to_query_string(query_params) ⇒ Object
Create a query string from query_params
89 90 91 92 93 94 95 |
# File 'lib/algolia/transport/http/http_requester.rb', line 89 def to_query_string(query_params) query_params .map do |key, value| "#{key}=#{value}" end .join("&") end |