Class: ActiveHarness::Http::Client

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

Overview

Thin Net::HTTP wrapper — no external dependencies.

Instance Method Summary collapse

Instance Method Details

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

Returns raw response body.

Parameters:

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

    JSON-serialized body

  • timeout (Integer) (defaults to: 30)

    seconds (open + read)

Returns:

  • (String)

    raw response body



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/active_harness/http/client.rb', line 12

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

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

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