Skip to content

Commit

Permalink
fixed exception on empty user response request
Browse files Browse the repository at this point in the history
  • Loading branch information
btschwertfeger committed Sep 18, 2022
1 parent ebb0bb5 commit 0bf2c86
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion examples/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def main() -> None:
# print(user.get_orders_info(txid='someid')) # or txid='id1,id2,id3' or txid=['id1','id2']
# print(user.get_trades_history())
# print(user.get_trades_info(txid='someid'))
# print(user.get_open_positions())#txid='someid'
print(user.get_open_positions())#txid='someid'
# print(user.get_ledgers_info())#asset='BTC' or asset='BTC,EUR' or asset=['BTC','EUR']
# print(user.get_ledgers(id='LNBK7T-BLEFU-C6NGIS'))
# print(user.get_trade_volume())#pair='BTC/EUR'
Expand Down
2 changes: 1 addition & 1 deletion kraken/__version__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
VERSION = (0, 5, 1)
VERSION = (0, 5, 2)

__version__ = '.'.join(map(str, VERSION))
6 changes: 3 additions & 3 deletions kraken/base_api/base_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def _request(self, method: str, uri: str, timeout: int=10, auth: bool=True, para
headers['User-Agent'] = 'Kraken-Python-SDK'
url = f'{self.url}{self._api_v}{uri}'

# logging.info(f'Request: {url}')
# logging.debug(f'Request to: {url}')

if method in ['GET', 'DELETE']:
return self.check_response_data(requests.request(method, url, headers=headers, timeout=timeout), return_raw)
Expand All @@ -75,15 +75,15 @@ def get_kraken_signature(self, urlpath: str, data: dict):

@staticmethod
def check_response_data(response_data, return_raw: bool=False):
if response_data.status_code == 200:
if response_data.status_code in [ '200', 200 ]:
if return_raw: return response_data
try:
data = response_data.json()
except ValueError:
raise Exception(response_data.content)
else:
if 'error' in data:
if len(data.get('error')) == 0 and data.get('result'): return data['result']
if len(data.get('error')) == 0 and 'result' in data: return data['result']
else: raise Exception(f'{response_data.status_code}-{response_data.text}')
else: return data
else: raise Exception(f'{response_data.status_code}-{response_data.text}')
Expand Down
17 changes: 10 additions & 7 deletions kraken/spot/websocket/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


class ConnectSpotWebsocket:
MAX_RECONNECTS = 5
MAX_SEND_MESSAGE_RETRIES = 5
MAX_RECONNECT_SECONDS = 60

def __init__(self, client, endpoint: str, callback=None, private: bool=False, beta: bool=False):
Expand All @@ -30,6 +30,8 @@ def __init__(self, client, endpoint: str, callback=None, private: bool=False, be

asyncio.ensure_future(self.run_forever(), loop=asyncio.get_running_loop())

self.connected = False

@property
def subscriptions(self) -> list:
return self._subscriptions
Expand All @@ -51,6 +53,7 @@ async def _run(self, event: asyncio.Event):
if self.private: self._client.websocket_priv = self
else: self._client.websocket_pub = self

self.connected = True
self._socket = socket
self._reconnect_num = 0

Expand Down Expand Up @@ -85,9 +88,9 @@ async def _reconnect(self):

self._reconnect_num += 1
reconnect_wait = self._get_reconnect_wait(self._reconnect_num)
logging.info(f'asyncio sleep reconnect_wait={reconnect_wait} s reconnect_num={self._reconnect_num}')
logging.debug(f'asyncio sleep reconnect_wait={reconnect_wait} s reconnect_num={self._reconnect_num}')
await asyncio.sleep(reconnect_wait)
logging.info(f'asyncio sleep ok')
logging.debug(f'asyncio sleep done')
event = asyncio.Event()

tasks = {
Expand Down Expand Up @@ -143,9 +146,9 @@ async def send_ping(self):

async def send_message(self, msg, private: bool=False, retry_count: int=0):
logging.info(f'send_message (private: {private}; tries: {retry_count}): {msg}')
if not self._socket:
if retry_count < self.MAX_RECONNECTS:
await asyncio.sleep(1)
if not self._socket: # if not connected
if retry_count < self.MAX_SEND_MESSAGE_RETRIES:
await asyncio.sleep(2)
await self.send_message(msg, private=private, retry_count=retry_count + 1)
else:
msg['reqid'] = int(time.time() * 1000)
Expand All @@ -165,7 +168,7 @@ class KrakenSpotWSClient(object):
def __init__(self, client, callback=None, beta: bool=False):
self._callback = callback
self._client = client

self._pub_conn = ConnectSpotWebsocket(
client=self._client,
endpoint=self.PROD_ENV_URL if not beta else BETA_ENV_URL,
Expand Down

0 comments on commit 0bf2c86

Please sign in to comment.