Class: Nahook::HttpClient Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Low-level HTTP client used by both Client and Management.

Handles request execution, retry logic with exponential backoff, and error parsing. Not intended for direct use.

Constant Summary collapse

DEFAULT_BASE_URL =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"https://api.nahook.com"
DEFAULT_TIMEOUT_MS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

30_000
BASE_DELAY_MS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

500
MAX_DELAY_MS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

10_000
DEFAULT_ADAPTER =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The default Faraday adapter. ‘net_http_persistent` keeps a per-thread TCP/TLS pool so back-to-back requests skip the full handshake. Callers can override via the `adapter:` kwarg, or bypass adapter selection entirely by passing a pre-built `connection:`.

:net_http_persistent
REGION_BASE_URLS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{
  "us" => "https://us.api.nahook.com",
  "eu" => "https://eu.api.nahook.com",
  "ap" => "https://ap.api.nahook.com",
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token:, base_url: DEFAULT_BASE_URL, timeout_ms: DEFAULT_TIMEOUT_MS, retries: 0, adapter: DEFAULT_ADAPTER, connection: nil) ⇒ HttpClient

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of HttpClient.

Parameters:

  • token (String)

    bearer token for authentication

  • base_url (String) (defaults to: DEFAULT_BASE_URL)

    API base URL

  • timeout_ms (Integer) (defaults to: DEFAULT_TIMEOUT_MS)

    request timeout in milliseconds (default: 30000)

  • retries (Integer) (defaults to: 0)

    number of retry attempts for retryable errors

  • adapter (Symbol) (defaults to: DEFAULT_ADAPTER)

    Faraday adapter to use when no custom connection is supplied. Defaults to ‘:net_http_persistent` (keep-alive). Other options the caller can pass: `:net_http`, `:typhoeus`, `:patron`, etc. Whatever adapter is named must be available — Faraday 2 split adapters into separate gems, so e.g. `:typhoeus` requires `gem “faraday-typhoeus”`.

  • connection (Faraday::Connection, nil) (defaults to: nil)

    a fully-configured Faraday connection to use verbatim. When supplied, ‘base_url`, `timeout_ms`, and `adapter` are ignored — the caller owns connection configuration (custom middleware, instrumentation, proxies, mTLS, etc.).



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/nahook/http_client.rb', line 55

def initialize(token:, base_url: DEFAULT_BASE_URL, timeout_ms: DEFAULT_TIMEOUT_MS,
               retries: 0, adapter: DEFAULT_ADAPTER, connection: nil)
  @token   = token
  @retries = retries

  if connection
    @conn = connection
    # Prefer the supplied connection's own timeout so `TimeoutError` reports
    # the value the caller actually configured. Faraday options.timeout is
    # in seconds — convert. Fall back to the constructor kwarg if unset.
    conn_timeout_secs = connection.options.timeout
    @timeout_ms = conn_timeout_secs ? (conn_timeout_secs * 1000).to_i : timeout_ms
  else
    @timeout_ms = timeout_ms
    @conn = build_default_connection(base_url, timeout_ms, adapter)
  end
end

Class Method Details

.resolve_base_url(token) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



34
35
36
37
38
39
40
# File 'lib/nahook/http_client.rb', line 34

def self.resolve_base_url(token)
  if (m = token.match(/\Anhk_([a-z]{2})_/))
    REGION_BASE_URLS[m[1]] || DEFAULT_BASE_URL
  else
    DEFAULT_BASE_URL
  end
end

Instance Method Details

#request(method:, path:, body: nil, query: nil) ⇒ Hash?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Execute an HTTP request with optional retry logic.

Parameters:

  • method (Symbol)

    HTTP method (:get, :post, :patch, :delete)

  • path (String)

    request path (will be appended to base URL)

  • body (Hash, nil) (defaults to: nil)

    request body (will be JSON-encoded)

  • query (Hash, nil) (defaults to: nil)

    query parameters

Returns:

  • (Hash, nil)

    parsed JSON response, or nil for 204

Raises:



83
84
85
# File 'lib/nahook/http_client.rb', line 83

def request(method:, path:, body: nil, query: nil)
  execute_with_retry(method, path, body, query)
end