Class: Melaya::HttpClient

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

Overview

Internal HTTP client. Supports Bearer JWT and mk_* platform API key. The credential is sent ONLY via the Authorization header — never in the URL query string, so it cannot leak into access logs or proxies. TLS is enforced by default; never log secrets.

Retry policy: bounded exponential back-off with jitter on network errors, 429, and 5xx — but ONLY for idempotent GET requests (max 2 retries). POST/PUT/PATCH/DELETE are never retried. Retry-After header is honoured on 429. Per-request timeout default: 30 seconds (configurable).

Constant Summary collapse

DEFAULT_BASE_URL =
"https://api.melaya.org"
DEFAULT_TIMEOUT_MS =

milliseconds

30_000
MAX_GET_RETRIES =

Maximum additional retries after the first attempt (2 retries = 3 total attempts) for idempotent GET requests only.

2
RETRY_STATUSES =
[429, 500, 502, 503, 504].freeze

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, base_url: DEFAULT_BASE_URL, verify_ssl: true, timeout_ms: DEFAULT_TIMEOUT_MS) ⇒ HttpClient

Returns a new instance of HttpClient.

Parameters:

  • api_key (String)

    mk_* platform key or Bearer JWT

  • base_url (String) (defaults to: DEFAULT_BASE_URL)
  • verify_ssl (Boolean) (defaults to: true)
  • timeout_ms (Integer) (defaults to: DEFAULT_TIMEOUT_MS)

    per-request timeout in milliseconds (default 30 000)

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
42
# File 'lib/melaya/http_client.rb', line 33

def initialize(api_key:, base_url: DEFAULT_BASE_URL, verify_ssl: true,
               timeout_ms: DEFAULT_TIMEOUT_MS)
  raise ArgumentError, "Melaya: TLS certificate verification cannot be disabled." unless verify_ssl
  # Never store in a way that could leak to logs accidentally — keep as
  # an opaque token string only accessible through the private accessor.
  @_tok       = api_key.freeze
  @base_uri   = URI.parse(base_url.chomp("/"))
  @verify_ssl = true
  @timeout_s  = (timeout_ms / 1000.0).ceil
end

Instance Method Details

#delete(path, params = {}) ⇒ Object



62
63
64
# File 'lib/melaya/http_client.rb', line 62

def delete(path, params = {})
  request(:delete, path, params: params)
end

#get(path, params = {}) ⇒ Object

── Public verb helpers ────────────────────────────────────────────────────



46
47
48
# File 'lib/melaya/http_client.rb', line 46

def get(path, params = {})
  request(:get, path, params: params)
end

#patch(path, body = nil) ⇒ Object



58
59
60
# File 'lib/melaya/http_client.rb', line 58

def patch(path, body = nil)
  request(:patch, path, body: body)
end

#post(path, body = nil) ⇒ Object



50
51
52
# File 'lib/melaya/http_client.rb', line 50

def post(path, body = nil)
  request(:post, path, body: body)
end

#put(path, body = nil) ⇒ Object



54
55
56
# File 'lib/melaya/http_client.rb', line 54

def put(path, body = nil)
  request(:put, path, body: body)
end