Skip to content

Commit

Permalink
fixed empty response bug on public REST endpoints; edited README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
btschwertfeger committed Jul 13, 2022
1 parent 9a52192 commit e333a19
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,7 @@ Logging messages are enabled, so you can configure your own logger to track ever
- https://docs.kraken.com/websockets
- https://docs.kraken.com/rest/
- https://support.kraken.com/hc/en-us/sections/360012894412-Futures-API
<!-- ## Notes: -->

## Notes:
- Pull requests will be ignored until the owner finished the core idea
<!-- - Triggers: stop-loss, stop-loss-limit, take-profit and take-profit-limit orders. -->
19 changes: 9 additions & 10 deletions kraken/base_api/base_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ def __init__(self, key: str='', secret: str='', url: str='', futures: bool=False
self._api_v = ''
if url: self.url = url
elif futures:
if sandbox: self.url = 'https://demo-futures.kraken.com/derivatives'
else: self.url = 'https://futures.kraken.com/derivatives'
self._api_v = '/api/v3'
if sandbox: self.url = 'https://demo-futures.kraken.com'
else: self.url = 'https://futures.kraken.com'
raise ValueError('Futures endpoints and clients not implemented yet.')
else:
self.url = 'https://api.kraken.com'
Expand All @@ -32,7 +31,6 @@ def __init__(self, key: str='', secret: str='', url: str='', futures: bool=False
def _request(self, method: str, uri: str, timeout: int=10, auth: bool=True, params: dict={}, do_json: bool=False, return_raw: bool=False):
uri_path = uri
data_json = ''
params['nonce'] = str(int(time.time()*1000)) # generate nonce

if method.upper() in ['GET', 'DELETE']:
if params:
Expand All @@ -46,6 +44,7 @@ def _request(self, method: str, uri: str, timeout: int=10, auth: bool=True, para
headers = {}
if auth:
if not self.key or self.key == '' or not self.secret or self.secret == '': raise ValueError('Missing credentials')
params['nonce'] = str(int(time.time()*1000)) # generate nonce
headers = {
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
'API-Key': self.key,
Expand All @@ -55,10 +54,10 @@ 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.info(f'Request: {url}')

if method in ['GET', 'DELETE']:
response_data = requests.request(method, url, headers=headers, timeout=timeout)
return self.check_response_data(requests.request(method, url, headers=headers, timeout=timeout), return_raw)
else:
if do_json:
return self.check_response_data(requests.request(method, url, headers=headers, json=params, timeout=timeout), return_raw)
Expand All @@ -83,10 +82,10 @@ def check_response_data(response_data, return_raw: bool=False):
except ValueError:
raise Exception(response_data.content)
else:
if len(data.get('error')) == 0:
if data.get('result'): return data['result']
else: return data
else: raise Exception(f'{response_data.status_code}-{response_data.text}')
if 'error' in data:
if len(data.get('error')) == 0 and data.get('result'): 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}')

@property
Expand Down

0 comments on commit e333a19

Please sign in to comment.