Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/valr_python/rest_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1019,4 +1019,4 @@ def delete_order(self, currency_pair: Union[str, CurrencyPair], order_id: str =
data["orderId"] = order_id
else:
data["customerOrderId"] = customer_order_id
return self._do('DELETE', '/v1/orders/order', data=data, is_authenticated=True, subaccount_id=subaccount_id)
return self._do_delete('DELETE', '/v1/orders/order', data=data, is_authenticated=True, subaccount_id=subaccount_id)
38 changes: 32 additions & 6 deletions src/valr_python/rest_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,15 @@ def _do(self, method: str, path: str, data: Optional[Dict] = None, params: Optio

try:
res.raise_for_status()
e = res.json()
self._raise_for_api_error(e)
# provide warning with bundled response dict for incomplete transactions
if res.status_code == 202:
warnings.warn(IncompleteOrderWarning(data=e, message="Order processing incomplete"))
return e
if res.text:
e = res.json()
self._raise_for_api_error(e)
# provide warning with bundled response dict for incomplete transactions
if res.status_code == 202:
warnings.warn(IncompleteOrderWarning(data=e, message="Order processing incomplete"))
return e
else:
return res.raise_for_status()
except HTTPError as he:
print(he)
if res.status_code == 429:
Expand All @@ -107,3 +110,26 @@ def _do(self, method: str, path: str, data: Optional[Dict] = None, params: Optio
except JSONDecodeError as jde:
raise RESTAPIException(res.status_code,
f'valr-python: unknown API error. HTTP ({res.status_code}): {jde.msg}')


def _do_delete(self, method: str, path: str, data: Optional[Dict] = None, params: Optional[Dict] = None,
is_authenticated: bool = False, subaccount_id: str = '') -> Optional[Union[List, Dict]]:
"""Executes API request and returns the response.

Includes HTTP 429 handling by honouring VALR's 429 Retry-After header cool-down.
"""
headers = {}
if data:
data = json.dumps(data, cls=DecimalEncoder) # serialize decimals as str
headers["Content-Type"] = "application/json"
params_str = parse.urlencode(params, safe=":") if params else None
if is_authenticated:
# todo - fix data processing in valr headers
headers.update(_get_valr_headers(api_key=self.api_key, api_secret=self.api_secret, method=method,
path=f'{path}?{params_str}' if params_str else path, data=data,
subaccount_id=subaccount_id))
url = self._base_url + '/' + path.lstrip('/')
args = dict(timeout=self._timeout, data=data, headers=headers)
if params_str:
args['params'] = params_str
return self._session.request(method, url, **args)