diff --git a/src/valr_python/rest_base.py b/src/valr_python/rest_base.py index 703f7af..b18f8a6 100644 --- a/src/valr_python/rest_base.py +++ b/src/valr_python/rest_base.py @@ -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) diff --git a/src/valr_python/rest_client.py b/src/valr_python/rest_client.py index 8efce2b..0869399 100644 --- a/src/valr_python/rest_client.py +++ b/src/valr_python/rest_client.py @@ -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: @@ -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)