Class: ChalkRuby::Http::HttpRequester

Inherits:
Object
  • Object
show all
Includes:
ChalkRuby::Helpers
Defined in:
lib/chalk_ruby/http/http_requester.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • adapter (Net::Http)

    adapter used to make requests. Defaults to Net::Http

  • logger (Logger)

    logger used to log requests. Defaults to ChalkRuby::LoggerHelper



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

#adapterObject

Returns the value of attribute adapter.



8
9
10
# File 'lib/chalk_ruby/http/http_requester.rb', line 8

def adapter
  @adapter
end

#loggerObject

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

Parameters:

  • host (String)

Returns:

  • (Faraday::Connection)


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

Parameters:

  • host (String)
  • method (Symbol)
  • path (String)
  • body (String)
  • headers (Hash)
  • timeout (Integer)
  • connect_timeout (Integer)

Returns:



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.options.timeout      = timeout
  connection.options.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.message}")
  end
  Response.new(error: e.message, has_timed_out: true)
rescue ::StandardError => e
  if ENV['CHALK_DEBUG']
    @logger.info("Request failed. Error: #{e.message}")
  end
  Response.new(error: e.message, network_failure: true)
end