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, explorer_base_url: nil) ⇒ Client
constructor
Initialize a new HTTP client.
-
#post(endpoint, body = {}, target: :default) ⇒ Hash, String
Make a POST request to the API.
Constructor Details
#initialize(base_url:, timeout: Constants::DEFAULT_TIMEOUT, retry_enabled: false, explorer_base_url: nil) ⇒ Client
Initialize a new HTTP client
29 30 31 32 33 34 35 36 |
# File 'lib/hyperliquid/client.rb', line 29 def initialize(base_url:, timeout: Constants::DEFAULT_TIMEOUT, retry_enabled: false, explorer_base_url: nil) @retry_enabled = retry_enabled @timeout = timeout @explorer_base_url = explorer_base_url @connection = build_connection(base_url) @explorer_connection = nil end |
Instance Method Details
#post(endpoint, body = {}, target: :default) ⇒ Hash, String
Make a POST request to the API
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/hyperliquid/client.rb', line 52 def post(endpoint, body = {}, target: :default) connection = connection_for(target) 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 |