Class: Hyperliquid::Client
- Inherits:
-
Object
- Object
- Hyperliquid::Client
- 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
-
#initialize(base_url:, timeout: Constants::DEFAULT_TIMEOUT, retry_enabled: false) ⇒ Client
constructor
Initialize a new HTTP client.
-
#post(endpoint, body = {}) ⇒ Hash, String
Make a POST request to the API.
Constructor Details
#initialize(base_url:, timeout: Constants::DEFAULT_TIMEOUT, retry_enabled: false) ⇒ Client
Initialize a new HTTP client
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
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.}" rescue Faraday::TimeoutError => e raise TimeoutError, "Request timed out: #{e.}" end |