mirror of
https://github.com/aljazceru/bitfinex-api-py.git
synced 2025-12-20 15:24:21 +01:00
Improve JSONEncoder class in bfxapi.utils.json_encoder.
This commit is contained in:
@@ -128,7 +128,7 @@ class RestAuthenticatedEndpoints(Middleware):
|
|||||||
all: bool = False) -> Notification[List[Order]]:
|
all: bool = False) -> Notification[List[Order]]:
|
||||||
body = {
|
body = {
|
||||||
"ids": ids, "cids": cids, "gids": gids,
|
"ids": ids, "cids": cids, "gids": gids,
|
||||||
"all": int(all)
|
"all": all
|
||||||
}
|
}
|
||||||
|
|
||||||
return _Notification[List[Order]](serializers.Order, is_iterable=True) \
|
return _Notification[List[Order]](serializers.Order, is_iterable=True) \
|
||||||
@@ -319,7 +319,7 @@ class RestAuthenticatedEndpoints(Middleware):
|
|||||||
rate: Optional[int] = None,
|
rate: Optional[int] = None,
|
||||||
period: Optional[int] = None) -> Notification[FundingAutoRenew]:
|
period: Optional[int] = None) -> Notification[FundingAutoRenew]:
|
||||||
body = {
|
body = {
|
||||||
"status": int(status), "currency": currency, "amount": amount,
|
"status": status, "currency": currency, "amount": amount,
|
||||||
"rate": rate, "period": period
|
"rate": rate, "period": period
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -449,7 +449,7 @@ class RestAuthenticatedEndpoints(Middleware):
|
|||||||
renew: bool = False) -> Notification[DepositAddress]:
|
renew: bool = False) -> Notification[DepositAddress]:
|
||||||
return _Notification[DepositAddress](serializers.DepositAddress) \
|
return _Notification[DepositAddress](serializers.DepositAddress) \
|
||||||
.parse(*self._post("auth/w/deposit/address", \
|
.parse(*self._post("auth/w/deposit/address", \
|
||||||
body={ "wallet": wallet, "method": method, "renew": int(renew) }))
|
body={ "wallet": wallet, "method": method, "renew": renew }))
|
||||||
|
|
||||||
def generate_deposit_invoice(self,
|
def generate_deposit_invoice(self,
|
||||||
wallet: str,
|
wallet: str,
|
||||||
|
|||||||
@@ -9,18 +9,20 @@ JSON = Union[Dict[str, "JSON"], List["JSON"], bool, int, float, str, Type[None]]
|
|||||||
def _strip(dictionary: Dict) -> Dict:
|
def _strip(dictionary: Dict) -> Dict:
|
||||||
return { key: value for key, value in dictionary.items() if value is not None }
|
return { key: value for key, value in dictionary.items() if value is not None }
|
||||||
|
|
||||||
def _convert_float_to_str(data: JSON) -> JSON:
|
def _convert_data_to_json(data: JSON) -> JSON:
|
||||||
|
if isinstance(data, bool):
|
||||||
|
return int(data)
|
||||||
if isinstance(data, float):
|
if isinstance(data, float):
|
||||||
return format(Decimal(repr(data)), "f")
|
return format(Decimal(repr(data)), "f")
|
||||||
if isinstance(data, list):
|
if isinstance(data, list):
|
||||||
return [ _convert_float_to_str(sub_data) for sub_data in data ]
|
return [ _convert_data_to_json(sub_data) for sub_data in data ]
|
||||||
if isinstance(data, dict):
|
if isinstance(data, dict):
|
||||||
return _strip({ key: _convert_float_to_str(value) for key, value in data.items() })
|
return _strip({ key: _convert_data_to_json(value) for key, value in data.items() })
|
||||||
return data
|
return data
|
||||||
|
|
||||||
class JSONEncoder(json.JSONEncoder):
|
class JSONEncoder(json.JSONEncoder):
|
||||||
def encode(self, o: JSON) -> str:
|
def encode(self, o: JSON) -> str:
|
||||||
return json.JSONEncoder.encode(self, _convert_float_to_str(o))
|
return json.JSONEncoder.encode(self, _convert_data_to_json(o))
|
||||||
|
|
||||||
def default(self, o: Any) -> Any:
|
def default(self, o: Any) -> Any:
|
||||||
if isinstance(o, Decimal):
|
if isinstance(o, Decimal):
|
||||||
|
|||||||
@@ -1,37 +1,36 @@
|
|||||||
from typing import TYPE_CHECKING, Callable, Awaitable, \
|
from typing import TYPE_CHECKING, Callable, Awaitable, \
|
||||||
Tuple, List, Union, Optional, Any
|
Tuple, List, Union, Optional, Any
|
||||||
|
|
||||||
from decimal import Decimal
|
|
||||||
|
|
||||||
from datetime import datetime
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from bfxapi.enums import \
|
from bfxapi.enums import \
|
||||||
OrderType, FundingOfferType
|
OrderType, FundingOfferType
|
||||||
|
|
||||||
from bfxapi.types import JSON
|
from bfxapi.types import JSON
|
||||||
|
|
||||||
_Handler = Callable[[str, Any], Awaitable[None]]
|
from decimal import Decimal
|
||||||
|
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
class BfxWebSocketInputs:
|
class BfxWebSocketInputs:
|
||||||
def __init__(self, handle_websocket_input: "_Handler") -> None:
|
def __init__(self, handle_websocket_input: Callable[[str, Any], Awaitable[None]]) -> None:
|
||||||
self.__handle_websocket_input = handle_websocket_input
|
self.__handle_websocket_input = handle_websocket_input
|
||||||
|
|
||||||
async def submit_order(self,
|
async def submit_order(self,
|
||||||
type: "OrderType",
|
type: "OrderType",
|
||||||
symbol: str,
|
symbol: str,
|
||||||
amount: Union[Decimal, float, str],
|
amount: Union["Decimal", float, str],
|
||||||
*,
|
*,
|
||||||
price: Optional[Union[Decimal, float, str]] = None,
|
price: Optional[Union["Decimal", float, str]] = None,
|
||||||
lev: Optional[int] = None,
|
lev: Optional[int] = None,
|
||||||
price_trailing: Optional[Union[Decimal, float, str]] = None,
|
price_trailing: Optional[Union["Decimal", float, str]] = None,
|
||||||
price_aux_limit: Optional[Union[Decimal, float, str]] = None,
|
price_aux_limit: Optional[Union["Decimal", float, str]] = None,
|
||||||
price_oco_stop: Optional[Union[Decimal, float, str]] = None,
|
price_oco_stop: Optional[Union["Decimal", float, str]] = None,
|
||||||
gid: Optional[int] = None,
|
gid: Optional[int] = None,
|
||||||
cid: Optional[int] = None,
|
cid: Optional[int] = None,
|
||||||
flags: Optional[int] = 0,
|
flags: Optional[int] = 0,
|
||||||
tif: Optional[Union[datetime, str]] = None,
|
tif: Optional[Union["datetime", str]] = None,
|
||||||
meta: Optional["JSON"] = None) -> None:
|
meta: Optional["JSON"] = None) -> None:
|
||||||
|
|
||||||
await self.__handle_websocket_input("on", {
|
await self.__handle_websocket_input("on", {
|
||||||
"type": type, "symbol": symbol, "amount": amount,
|
"type": type, "symbol": symbol, "amount": amount,
|
||||||
"price": price, "lev": lev, "price_trailing": price_trailing,
|
"price": price, "lev": lev, "price_trailing": price_trailing,
|
||||||
@@ -41,19 +40,19 @@ class BfxWebSocketInputs:
|
|||||||
})
|
})
|
||||||
|
|
||||||
async def update_order(self,
|
async def update_order(self,
|
||||||
id: int,
|
id: int,
|
||||||
*,
|
*,
|
||||||
amount: Optional[Union[Decimal, float, str]] = None,
|
amount: Optional[Union["Decimal", float, str]] = None,
|
||||||
price: Optional[Union[Decimal, float, str]] = None,
|
price: Optional[Union["Decimal", float, str]] = None,
|
||||||
cid: Optional[int] = None,
|
cid: Optional[int] = None,
|
||||||
cid_date: Optional[str] = None,
|
cid_date: Optional[str] = None,
|
||||||
gid: Optional[int] = None,
|
gid: Optional[int] = None,
|
||||||
flags: Optional[int] = 0,
|
flags: Optional[int] = 0,
|
||||||
lev: Optional[int] = None,
|
lev: Optional[int] = None,
|
||||||
delta: Optional[Union[Decimal, float, str]] = None,
|
delta: Optional[Union["Decimal", float, str]] = None,
|
||||||
price_aux_limit: Optional[Union[Decimal, float, str]] = None,
|
price_aux_limit: Optional[Union["Decimal", float, str]] = None,
|
||||||
price_trailing: Optional[Union[Decimal, float, str]] = None,
|
price_trailing: Optional[Union["Decimal", float, str]] = None,
|
||||||
tif: Optional[Union[datetime, str]] = None) -> None:
|
tif: Optional[Union["datetime", str]] = None) -> None:
|
||||||
await self.__handle_websocket_input("ou", {
|
await self.__handle_websocket_input("ou", {
|
||||||
"id": id, "amount": amount, "price": price,
|
"id": id, "amount": amount, "price": price,
|
||||||
"cid": cid, "cid_date": cid_date, "gid": gid,
|
"cid": cid, "cid_date": cid_date, "gid": gid,
|
||||||
@@ -62,34 +61,34 @@ class BfxWebSocketInputs:
|
|||||||
})
|
})
|
||||||
|
|
||||||
async def cancel_order(self,
|
async def cancel_order(self,
|
||||||
*,
|
*,
|
||||||
id: Optional[int] = None,
|
id: Optional[int] = None,
|
||||||
cid: Optional[int] = None,
|
cid: Optional[int] = None,
|
||||||
cid_date: Optional[str] = None) -> None:
|
cid_date: Optional[str] = None) -> None:
|
||||||
await self.__handle_websocket_input("oc", {
|
await self.__handle_websocket_input("oc", {
|
||||||
"id": id, "cid": cid, "cid_date": cid_date
|
"id": id, "cid": cid, "cid_date": cid_date
|
||||||
})
|
})
|
||||||
|
|
||||||
async def cancel_order_multi(self,
|
async def cancel_order_multi(self,
|
||||||
*,
|
*,
|
||||||
ids: Optional[List[int]] = None,
|
ids: Optional[List[int]] = None,
|
||||||
cids: Optional[List[Tuple[int, str]]] = None,
|
cids: Optional[List[Tuple[int, str]]] = None,
|
||||||
gids: Optional[List[int]] = None,
|
gids: Optional[List[int]] = None,
|
||||||
all: bool = False) -> None:
|
all: bool = False) -> None:
|
||||||
await self.__handle_websocket_input("oc_multi", {
|
await self.__handle_websocket_input("oc_multi", {
|
||||||
"ids": ids, "cids": cids, "gids": gids,
|
"ids": ids, "cids": cids, "gids": gids,
|
||||||
"all": int(all)
|
"all": all
|
||||||
})
|
})
|
||||||
|
|
||||||
#pylint: disable-next=too-many-arguments
|
#pylint: disable-next=too-many-arguments
|
||||||
async def submit_funding_offer(self,
|
async def submit_funding_offer(self,
|
||||||
type: "FundingOfferType",
|
type: "FundingOfferType",
|
||||||
symbol: str,
|
symbol: str,
|
||||||
amount: Union[Decimal, float, str],
|
amount: Union["Decimal", float, str],
|
||||||
rate: Union[Decimal, float, str],
|
rate: Union["Decimal", float, str],
|
||||||
period: int,
|
period: int,
|
||||||
*,
|
*,
|
||||||
flags: Optional[int] = 0) -> None:
|
flags: Optional[int] = 0) -> None:
|
||||||
await self.__handle_websocket_input("fon", {
|
await self.__handle_websocket_input("fon", {
|
||||||
"type": type, "symbol": symbol, "amount": amount,
|
"type": type, "symbol": symbol, "amount": amount,
|
||||||
"rate": rate, "period": period, "flags": flags
|
"rate": rate, "period": period, "flags": flags
|
||||||
|
|||||||
Reference in New Issue
Block a user