Class: Melaya::HttpClient
- Inherits:
-
Object
- Object
- Melaya::HttpClient
- 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
- #delete(path, params = {}) ⇒ Object
-
#get(path, params = {}) ⇒ Object
── Public verb helpers ────────────────────────────────────────────────────.
-
#initialize(api_key:, base_url: DEFAULT_BASE_URL, verify_ssl: true, timeout_ms: DEFAULT_TIMEOUT_MS) ⇒ HttpClient
constructor
A new instance of HttpClient.
- #patch(path, body = nil) ⇒ Object
- #post(path, body = nil) ⇒ Object
- #put(path, body = nil) ⇒ Object
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.
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 |