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
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) ⇒ 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



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/nahook/http_client.rb', line 39

def initialize(token:, base_url: DEFAULT_BASE_URL, timeout_ms: DEFAULT_TIMEOUT_MS, retries: 0)
  @token      = token
  @retries    = retries
  @timeout_ms = timeout_ms

  timeout_secs = timeout_ms / 1000.0
  @conn = Faraday.new(url: base_url.chomp("/")) do |f|
    f.options.timeout      = timeout_secs
    f.options.open_timeout = timeout_secs
    f.adapter Faraday.default_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.



27
28
29
30
31
32
33
# File 'lib/nahook/http_client.rb', line 27

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:



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

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