Class: Collavre::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
app/services/collavre/http_client.rb

Overview

Thin, dependency-free HTTP wrapper over Net::HTTP shared across engines.

It centralizes timeout configuration, TLS negotiation and transport-error handling so vendor engines no longer each hand-roll their own Net::HTTP / HTTParty / Faraday setup. Callers keep full ownership of their own response parsing and domain error mapping — this wrapper only performs the request and returns a small Response value object.

Transport-layer failures (DNS, connection refused/reset, TLS handshake, open/read timeouts) are wrapped in Collavre::HttpClient::ConnectionError so a caller can rescue a single, stable error type regardless of the underlying exception class.

Defined Under Namespace

Classes: ConnectionError, Error, Response

Constant Summary collapse

DEFAULT_OPEN_TIMEOUT =
10
DEFAULT_READ_TIMEOUT =
30
TRANSPORT_ERRORS =

Transport-level exceptions that occur before any HTTP response exists.

[
  SocketError, SystemCallError, Timeout::Error, IOError,
  OpenSSL::SSL::SSLError, Net::OpenTimeout, Net::ReadTimeout
].freeze
REQUEST_CLASSES =
{
  get: Net::HTTP::Get,
  post: Net::HTTP::Post,
  patch: Net::HTTP::Patch,
  put: Net::HTTP::Put,
  delete: Net::HTTP::Delete
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(open_timeout: DEFAULT_OPEN_TIMEOUT, read_timeout: DEFAULT_READ_TIMEOUT, default_headers: {}) ⇒ HttpClient

Returns a new instance of HttpClient.



65
66
67
68
69
# File 'app/services/collavre/http_client.rb', line 65

def initialize(open_timeout: DEFAULT_OPEN_TIMEOUT, read_timeout: DEFAULT_READ_TIMEOUT, default_headers: {})
  @open_timeout = open_timeout
  @read_timeout = read_timeout
  @default_headers = default_headers
end

Instance Method Details

#delete(url, headers: {}) ⇒ Object



87
88
89
# File 'app/services/collavre/http_client.rb', line 87

def delete(url, headers: {})
  request(:delete, url, headers: headers)
end

#get(url, headers: {}) ⇒ Object



71
72
73
# File 'app/services/collavre/http_client.rb', line 71

def get(url, headers: {})
  request(:get, url, headers: headers)
end

#patch(url, body: nil, headers: {}) ⇒ Object



79
80
81
# File 'app/services/collavre/http_client.rb', line 79

def patch(url, body: nil, headers: {})
  request(:patch, url, body: body, headers: headers)
end

#post(url, body: nil, headers: {}) ⇒ Object



75
76
77
# File 'app/services/collavre/http_client.rb', line 75

def post(url, body: nil, headers: {})
  request(:post, url, body: body, headers: headers)
end

#put(url, body: nil, headers: {}) ⇒ Object



83
84
85
# File 'app/services/collavre/http_client.rb', line 83

def put(url, body: nil, headers: {})
  request(:put, url, body: body, headers: headers)
end