Class: Vangrail::HTTP

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

Overview

JSON over Net::HTTP with stdlib only: a guardrail that pulls in a transport stack is a guardrail nobody installs. Every call is bounded by open and read timeouts, because a rail that hangs is worse than a rail that is absent.

Constant Summary collapse

DEFAULT_OPEN_TIMEOUT =
5
DEFAULT_READ_TIMEOUT =
30

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, api_key: nil, open_timeout: DEFAULT_OPEN_TIMEOUT, read_timeout: DEFAULT_READ_TIMEOUT, retries: 1, headers: {}) ⇒ HTTP

Returns a new instance of HTTP.

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
40
41
# File 'lib/vangrail/http.rb', line 31

def initialize(base_url:, api_key: nil, open_timeout: DEFAULT_OPEN_TIMEOUT,
               read_timeout: DEFAULT_READ_TIMEOUT, retries: 1, headers: {})
  @base_url = base_url.to_s.sub(/\/+\z/, '')
  raise ArgumentError, 'base_url is required' if @base_url.empty?

  @api_key = api_key
  @open_timeout = open_timeout
  @read_timeout = read_timeout
  @retries = retries
  @headers = headers
end

Instance Attribute Details

#base_urlObject (readonly)

retries is a switch, not a count: 0 means no retry, any positive value retries TransportError once. HTTPError (including 429) is never retried and never slept on; a rail that waits is a rail that hangs the request.



18
19
20
# File 'lib/vangrail/http.rb', line 18

def base_url
  @base_url
end

#open_timeoutObject (readonly)

retries is a switch, not a count: 0 means no retry, any positive value retries TransportError once. HTTPError (including 429) is never retried and never slept on; a rail that waits is a rail that hangs the request.



18
19
20
# File 'lib/vangrail/http.rb', line 18

def open_timeout
  @open_timeout
end

#read_timeoutObject (readonly)

retries is a switch, not a count: 0 means no retry, any positive value retries TransportError once. HTTPError (including 429) is never retried and never slept on; a rail that waits is a rail that hangs the request.



18
19
20
# File 'lib/vangrail/http.rb', line 18

def read_timeout
  @read_timeout
end

#retriesObject (readonly)

retries is a switch, not a count: 0 means no retry, any positive value retries TransportError once. HTTPError (including 429) is never retried and never slept on; a rail that waits is a rail that hangs the request.



18
19
20
# File 'lib/vangrail/http.rb', line 18

def retries
  @retries
end

Class Method Details

.build(http: nil, base_url: nil, api_key: nil, open_timeout: DEFAULT_OPEN_TIMEOUT, read_timeout: DEFAULT_READ_TIMEOUT, missing: 'a client needs a base_url or an http client') ⇒ Object

Chat, Embeddings, Completion, and Client all take an HTTP or the arguments that build one. One helper so those constructors stay thin.

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
# File 'lib/vangrail/http.rb', line 22

def self.build(http: nil, base_url: nil, api_key: nil,
               open_timeout: DEFAULT_OPEN_TIMEOUT, read_timeout: DEFAULT_READ_TIMEOUT,
               missing: 'a client needs a base_url or an http client')
  return http if http
  raise ArgumentError, missing if base_url.to_s.strip.empty?

  new(base_url: base_url, api_key: api_key, open_timeout: open_timeout, read_timeout: read_timeout)
end

Instance Method Details

#get_json(path) ⇒ Object



43
44
45
# File 'lib/vangrail/http.rb', line 43

def get_json(path)
  request(Net::HTTP::Get, path, nil)
end

#post_json(path, payload) ⇒ Object



47
48
49
# File 'lib/vangrail/http.rb', line 47

def post_json(path, payload)
  request(Net::HTTP::Post, path, payload)
end

#reachable?(path) ⇒ Boolean

True when the endpoint answers at all. Used to pick a rail mode without making the caller handle an exception for the ordinary "not running" case.

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
# File 'lib/vangrail/http.rb', line 53

def reachable?(path)
  get_json(path)
  true
rescue HTTPError
  true
rescue Error
  false
end