Class: E2B::API::HttpClient

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

Overview

HTTP client wrapper for E2B API communication

Handles authentication, request/response processing, and error handling.

Defined Under Namespace

Classes: DetailedResponse

Constant Summary collapse

DEFAULT_TIMEOUT =

Default request timeout in seconds

120

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, api_key: nil, access_token: nil, logger: nil) ⇒ HttpClient

Initialize a new HTTP client

Parameters:

  • base_url (String)

    Base URL for API requests

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

    API key for authentication

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

    Access token for bearer authentication

  • logger (Logger, nil) (defaults to: nil)

    Optional logger



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

def initialize(base_url:, api_key: nil, access_token: nil, logger: nil)
  @base_url = base_url.end_with?("/") ? base_url : "#{base_url}/"
  @api_key = api_key
  @access_token = access_token
  @logger = logger
  @connection = build_connection
end

Instance Attribute Details

#base_urlString (readonly)

Returns Base URL for API requests.

Returns:

  • (String)

    Base URL for API requests



19
20
21
# File 'lib/e2b/api/http_client.rb', line 19

def base_url
  @base_url
end

Instance Method Details

#delete(path, body: nil, timeout: DEFAULT_TIMEOUT, detailed: false) ⇒ Hash, ...

Perform a DELETE request

Parameters:

  • path (String)

    API endpoint path

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

    Request body

  • timeout (Integer) (defaults to: DEFAULT_TIMEOUT)

    Request timeout in seconds

Returns:

  • (Hash, Array, String, nil)

    Parsed response body



86
87
88
89
90
91
92
93
# File 'lib/e2b/api/http_client.rb', line 86

def delete(path, body: nil, timeout: DEFAULT_TIMEOUT, detailed: false)
  handle_response(detailed: detailed) do
    @connection.delete(normalize_path(path)) do |req|
      req.body = body.to_json if body
      req.options.timeout = timeout
    end
  end
end

#get(path, params: {}, timeout: DEFAULT_TIMEOUT, detailed: false) ⇒ Hash, ...

Perform a GET request

Parameters:

  • path (String)

    API endpoint path

  • params (Hash) (defaults to: {})

    Query parameters

  • timeout (Integer) (defaults to: DEFAULT_TIMEOUT)

    Request timeout in seconds

Returns:

  • (Hash, Array, String)

    Parsed response body



41
42
43
44
45
46
47
48
# File 'lib/e2b/api/http_client.rb', line 41

def get(path, params: {}, timeout: DEFAULT_TIMEOUT, detailed: false)
  handle_response(detailed: detailed) do
    @connection.get(normalize_path(path)) do |req|
      req.params = params
      req.options.timeout = timeout
    end
  end
end

#post(path, body: nil, timeout: DEFAULT_TIMEOUT, detailed: false) ⇒ Hash, ...

Perform a POST request

Parameters:

  • path (String)

    API endpoint path

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

    Request body

  • timeout (Integer) (defaults to: DEFAULT_TIMEOUT)

    Request timeout in seconds

Returns:

  • (Hash, Array, String)

    Parsed response body



56
57
58
59
60
61
62
63
# File 'lib/e2b/api/http_client.rb', line 56

def post(path, body: nil, timeout: DEFAULT_TIMEOUT, detailed: false)
  handle_response(detailed: detailed) do
    @connection.post(normalize_path(path)) do |req|
      req.body = body.to_json if body
      req.options.timeout = timeout
    end
  end
end

#put(path, body: nil, timeout: DEFAULT_TIMEOUT, detailed: false) ⇒ Hash, ...

Perform a PUT request

Parameters:

  • path (String)

    API endpoint path

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

    Request body

  • timeout (Integer) (defaults to: DEFAULT_TIMEOUT)

    Request timeout in seconds

Returns:

  • (Hash, Array, String)

    Parsed response body



71
72
73
74
75
76
77
78
# File 'lib/e2b/api/http_client.rb', line 71

def put(path, body: nil, timeout: DEFAULT_TIMEOUT, detailed: false)
  handle_response(detailed: detailed) do
    @connection.put(normalize_path(path)) do |req|
      req.body = body.to_json if body
      req.options.timeout = timeout
    end
  end
end