Add implementation for submit_order, update_order and cancel_order endpoint handlers in BfxRestInterface.py.

This commit is contained in:
Davide Casale
2022-12-22 17:07:36 +01:00
parent 79ae0b48e0
commit d5ace49555
2 changed files with 20 additions and 8 deletions

View File

@@ -10,6 +10,8 @@ from .typings import *
from .enums import OrderType, Config, Precision, Sort from .enums import OrderType, Config, Precision, Sort
from .exceptions import ResourceNotFound, RequestParametersError, InvalidAuthenticationCredentials, UnknownGenericError from .exceptions import ResourceNotFound, RequestParametersError, InvalidAuthenticationCredentials, UnknownGenericError
from .. utils.encoder import JSONEncoder
class BfxRestInterface(object): class BfxRestInterface(object):
def __init__(self, host, API_KEY = None, API_SECRET = None): def __init__(self, host, API_KEY = None, API_SECRET = None):
self.public = _RestPublicEndpoints(host=host) self.public = _RestPublicEndpoints(host=host)
@@ -58,7 +60,7 @@ class _Requests(object):
if _append_authentication_headers: if _append_authentication_headers:
headers = { **headers, **self.__build_authentication_headers(endpoint, data) } headers = { **headers, **self.__build_authentication_headers(endpoint, data) }
response = requests.post(f"{self.host}/{endpoint}", params=params, data=json.dumps(data), headers=headers) response = requests.post(f"{self.host}/{endpoint}", params=params, data=json.dumps(data, cls=JSONEncoder), headers=headers)
if response.status_code == HTTPStatus.NOT_FOUND: if response.status_code == HTTPStatus.NOT_FOUND:
raise ResourceNotFound(f"No resources found at endpoint <{endpoint}>.") raise ResourceNotFound(f"No resources found at endpoint <{endpoint}>.")
@@ -252,4 +254,20 @@ class _RestAuthenticatedEndpoints(_Requests):
"flags": flags, "tif": tif, "meta": meta "flags": flags, "tif": tif, "meta": meta
} }
return self._POST("auth/w/order/submit", data=data) return serializers.Notification.parse(*self._POST("auth/w/order/submit", data=data))
def update_order(self, id: Union[Int64, int], amount: Optional[Union[Decimal, str]] = None, price: Optional[Union[Decimal, str]] = None,
cid: Optional[Union[Int45, int]] = None, cid_date: Optional[str] = None, gid: Optional[Union[Int32, int]] = None,
flags: Optional[Union[Int16, int]] = None, lev: Optional[Union[Int32, int]] = None, delta: Optional[Union[Decimal, str]] = None,
price_aux_limit: Optional[Union[Decimal, str]] = None, price_trailing: Optional[Union[Decimal, str]] = None, tif: Optional[Union[datetime, str]] = None) -> Notification:
data = {
"id": id, "amount": amount, "price": price,
"cid": cid, "cid_date": cid_date, "gid": gid,
"flags": flags, "lev": lev, "delta": delta,
"price_aux_limit": price_aux_limit, "price_trailing": price_trailing, "tif": tif
}
return serializers.Notification.parse(*self._POST("auth/w/order/update", data=data))
def cancel_order(self, id: Union[Int64, int]) -> Notification:
return serializers.Notification.parse(*self._POST("auth/w/order/cancel", data={ "id": id }))

View File

@@ -1,11 +1,5 @@
from decimal import Decimal
from datetime import datetime
from typing import Type, Tuple, List, Dict, TypedDict, Union, Optional, Any from typing import Type, Tuple, List, Dict, TypedDict, Union, Optional, Any
from ..utils.integers import Int16, Int32, Int45, Int64
JSON = Union[Dict[str, "JSON"], List["JSON"], bool, int, float, str, Type[None]] JSON = Union[Dict[str, "JSON"], List["JSON"], bool, int, float, str, Type[None]]
#region Type hinting for Rest Public Endpoints #region Type hinting for Rest Public Endpoints