Class: Cryptohopper::Client

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

Overview

Synchronous Cryptohopper API client.

Examples:

ch = Cryptohopper::Client.new(api_key: ENV.fetch("CRYPTOHOPPER_TOKEN"))
me = ch.user.get
ticker = ch.exchange.ticker(exchange: "binance", market: "BTC/USDT")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, app_key: nil, base_url: nil, timeout: DEFAULT_TIMEOUT, max_retries: DEFAULT_MAX_RETRIES, user_agent: nil) ⇒ Client

Returns a new instance of Client.

Parameters:

  • api_key (String)

    40-char OAuth2 bearer token.

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

    Optional OAuth client_id, sent as ‘x-api-app-key`.

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

    Override for staging.

  • timeout (Integer, Float) (defaults to: DEFAULT_TIMEOUT)

    Per-request timeout in seconds.

  • max_retries (Integer) (defaults to: DEFAULT_MAX_RETRIES)

    Retries on 429 (respecting Retry-After).

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

    Appended after ‘cryptohopper-sdk-ruby/<v>`.

Raises:

  • (ArgumentError)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cryptohopper/client.rb', line 53

def initialize(api_key:, app_key: nil, base_url: nil, timeout: DEFAULT_TIMEOUT,
               max_retries: DEFAULT_MAX_RETRIES, user_agent: nil)
  raise ArgumentError, "api_key is required" if api_key.nil? || api_key.empty?

  @api_key = api_key
  @app_key = app_key
  @base_url = (base_url || DEFAULT_BASE_URL).chomp("/")
  @timeout = timeout
  @max_retries = max_retries
  @user_agent_suffix = user_agent

  @user = Resources::User.new(self)
  @hoppers = Resources::Hoppers.new(self)
  @exchange = Resources::Exchange.new(self)
  @strategy = Resources::Strategies.new(self)
  @backtest = Resources::Backtests.new(self)
  @market = Resources::Market.new(self)
  @signals = Resources::Signals.new(self)
  @arbitrage = Resources::Arbitrage.new(self)
  @marketmaker = Resources::MarketMaker.new(self)
  @template = Resources::Templates.new(self)
  @ai = Resources::AI.new(self)
  @platform = Resources::Platform.new(self)
  @chart = Resources::Chart.new(self)
  @subscription = Resources::Subscription.new(self)
  @social = Resources::Social.new(self)
  @tournaments = Resources::Tournaments.new(self)
  @webhooks = Resources::Webhooks.new(self)
  @app = Resources::App.new(self)
end

Instance Attribute Details

#aiObject (readonly)

Returns the value of attribute ai.



40
41
42
# File 'lib/cryptohopper/client.rb', line 40

def ai
  @ai
end

#appObject (readonly)

Returns the value of attribute app.



40
41
42
# File 'lib/cryptohopper/client.rb', line 40

def app
  @app
end

#arbitrageObject (readonly)

Returns the value of attribute arbitrage.



40
41
42
# File 'lib/cryptohopper/client.rb', line 40

def arbitrage
  @arbitrage
end

#backtestObject (readonly)

Returns the value of attribute backtest.



40
41
42
# File 'lib/cryptohopper/client.rb', line 40

def backtest
  @backtest
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



40
41
42
# File 'lib/cryptohopper/client.rb', line 40

def base_url
  @base_url
end

#chartObject (readonly)

Returns the value of attribute chart.



40
41
42
# File 'lib/cryptohopper/client.rb', line 40

def chart
  @chart
end

#exchangeObject (readonly)

Returns the value of attribute exchange.



40
41
42
# File 'lib/cryptohopper/client.rb', line 40

def exchange
  @exchange
end

#hoppersObject (readonly)

Returns the value of attribute hoppers.



40
41
42
# File 'lib/cryptohopper/client.rb', line 40

def hoppers
  @hoppers
end

#marketObject (readonly)

Returns the value of attribute market.



40
41
42
# File 'lib/cryptohopper/client.rb', line 40

def market
  @market
end

#marketmakerObject (readonly)

Returns the value of attribute marketmaker.



40
41
42
# File 'lib/cryptohopper/client.rb', line 40

def marketmaker
  @marketmaker
end

#platformObject (readonly)

Returns the value of attribute platform.



40
41
42
# File 'lib/cryptohopper/client.rb', line 40

def platform
  @platform
end

#signalsObject (readonly)

Returns the value of attribute signals.



40
41
42
# File 'lib/cryptohopper/client.rb', line 40

def signals
  @signals
end

#socialObject (readonly)

Returns the value of attribute social.



40
41
42
# File 'lib/cryptohopper/client.rb', line 40

def social
  @social
end

#strategyObject (readonly)

Returns the value of attribute strategy.



40
41
42
# File 'lib/cryptohopper/client.rb', line 40

def strategy
  @strategy
end

#subscriptionObject (readonly)

Returns the value of attribute subscription.



40
41
42
# File 'lib/cryptohopper/client.rb', line 40

def subscription
  @subscription
end

#templateObject (readonly)

Returns the value of attribute template.



40
41
42
# File 'lib/cryptohopper/client.rb', line 40

def template
  @template
end

#tournamentsObject (readonly)

Returns the value of attribute tournaments.



40
41
42
# File 'lib/cryptohopper/client.rb', line 40

def tournaments
  @tournaments
end

#userObject (readonly)

Returns the value of attribute user.



40
41
42
# File 'lib/cryptohopper/client.rb', line 40

def user
  @user
end

#webhooksObject (readonly)

Returns the value of attribute webhooks.



40
41
42
# File 'lib/cryptohopper/client.rb', line 40

def webhooks
  @webhooks
end

Instance Method Details

#_request(method, path, params: nil, body: nil, max_retries: 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.

Internal transport. Resources call this. Users shouldn’t.



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/cryptohopper/client.rb', line 87

def _request(method, path, params: nil, body: nil, max_retries: nil)
  retries = max_retries || @max_retries
  attempt = 0
  loop do
    return do_request(method, path, params: params, body: body)
  rescue Error => e
    raise unless e.code == "RATE_LIMITED" && attempt < retries

    wait = e.retry_after_ms ? e.retry_after_ms / 1000.0 : (2**attempt)
    sleep(wait)
    attempt += 1
  end
end