Class: ActiveHarness::Http::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/active_harness/http/client.rb

Overview

Thin HTTP POST adapter backed by Net::HTTP.

To swap in Faraday (or any other client), assign a compatible object to ActiveHarness.config.http_client:

class FaradayClient
  def post(url, headers:, body:, timeout:) = ...
end

ActiveHarness.configure { |c| c.http_client = FaradayClient.new }

Instance Method Summary collapse

Instance Method Details

#post(url, headers:, body:, timeout:) ⇒ String

Returns raw response body.

Parameters:

  • url (URI)
  • headers (Hash{String => String})
  • body (String)

    serialized request body

  • timeout (Integer)

    seconds for both open and read timeout

Returns:

  • (String)

    raw response body



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/active_harness/http/client.rb', line 23

def post(url, headers:, body:, timeout:)
  http              = Net::HTTP.new(url.host, url.port)
  http.use_ssl      = true
  http.read_timeout = timeout
  http.open_timeout = timeout

  req      = Net::HTTP::Post.new(url)
  headers.each { |k, v| req[k] = v }
  req.body = body

  http.request(req).body
rescue Net::ReadTimeout, Net::OpenTimeout
  raise Errors::TimeoutError, "Request timed out (#{url.host})"
rescue => e
  raise Errors::ProviderUnavailableError, "#{url.host} unreachable: #{e.message}"
end