Class: Cybertrain::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
cybertrain/http_client.rb

Overview

Sends GET and POST requests to other servers, with bounded waits.

Examples:

A Slack incoming webhook

require "cybertrain/http_client"
require "cybertrain/net/tls"

client = Cybertrain::HttpClient.new(timeout: 5, deadline: 15)
begin
  res = client.post(ENV["SLACK_WEBHOOK_URL"], JSON.generate({ "text" => "Deployed" }),
                    content_type: "application/json")
  Cybertrain.logger.warn("slack answered #{res.status}") unless res.success?
rescue Cybertrain::HttpClient::Error => e
  Cybertrain.logger.warn("slack: #{e.message}") # host and port only
end

Defined Under Namespace

Classes: BadResponseError, BlockedAddressError, Error, RefusedError, Response, TLSError, TimeoutError, TooLargeError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timeout: 5, deadline: 15, max_body: 1_048_576, allow_private: Cybertrain.config.http_allow_private) ⇒ HttpClient

Returns a new instance of HttpClient.

Parameters:

  • timeout (Numeric) (defaults to: 5) —

    seconds for each wait: connecting, each TLS step, each read, each write. Default 5.

  • deadline (Numeric) (defaults to: 15) —

    seconds for the whole request, name resolution included. Default 15.

  • max_body (Integer) (defaults to: 1_048_576) —

    the largest response body accepted, in bytes (larger raises TooLargeError). Default 1 MiB.

  • allow_private (Boolean) (defaults to: Cybertrain.config.http_allow_private) —

    allow loopback, private and other non-public destinations. Default Config#http_allow_private.

Raises:

  • (ArgumentError)


184
185
186
187
188
189
190
191
192
# File 'cybertrain/http_client.rb', line 184

def initialize(timeout: 5, deadline: 15, max_body: 1_048_576, allow_private: Cybertrain.config.http_allow_private)
  @timeout = timeout.to_f
  @deadline = deadline.to_f
  @max_body = max_body.to_i
  @allow_private = allow_private == true
  raise ArgumentError, "timeout must be positive" unless @timeout > 0.0
  raise ArgumentError, "deadline must be positive" unless @deadline > 0.0
  raise ArgumentError, "max_body must be at least 0" if @max_body < 0
end

Instance Attribute Details

#allow_private ⇒ Boolean (readonly)

Returns whether non-public destinations are allowed.

Returns:

  • (Boolean) —

    whether non-public destinations are allowed



173
174
175
# File 'cybertrain/http_client.rb', line 173

def allow_private
  @allow_private
end

#deadline ⇒ Float (readonly)

Returns seconds the whole request may take.

Returns:

  • (Float) —

    seconds the whole request may take



167
168
169
# File 'cybertrain/http_client.rb', line 167

def deadline
  @deadline
end

#max_body ⇒ Integer (readonly)

Returns the largest body accepted, in bytes.

Returns:

  • (Integer) —

    the largest body accepted, in bytes



170
171
172
# File 'cybertrain/http_client.rb', line 170

def max_body
  @max_body
end

#timeout ⇒ Float (readonly)

Returns seconds any one wait may take.

Returns:

  • (Float) —

    seconds any one wait may take



164
165
166
# File 'cybertrain/http_client.rb', line 164

def timeout
  @timeout
end

Instance Method Details

#get(url, headers: NO_HEADERS) ⇒ Response

GET url.

Parameters:

  • url (String) —

    http:// or https://

  • headers (Hash{String => String}) (defaults to: NO_HEADERS) —

    extra request headers

Returns:

Raises:

  • (Error) —

    a subclass; ArgumentError for a URL or header the client refuses



200
201
202
# File 'cybertrain/http_client.rb', line 200

def get(url, headers: NO_HEADERS)
  perform("GET", url, "", "", headers)
end

#post(url, body, content_type: "application/json", headers: NO_HEADERS) ⇒ Response

POST body to url.

Parameters:

  • url (String) —

    http:// or https://

  • body (String)
  • content_type (String) (defaults to: "application/json") —

    the Content-Type header

  • headers (Hash{String => String}) (defaults to: NO_HEADERS) —

    extra request headers

Returns:

Raises:

  • (Error) —

    a subclass; ArgumentError for a URL or header the client refuses



212
213
214
# File 'cybertrain/http_client.rb', line 212

def post(url, body, content_type: "application/json", headers: NO_HEADERS)
  perform("POST", url, body.to_s, content_type.to_s, headers)
end