Class: Hyperliquid::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/hyperliquid/client.rb

Overview

HTTP client for making requests to Hyperliquid API

Constant Summary collapse

DEFAULT_RETRY_OPTIONS =

Default retry configuration for API requests

{
  max: 2,
  interval: 0.5,
  interval_randomness: 0.5,
  backoff_factor: 2,
  retry_statuses: [429, 502, 503, 504],
  exceptions: [
    Faraday::ConnectionFailed,
    Faraday::TimeoutError
  ]
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, timeout: Constants::DEFAULT_TIMEOUT, retry_enabled: false) ⇒ Client

Initialize a new HTTP client

Parameters:

  • base_url (String)

    The base URL for the API

  • timeout (Integer) (defaults to: Constants::DEFAULT_TIMEOUT)

    Request timeout in seconds (default: Constants::DEFAULT_TIMEOUT)

  • retry_enabled (Boolean) (defaults to: false)

    Whether to enable retry logic (default: false)



27
28
29
30
# File 'lib/hyperliquid/client.rb', line 27

def initialize(base_url:, timeout: Constants::DEFAULT_TIMEOUT, retry_enabled: false)
  @retry_enabled = retry_enabled
  @connection = build_connection(base_url, timeout)
end

Instance Method Details

#post(endpoint, body = {}) ⇒ Hash, String

Make a POST request to the API

Parameters:

  • endpoint (String)

    The API endpoint to make the request to

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

    The request body as a hash (default: {})

Returns:

  • (Hash, String)

    The parsed JSON response or raw response body

Raises:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/hyperliquid/client.rb', line 44

def post(endpoint, body = {})
  response = @connection.post(endpoint) do |req|
    req.headers['Content-Type'] = 'application/json'
    req.body = body.to_json unless body.empty?
  end

  handle_response(response)
rescue Faraday::RetriableResponse => e
  # After retries are exhausted, Faraday throws a RetriableResponse
  # Catch and handle that here to bubble up the actual network error
  handle_response(e.response)
rescue Faraday::ConnectionFailed => e
  raise NetworkError, "Connection failed: #{e.message}"
rescue Faraday::TimeoutError => e
  raise TimeoutError, "Request timed out: #{e.message}"
end