From 26504044a64d28603056411b60627272da2aa357 Mon Sep 17 00:00:00 2001 From: Thierry Deruyttere Date: Thu, 5 Oct 2017 16:31:23 +0200 Subject: [PATCH] Added timer for api call rate limit (#56) * added timer * naming * forgot old var --- bittrex/bittrex.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/bittrex/bittrex.py b/bittrex/bittrex.py index 68cc9ac..6ad6a4b 100644 --- a/bittrex/bittrex.py +++ b/bittrex/bittrex.py @@ -75,10 +75,12 @@ class Bittrex(object): """ Used for requesting Bittrex with API key and API secret """ - def __init__(self, api_key, api_secret, dispatch=using_requests): + def __init__(self, api_key, api_secret, calls_per_second=1, dispatch=using_requests): self.api_key = str(api_key) if api_key is not None else '' self.api_secret = str(api_secret) if api_secret is not None else '' self.dispatch = dispatch + self.call_rate = 1.0/calls_per_second + self.last_call = None def decrypt(self): if encrypted: @@ -96,6 +98,18 @@ def decrypt(self): else: raise ImportError('"pycrypto" module has to be installed') + def wait(self): + if self.last_call is None: + self.last_call = time.time() + else: + now = time.time() + passed = now - self.last_call + if passed < self.call_rate: + #print("sleep") + time.sleep(1.0 - passed) + + self.last_call = time.time() + def api_query(self, method, options=None): """ Queries Bittrex with given method and options. @@ -128,6 +142,9 @@ def api_query(self, method, options=None): apisign = hmac.new(self.api_secret.encode(), request_url.encode(), hashlib.sha512).hexdigest() + + self.wait() + return self.dispatch(request_url, apisign) def get_markets(self):