From c352c9b293b84326eeda462e95acbd8ac770a290 Mon Sep 17 00:00:00 2001 From: MoonGem <34537029+MoonGem@users.noreply.github.com> Date: Fri, 23 Mar 2018 16:44:27 -0500 Subject: [PATCH] API key error handling. --- freqtrade/exchange/bittrex.py | 102 ++++++++++++++++++++++------------ 1 file changed, 66 insertions(+), 36 deletions(-) diff --git a/freqtrade/exchange/bittrex.py b/freqtrade/exchange/bittrex.py index 0cba621af..002aa2ddf 100644 --- a/freqtrade/exchange/bittrex.py +++ b/freqtrade/exchange/bittrex.py @@ -62,49 +62,64 @@ class Bittrex(Exchange): def buy(self, pair: str, rate: float, amount: float) -> str: data = _API.buy_limit(pair.replace('_', '-'), amount, rate) if not data['success']: - Bittrex._validate_response(data) - raise OperationalException('{message} params=({pair}, {rate}, {amount})'.format( - message=data['message'], - pair=pair, - rate=rate, - amount=amount)) + if 'APIKEY_INVALID' in str(data['message']): + print('Api Key...') + else: + Bittrex._validate_response(data) + raise OperationalException('{message} params=({pair}, {rate}, {amount})'.format( + message=data['message'], + pair=pair, + rate=rate, + amount=amount)) return data['result']['uuid'] def sell(self, pair: str, rate: float, amount: float) -> str: data = _API.sell_limit(pair.replace('_', '-'), amount, rate) if not data['success']: - Bittrex._validate_response(data) - raise OperationalException('{message} params=({pair}, {rate}, {amount})'.format( - message=data['message'], - pair=pair, - rate=rate, - amount=amount)) + if 'APIKEY_INVALID' in str(data['message']): + print('Api Key...') + else: + Bittrex._validate_response(data) + raise OperationalException('{message} params=({pair}, {rate}, {amount})'.format( + message=data['message'], + pair=pair, + rate=rate, + amount=amount)) return data['result']['uuid'] def get_balance(self, currency: str) -> float: data = _API.get_balance(currency) if not data['success']: - Bittrex._validate_response(data) - raise OperationalException('{message} params=({currency})'.format( - message=data['message'], - currency=currency)) + if 'APIKEY_INVALID' in str(data['message']): + print('Api Key...') + else: + Bittrex._validate_response(data) + raise OperationalException('{message} params=({currency})'.format( + message=data['message'], + currency=currency)) return float(data['result']['Balance'] or 0.0) def get_balances(self): data = _API.get_balances() if not data['success']: - Bittrex._validate_response(data) - raise OperationalException('{message}'.format(message=data['message'])) + if 'APIKEY_INVALID' in str(data['message']): + print('Api Key...') + else: + Bittrex._validate_response(data) + raise OperationalException('{message}'.format(message=data['message'])) return data['result'] def get_ticker(self, pair: str, refresh: Optional[bool] = True) -> dict: if refresh or pair not in self.cached_ticker.keys(): data = _API.get_ticker(pair.replace('_', '-')) if not data['success']: - Bittrex._validate_response(data) - raise OperationalException('{message} params=({pair})'.format( - message=data['message'], - pair=pair)) + if 'APIKEY_INVALID' in str(data['message']): + print('Api Key...') + else: + Bittrex._validate_response(data) + raise OperationalException('{message} params=({pair})'.format( + message=data['message'], + pair=pair)) keys = ['Bid', 'Ask', 'Last'] if not data.get('result') or\ not all(key in data.get('result', {}) for key in keys) or\ @@ -147,10 +162,13 @@ class Bittrex(Exchange): 'in response params=({})'.format(prop, pair)) if not data['success']: - Bittrex._validate_response(data) - raise OperationalException('{message} params=({pair})'.format( - message=data['message'], - pair=pair)) + if 'APIKEY_INVALID' in str(data['message']): + print('Api Key...') + else: + Bittrex._validate_response(data) + raise OperationalException('{message} params=({pair})'.format( + message=data['message'], + pair=pair)) return data['result'] @@ -176,10 +194,13 @@ class Bittrex(Exchange): def cancel_order(self, order_id: str) -> None: data = _API.cancel(order_id) if not data['success']: - Bittrex._validate_response(data) - raise OperationalException('{message} params=({order_id})'.format( - message=data['message'], - order_id=order_id)) + if 'APIKEY_INVALID' in str(data['message']): + print('Api Key...') + else: + Bittrex._validate_response(data) + raise OperationalException('{message} params=({order_id})'.format( + message=data['message'], + order_id=order_id)) def get_pair_detail_url(self, pair: str) -> str: return self.PAIR_DETAIL_METHOD + '?MarketName={}'.format(pair.replace('_', '-')) @@ -187,22 +208,31 @@ class Bittrex(Exchange): def get_markets(self) -> List[str]: data = _API.get_markets() if not data['success']: - Bittrex._validate_response(data) - raise OperationalException(data['message']) + if 'APIKEY_INVALID' in str(data['message']): + print('Api Key...') + else: + Bittrex._validate_response(data) + raise OperationalException(data['message']) return [m['MarketName'].replace('-', '_') for m in data['result']] def get_market_summaries(self) -> List[Dict]: data = _API.get_market_summaries() if not data['success']: - Bittrex._validate_response(data) - raise OperationalException(data['message']) + if 'APIKEY_INVALID' in str(data['message']): + print('Api Key...') + else: + Bittrex._validate_response(data) + raise OperationalException(data['message']) return data['result'] def get_wallet_health(self) -> List[Dict]: data = _API_V2.get_wallet_health() if not data['success']: - Bittrex._validate_response(data) - raise OperationalException(data['message']) + if 'APIKEY_INVALID' in str(data['message']): + print('Api Key...') + else: + Bittrex._validate_response(data) + raise OperationalException(data['message']) return [{ 'Currency': entry['Health']['Currency'], 'IsActive': entry['Health']['IsActive'],