Class: Algolia::Http::HttpRequester
- Inherits:
-
Object
- Object
- Algolia::Http::HttpRequester
- Includes:
- Helpers
- Defined in:
- lib/algolia/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.
-
#initialize(adapter, logger) ⇒ HttpRequester
constructor
A new instance of HttpRequester.
-
#send_request(host, method, path, body, headers, timeout, connect_timeout) ⇒ Http::Response
Sends request to the engine.
Methods included from Helpers
#check_array, #check_object, #chunk, #deserialize_settings, #get_object_id, #get_option, #handle_params, #hash_includes_subset?, included, #json_to_hash, #path_encode, #symbolize_hash, #to_json, #to_query_string
Constructor Details
#initialize(adapter, logger) ⇒ HttpRequester
Returns a new instance of HttpRequester.
11 12 13 14 15 |
# File 'lib/algolia/http/http_requester.rb', line 11 def initialize(adapter, logger) @adapter = adapter @logger = logger @connections = {} end |
Instance Attribute Details
#adapter ⇒ Object
Returns the value of attribute adapter.
5 6 7 |
# File 'lib/algolia/http/http_requester.rb', line 5 def adapter @adapter end |
#logger ⇒ Object
Returns the value of attribute logger.
5 6 7 |
# File 'lib/algolia/http/http_requester.rb', line 5 def logger @logger end |
Instance Method Details
#build_url(host) ⇒ String
Build url from host, path and parameters
79 80 81 |
# File 'lib/algolia/http/http_requester.rb', line 79 def build_url(host) host.protocol + host.url end |
#connection(host) ⇒ Faraday::Connection
Retrieve the connection from the @connections
67 68 69 70 71 |
# File 'lib/algolia/http/http_requester.rb', line 67 def connection(host) @connections[host.accept] ||= Faraday.new(build_url(host)) do |f| f.adapter @adapter.to_sym end end |
#send_request(host, method, path, body, 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 |
# File 'lib/algolia/http/http_requester.rb', line 27 def send_request(host, method, path, body, headers, timeout, connect_timeout) connection = connection(host) connection..timeout = timeout connection..open_timeout = connect_timeout if ENV['ALGOLIA_DEBUG'] @logger.info("Sending #{method.to_s.upcase!} request to #{path} with body #{body}") end 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 if ENV['ALGOLIA_DEBUG'] @logger.info("Request timed out. Error: #{e.message}") end Http::Response.new(error: e., has_timed_out: true) rescue ::StandardError => e if ENV['ALGOLIA_DEBUG'] @logger.info("Request failed. Error: #{e.message}") end Http::Response.new(error: e., network_failure: true) end |