Class: ChalkRuby::Http::HttpRequester
- Inherits:
-
Object
- Object
- ChalkRuby::Http::HttpRequester
- Includes:
- ChalkRuby::Helpers
- Defined in:
- lib/chalk_ruby/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
-
#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:) ⇒ Response
Sends a request.
Methods included from ChalkRuby::Helpers
#get_option, included, #json_to_hash, #path_encode, #symbolize_hash, #to_json
Constructor Details
#initialize(adapter:, logger:) ⇒ HttpRequester
Returns a new instance of HttpRequester.
14 15 16 17 18 |
# File 'lib/chalk_ruby/http/http_requester.rb', line 14 def initialize(adapter:, logger:) @adapter = adapter @logger = logger @connections = {} end |
Instance Attribute Details
#adapter ⇒ Object
Returns the value of attribute adapter.
8 9 10 |
# File 'lib/chalk_ruby/http/http_requester.rb', line 8 def adapter @adapter end |
#logger ⇒ Object
Returns the value of attribute logger.
8 9 10 |
# File 'lib/chalk_ruby/http/http_requester.rb', line 8 def logger @logger end |
Instance Method Details
#connection(host) ⇒ Faraday::Connection
Retrieve the connection from the @connections
86 87 88 89 90 |
# File 'lib/chalk_ruby/http/http_requester.rb', line 86 def connection(host) @connections[host] ||= Faraday.new(host) do |f| f.adapter @adapter.to_sym end end |
#send_request(host:, method:, path:, body:, headers:, timeout:, connect_timeout:) ⇒ Response
Sends a request
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 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/chalk_ruby/http/http_requester.rb', line 32 def send_request( host:, method:, path:, body:, headers:, timeout:, connect_timeout: ) normalized_host = host.is_a?(String) ? host : host[:api_server] connection = connection(normalized_host) connection..timeout = timeout connection..open_timeout = connect_timeout if ENV['CHALK_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['CHALK_DEBUG'] @logger.info("Request succeeded. Response status: #{response.status}, body: #{response.body}") end return Response.new(status: response.status, body: response.body, headers: response.headers) end if ENV['CHALK_DEBUG'] @logger.info("Request failed. Response status: #{response.status}, error: #{response.body}") end Response.new(status: response.status, error: response.body, headers: response.headers) rescue Faraday::TimeoutError => e if ENV['CHALK_DEBUG'] @logger.info("Request timed out. Error: #{e.}") end Response.new(error: e., has_timed_out: true) rescue ::StandardError => e if ENV['CHALK_DEBUG'] @logger.info("Request failed. Error: #{e.}") end Response.new(error: e., network_failure: true) end |