Skip to content

HTTP Client

Matt Muller edited this page Apr 10, 2024 · 8 revisions

The HTTP client is a class that handles transmission of a Request object over the network and populates a Response object.

Usage

Hearth provides a default HTTP client implemented using Ruby's Net::HTTP. The HTTP client takes many options related to wire trace output, various timeouts, SSL verify, certificate authorities, DNS host resolving options, etc. The default HTTP client also uses a connection pool to re-use existing and known good connections.

http_client = Hearth::HTTP::Client.new(read_timeout: 1, debug_output: true)
# => #<Hearth::HTTP ... >

client = HighScoreService::Client.new(
  endpoint: 'http://127.0.0.1:3000',
  http_client: http_client
)
# => #<HighScoreService ... >

client.list_high_scores
# <Uses HTTP client with wire trace logging to send>
# GET /high_scores ...
# => #<Hearth::Output @data=... >

Custom HTTP client

A custom implemented HTTP client can be provided as long as it implements a transmit(request:, response:, logger: nil) method. Implementations can use whatever transport library it would like, such as raw sockets, curb, Faraday, etc. The method must populate the Response object with headers, body, status, etc. If an networking issue occurs, a Hearth::NetworkingError should be raised.

Clone this wiki locally