Merge pull request #32 from Davi0kProgramsThings/fix/refactoring

Merge branch `fix/refactoring` in branch `feature/rest`.
This commit is contained in:
Davide Casale
2023-02-10 03:07:10 +01:00
committed by GitHub
3 changed files with 14 additions and 14 deletions

View File

@@ -14,7 +14,7 @@ class RestAuthenticatedEndpoints(Middleware):
def get_user_info(self) -> UserInfo: def get_user_info(self) -> UserInfo:
return serializers.UserInfo.parse(*self._POST(f"auth/r/info/user")) return serializers.UserInfo.parse(*self._POST(f"auth/r/info/user"))
def get_login_history(self) -> LoginHistory: def get_login_history(self) -> List[LoginHistory]:
return [ serializers.LoginHistory.parse(*sub_data) for sub_data in self._POST("auth/r/logins/hist") ] return [ serializers.LoginHistory.parse(*sub_data) for sub_data in self._POST("auth/r/logins/hist") ]
def get_balance_available_for_orders_or_offers(self, symbol: str, type: str, dir: Optional[int] = None, rate: Optional[str] = None, lev: Optional[str] = None) -> BalanceAvailable: def get_balance_available_for_orders_or_offers(self, symbol: str, type: str, dir: Optional[int] = None, rate: Optional[str] = None, lev: Optional[str] = None) -> BalanceAvailable:
@@ -125,8 +125,8 @@ class RestAuthenticatedEndpoints(Middleware):
def get_symbol_margin_info(self, symbol: str) -> SymbolMarginInfo: def get_symbol_margin_info(self, symbol: str) -> SymbolMarginInfo:
response = self._POST(f"auth/r/info/margin/{symbol}") response = self._POST(f"auth/r/info/margin/{symbol}")
data = [response[1]] + response[2]
return serializers.SymbolMarginInfo.parse(*([response[1]] + response[2])) return serializers.SymbolMarginInfo.parse(*data)
def get_all_symbols_margin_info(self) -> List[SymbolMarginInfo]: def get_all_symbols_margin_info(self) -> List[SymbolMarginInfo]:
return [ serializers.SymbolMarginInfo.parse(*([sub_data[1]] + sub_data[2])) for sub_data in self._POST(f"auth/r/info/margin/sym_all") ] return [ serializers.SymbolMarginInfo.parse(*([sub_data[1]] + sub_data[2])) for sub_data in self._POST(f"auth/r/info/margin/sym_all") ]
@@ -146,10 +146,8 @@ class RestAuthenticatedEndpoints(Middleware):
def get_increase_position_info(self, symbol: str, amount: Union[Decimal, float, str]) -> PositionIncreaseInfo: def get_increase_position_info(self, symbol: str, amount: Union[Decimal, float, str]) -> PositionIncreaseInfo:
response = self._POST(f"auth/r/position/increase/info", body={ "symbol": symbol, "amount": amount }) response = self._POST(f"auth/r/position/increase/info", body={ "symbol": symbol, "amount": amount })
data = response[0] + [response[1][0]] + response[1][1] + [response[1][2]] + response[4] + response[5]
return serializers.PositionIncreaseInfo.parse(*( return serializers.PositionIncreaseInfo.parse(*data)
response[0] + [response[1][0]] + response[1][1] + [response[1][2]] + response[4] + response[5]
))
def get_positions_history(self, start: Optional[str] = None, end: Optional[str] = None, limit: Optional[int] = None) -> List[PositionHistory]: def get_positions_history(self, start: Optional[str] = None, end: Optional[str] = None, limit: Optional[int] = None) -> List[PositionHistory]:
return [ serializers.PositionHistory.parse(*sub_data) for sub_data in self._POST("auth/r/positions/hist", body={ "start": start, "end": end, "limit": limit }) ] return [ serializers.PositionHistory.parse(*sub_data) for sub_data in self._POST("auth/r/positions/hist", body={ "start": start, "end": end, "limit": limit }) ]
@@ -277,8 +275,8 @@ class RestAuthenticatedEndpoints(Middleware):
def get_funding_info(self, key: str) -> FundingInfo: def get_funding_info(self, key: str) -> FundingInfo:
response = self._POST(f"auth/r/info/funding/{key}") response = self._POST(f"auth/r/info/funding/{key}")
data = [response[1]] + response[2]
return serializers.FundingInfo.parse(*([response[1]] + response[2])) return serializers.FundingInfo.parse(*data)
def transfer_between_wallets(self, from_wallet: str, to_wallet: str, currency: str, currency_to: str, amount: Union[Decimal, float, str]) -> Notification[Transfer]: def transfer_between_wallets(self, from_wallet: str, to_wallet: str, currency: str, currency_to: str, amount: Union[Decimal, float, str]) -> Notification[Transfer]:
body = { body = {

View File

@@ -1,6 +1,6 @@
import time, hmac, hashlib, json, requests import time, hmac, hashlib, json, requests
from typing import TYPE_CHECKING, Optional, Any from typing import TYPE_CHECKING, Optional, Any, cast
from http import HTTPStatus from http import HTTPStatus
from ..enums import Error from ..enums import Error
@@ -15,7 +15,7 @@ class Middleware(object):
def __init__(self, host: str, API_KEY: Optional[str] = None, API_SECRET: Optional[str] = None): def __init__(self, host: str, API_KEY: Optional[str] = None, API_SECRET: Optional[str] = None):
self.host, self.API_KEY, self.API_SECRET = host, API_KEY, API_SECRET self.host, self.API_KEY, self.API_SECRET = host, API_KEY, API_SECRET
def __build_authentication_headers(self, endpoint: str, data: str): def __build_authentication_headers(self, endpoint: str, data: Optional[str] = None):
assert isinstance(self.API_KEY, str) and isinstance(self.API_SECRET, str), \ assert isinstance(self.API_KEY, str) and isinstance(self.API_SECRET, str), \
"API_KEY and API_SECRET must be both str to call __build_authentication_headers" "API_KEY and API_SECRET must be both str to call __build_authentication_headers"
@@ -69,7 +69,7 @@ class Middleware(object):
data = response.json() data = response.json()
if len(data) and data[0] == "error": if isinstance(data, list) and len(data) and data[0] == "error":
if data[1] == Error.ERR_PARAMS: if data[1] == Error.ERR_PARAMS:
raise RequestParametersError(f"The request was rejected with the following parameter error: <{data[2]}>") raise RequestParametersError(f"The request was rejected with the following parameter error: <{data[2]}>")

View File

@@ -38,7 +38,8 @@ class BfxWebsocketClient(object):
self.host, self.websocket, self.event_emitter = host, None, AsyncIOEventEmitter() self.host, self.websocket, self.event_emitter = host, None, AsyncIOEventEmitter()
self.event_emitter.add_listener("error", self.event_emitter.add_listener("error",
lambda exception: self.logger.error("\n" + str().join(traceback.format_exception(type(exception), exception, exception.__traceback__))[:-1]) lambda exception: self.logger.error(str(exception) + "\n" +
str().join(traceback.format_exception(type(exception), exception, exception.__traceback__))[:-1])
) )
self.API_KEY, self.API_SECRET, self.filter, self.authentication = API_KEY, API_SECRET, filter, False self.API_KEY, self.API_SECRET, self.filter, self.authentication = API_KEY, API_SECRET, filter, False
@@ -52,7 +53,8 @@ class BfxWebsocketClient(object):
self.logger = CustomLogger("BfxWebsocketClient", logLevel=log_level) self.logger = CustomLogger("BfxWebsocketClient", logLevel=log_level)
if buckets > BfxWebsocketClient.MAXIMUM_BUCKETS_AMOUNT: if buckets > BfxWebsocketClient.MAXIMUM_BUCKETS_AMOUNT:
self.logger.warning(f"It is not safe to use more than {BfxWebsocketClient.MAXIMUM_BUCKETS_AMOUNT} buckets from the same connection ({buckets} in use), the server could momentarily block the client with <429 Too Many Requests>.") self.logger.warning(f"It is not safe to use more than {BfxWebsocketClient.MAXIMUM_BUCKETS_AMOUNT} buckets from the same \
connection ({buckets} in use), the server could momentarily block the client with <429 Too Many Requests>.")
def run(self): def run(self):
return asyncio.run(self.start()) return asyncio.run(self.start())