Class: EasyLabs::HTTP Private

Inherits:
Object
  • Object
show all
Defined in:
lib/easylabs/http.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.

Internal HTTP wrapper used by every resource. Delegates to Ruby’s stdlib ‘Net::HTTP` to avoid a runtime gem dependency.

Public interface mirrors ‘EasyApiClient#makeRequest` from @easylabs/common — same auth header, same error mapping, same Retry-After parsing.

Constant Summary collapse

DEFAULT_TIMEOUT =

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.

seconds

30

Instance Method Summary collapse

Constructor Details

#initialize(api_url:, api_key:, timeout: DEFAULT_TIMEOUT) ⇒ HTTP

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 HTTP.

Parameters:

  • api_url (String)

    base URL including ‘/v1/api`.

  • api_key (String)

    Easy Labs API key.

  • timeout (Integer) (defaults to: DEFAULT_TIMEOUT)

    open + read timeout in seconds.



24
25
26
27
28
# File 'lib/easylabs/http.rb', line 24

def initialize(api_url:, api_key:, timeout: DEFAULT_TIMEOUT)
  @api_url = api_url
  @api_key = api_key
  @timeout = timeout
end

Instance Method Details

#request(method, path, query: nil, body: nil, skip_auth: false, idempotency_key: nil) ⇒ 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.

Issue an API request.

Parameters:

  • method (Symbol)

    :get / :post / :patch / :put / :delete

  • path (String)

    path under the api base, e.g. “/customer”.

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

    query params (nil values dropped, arrays joined with ‘,`, booleans stringified).

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

    request body (any JSON-serializable object).

  • skip_auth (Boolean) (defaults to: false)

    when true, omit the ‘x-easy-api-key` header. Used by public iframe endpoints that authenticate via a `client_secret` in the body.

  • idempotency_key (String, nil) (defaults to: nil)

    optional ‘Idempotency-Key` header value.

Returns:

  • (Object, nil)

    parsed JSON body (Hash with symbolized keys), or nil for 204 responses.

Raises:



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/easylabs/http.rb', line 46

def request(method, path, query: nil, body: nil, skip_auth: false, idempotency_key: nil)
  uri = build_uri(path, query)
  req = build_request(method, uri, body: body, skip_auth: skip_auth, idempotency_key: idempotency_key)

  res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https",
                                                open_timeout: @timeout, read_timeout: @timeout) do |http|
    http.request(req)
  end

  handle_response(res)
end