From 2734ff9e1a46ea973e2135c9132523a5f6205fc2 Mon Sep 17 00:00:00 2001 From: Davide Casale Date: Thu, 26 Oct 2023 05:41:47 +0200 Subject: [PATCH] Drop modules bfxapi.enums, bfxapi.rest.enums and bfxapi.websocket.enums. --- bfxapi/__init__.py | 12 ++--- bfxapi/{client.py => _client.py} | 7 ++- bfxapi/{version.py => _version.py} | 0 bfxapi/enums.py | 50 ------------------- bfxapi/rest/endpoints/rest_auth_endpoints.py | 10 ++-- .../rest/endpoints/rest_merchant_endpoints.py | 8 ++- .../rest/endpoints/rest_public_endpoints.py | 26 +++++----- bfxapi/rest/enums.py | 47 ----------------- bfxapi/rest/middleware/middleware.py | 19 ++++--- bfxapi/urls.py | 5 -- .../websocket/_client/bfx_websocket_inputs.py | 8 +-- bfxapi/websocket/enums.py | 9 ---- setup.py | 8 +-- 13 files changed, 50 insertions(+), 159 deletions(-) rename bfxapi/{client.py => _client.py} (89%) rename bfxapi/{version.py => _version.py} (100%) delete mode 100644 bfxapi/enums.py delete mode 100644 bfxapi/rest/enums.py delete mode 100644 bfxapi/urls.py delete mode 100644 bfxapi/websocket/enums.py diff --git a/bfxapi/__init__.py b/bfxapi/__init__.py index b583248..9138036 100644 --- a/bfxapi/__init__.py +++ b/bfxapi/__init__.py @@ -1,6 +1,6 @@ -from .client import Client - -from .urls import REST_HOST, PUB_REST_HOST, \ - WSS_HOST, PUB_WSS_HOST - -from .version import __version__ +from ._client import \ + Client, \ + REST_HOST, \ + WSS_HOST, \ + PUB_REST_HOST, \ + PUB_WSS_HOST diff --git a/bfxapi/client.py b/bfxapi/_client.py similarity index 89% rename from bfxapi/client.py rename to bfxapi/_client.py index d45b925..baf81c6 100644 --- a/bfxapi/client.py +++ b/bfxapi/_client.py @@ -7,12 +7,17 @@ from bfxapi._exceptions import IncompleteCredentialError from bfxapi.rest import BfxRestInterface from bfxapi.websocket import BfxWebSocketClient -from bfxapi.urls import REST_HOST, WSS_HOST if TYPE_CHECKING: from bfxapi.websocket._client.bfx_websocket_client import \ _Credentials +REST_HOST = "https://api.bitfinex.com/v2" +WSS_HOST = "wss://api.bitfinex.com/ws/2" + +PUB_REST_HOST = "https://api-pub.bitfinex.com/v2" +PUB_WSS_HOST = "wss://api-pub.bitfinex.com/ws/2" + class Client: def __init__( self, diff --git a/bfxapi/version.py b/bfxapi/_version.py similarity index 100% rename from bfxapi/version.py rename to bfxapi/_version.py diff --git a/bfxapi/enums.py b/bfxapi/enums.py deleted file mode 100644 index 9b06bc2..0000000 --- a/bfxapi/enums.py +++ /dev/null @@ -1,50 +0,0 @@ -from enum import Enum - -class OrderType(str, Enum): - LIMIT = "LIMIT" - EXCHANGE_LIMIT = "EXCHANGE LIMIT" - MARKET = "MARKET" - EXCHANGE_MARKET = "EXCHANGE MARKET" - STOP = "STOP" - EXCHANGE_STOP = "EXCHANGE STOP" - STOP_LIMIT = "STOP LIMIT" - EXCHANGE_STOP_LIMIT = "EXCHANGE STOP LIMIT" - TRAILING_STOP = "TRAILING STOP" - EXCHANGE_TRAILING_STOP = "EXCHANGE TRAILING STOP" - FOK = "FOK" - EXCHANGE_FOK = "EXCHANGE FOK" - IOC = "IOC" - EXCHANGE_IOC = "EXCHANGE IOC" - -class FundingOfferType(str, Enum): - LIMIT = "LIMIT" - FRR_DELTA_FIX = "FRRDELTAFIX" - FRR_DELTA_VAR = "FRRDELTAVAR" - -class Flag(int, Enum): - HIDDEN = 64 - CLOSE = 512 - REDUCE_ONLY = 1024 - POST_ONLY = 4096 - OCO = 16384 - NO_VAR_RATES = 524288 - -class Error(int, Enum): - ERR_UNK = 10000 - ERR_GENERIC = 10001 - ERR_CONCURRENCY = 10008 - ERR_PARAMS = 10020 - ERR_CONF_FAIL = 10050 - ERR_AUTH_FAIL = 10100 - ERR_AUTH_PAYLOAD = 10111 - ERR_AUTH_SIG = 10112 - ERR_AUTH_HMAC = 10113 - ERR_AUTH_NONCE = 10114 - ERR_UNAUTH_FAIL = 10200 - ERR_SUB_FAIL = 10300 - ERR_SUB_MULTI = 10301 - ERR_SUB_UNK = 10302 - ERR_SUB_LIMIT = 10305 - ERR_UNSUB_FAIL = 10400 - ERR_UNSUB_NOT = 10401 - ERR_READY = 11000 diff --git a/bfxapi/rest/endpoints/rest_auth_endpoints.py b/bfxapi/rest/endpoints/rest_auth_endpoints.py index 3dc0885..658e182 100644 --- a/bfxapi/rest/endpoints/rest_auth_endpoints.py +++ b/bfxapi/rest/endpoints/rest_auth_endpoints.py @@ -4,8 +4,6 @@ from decimal import Decimal from ..middleware import Middleware -from ..enums import Sort, OrderType, FundingOfferType - from ...types import Notification, \ UserInfo, LoginHistory, BalanceAvailable, \ Order, Position, Trade, \ @@ -63,7 +61,7 @@ class RestAuthEndpoints(Middleware): for sub_data in self._post(endpoint, body={ "id": ids }) ] def submit_order(self, - type: OrderType, + type: str, symbol: str, amount: Union[Decimal, float, str], *, @@ -163,7 +161,7 @@ class RestAuthEndpoints(Middleware): def get_trades_history(self, *, symbol: Optional[str] = None, - sort: Optional[Sort] = None, + sort: Optional[int] = None, start: Optional[str] = None, end: Optional[str] = None, limit: Optional[int] = None) -> List[Trade]: @@ -285,7 +283,7 @@ class RestAuthEndpoints(Middleware): #pylint: disable-next=too-many-arguments def submit_funding_offer(self, - type: FundingOfferType, + type: str, symbol: str, amount: Union[Decimal, float, str], rate: Union[Decimal, float, str], @@ -397,7 +395,7 @@ class RestAuthEndpoints(Middleware): def get_funding_trades_history(self, *, symbol: Optional[str] = None, - sort: Optional[Sort] = None, + sort: Optional[int] = None, start: Optional[str] = None, end: Optional[str] = None, limit: Optional[int] = None) -> List[FundingTrade]: diff --git a/bfxapi/rest/endpoints/rest_merchant_endpoints.py b/bfxapi/rest/endpoints/rest_merchant_endpoints.py index d201a10..d34028b 100644 --- a/bfxapi/rest/endpoints/rest_merchant_endpoints.py +++ b/bfxapi/rest/endpoints/rest_merchant_endpoints.py @@ -7,8 +7,6 @@ from decimal import Decimal from bfxapi.rest.middleware import Middleware -from bfxapi.rest.enums import MerchantSettingsKey - from bfxapi.types import \ InvoiceSubmission, \ InvoicePage, \ @@ -140,16 +138,16 @@ class RestMerchantEndpoints(Middleware): body={ "baseCcy": base_ccy, "convertCcy": convert_ccy })) def set_merchant_settings(self, - key: MerchantSettingsKey, + key: str, val: Any) -> bool: return bool(self._post("auth/w/ext/pay/settings/set", \ body={ "key": key, "val": val })) - def get_merchant_settings(self, key: MerchantSettingsKey) -> Any: + def get_merchant_settings(self, key: str) -> Any: return self._post("auth/r/ext/pay/settings/get", body={ "key": key }) #pylint: disable-next=dangerous-default-value - def list_merchant_settings(self, keys: List[MerchantSettingsKey] = []) -> Dict[MerchantSettingsKey, Any]: + def list_merchant_settings(self, keys: List[str] = []) -> Dict[str, Any]: return self._post("auth/r/ext/pay/settings/list", body={ "keys": keys }) def get_deposits(self, diff --git a/bfxapi/rest/endpoints/rest_public_endpoints.py b/bfxapi/rest/endpoints/rest_public_endpoints.py index 4401057..5549730 100644 --- a/bfxapi/rest/endpoints/rest_public_endpoints.py +++ b/bfxapi/rest/endpoints/rest_public_endpoints.py @@ -4,8 +4,6 @@ from decimal import Decimal from ..middleware import Middleware -from ..enums import Config, Sort - from ...types import \ PlatformStatus, TradingPairTicker, FundingCurrencyTicker, \ TickersHistory, TradingPairTrade, FundingCurrencyTrade, \ @@ -19,7 +17,7 @@ from ...types import serializers #pylint: disable-next=too-many-public-methods class RestPublicEndpoints(Middleware): - def conf(self, config: Config) -> Any: + def conf(self, config: str) -> Any: return self._get(f"conf/{config}")[0] def get_platform_status(self) -> PlatformStatus: @@ -84,7 +82,7 @@ class RestPublicEndpoints(Middleware): limit: Optional[int] = None, start: Optional[str] = None, end: Optional[str] = None, - sort: Optional[Sort] = None) -> List[TradingPairTrade]: + sort: Optional[int] = None) -> List[TradingPairTrade]: params = { "limit": limit, "start": start, "end": end, "sort": sort } data = self._get(f"trades/{pair}/hist", params=params) return [ serializers.TradingPairTrade.parse(*sub_data) for sub_data in data ] @@ -95,7 +93,7 @@ class RestPublicEndpoints(Middleware): limit: Optional[int] = None, start: Optional[str] = None, end: Optional[str] = None, - sort: Optional[Sort] = None) -> List[FundingCurrencyTrade]: + sort: Optional[int] = None) -> List[FundingCurrencyTrade]: params = { "limit": limit, "start": start, "end": end, "sort": sort } data = self._get(f"trades/{currency}/hist", params=params) return [ serializers.FundingCurrencyTrade.parse(*sub_data) for sub_data in data ] @@ -133,7 +131,7 @@ class RestPublicEndpoints(Middleware): def get_stats_hist(self, resource: str, *, - sort: Optional[Sort] = None, + sort: Optional[int] = None, start: Optional[str] = None, end: Optional[str] = None, limit: Optional[int] = None) -> List[Statistic]: @@ -144,7 +142,7 @@ class RestPublicEndpoints(Middleware): def get_stats_last(self, resource: str, *, - sort: Optional[Sort] = None, + sort: Optional[int] = None, start: Optional[str] = None, end: Optional[str] = None, limit: Optional[int] = None) -> Statistic: @@ -156,7 +154,7 @@ class RestPublicEndpoints(Middleware): symbol: str, tf: str = "1m", *, - sort: Optional[Sort] = None, + sort: Optional[int] = None, start: Optional[str] = None, end: Optional[str] = None, limit: Optional[int] = None) -> List[Candle]: @@ -168,7 +166,7 @@ class RestPublicEndpoints(Middleware): symbol: str, tf: str = "1m", *, - sort: Optional[Sort] = None, + sort: Optional[int] = None, start: Optional[str] = None, end: Optional[str] = None, limit: Optional[int] = None) -> Candle: @@ -192,7 +190,7 @@ class RestPublicEndpoints(Middleware): def get_derivatives_status_history(self, key: str, *, - sort: Optional[Sort] = None, + sort: Optional[int] = None, start: Optional[str] = None, end: Optional[str] = None, limit: Optional[int] = None) -> List[DerivativesStatus]: @@ -202,7 +200,7 @@ class RestPublicEndpoints(Middleware): def get_liquidations(self, *, - sort: Optional[Sort] = None, + sort: Optional[int] = None, start: Optional[str] = None, end: Optional[str] = None, limit: Optional[int] = None) -> List[Liquidation]: @@ -214,7 +212,7 @@ class RestPublicEndpoints(Middleware): symbol: str, tf: str = "1m", *, - sort: Optional[Sort] = None, + sort: Optional[int] = None, start: Optional[str] = None, end: Optional[str] = None, limit: Optional[int] = None) -> List[Candle]: @@ -225,7 +223,7 @@ class RestPublicEndpoints(Middleware): def get_leaderboards_hist(self, resource: str, *, - sort: Optional[Sort] = None, + sort: Optional[int] = None, start: Optional[str] = None, end: Optional[str] = None, limit: Optional[int] = None) -> List[Leaderboard]: @@ -236,7 +234,7 @@ class RestPublicEndpoints(Middleware): def get_leaderboards_last(self, resource: str, *, - sort: Optional[Sort] = None, + sort: Optional[int] = None, start: Optional[str] = None, end: Optional[str] = None, limit: Optional[int] = None) -> Leaderboard: diff --git a/bfxapi/rest/enums.py b/bfxapi/rest/enums.py deleted file mode 100644 index 17b3753..0000000 --- a/bfxapi/rest/enums.py +++ /dev/null @@ -1,47 +0,0 @@ -#pylint: disable-next=wildcard-import,unused-wildcard-import -from ..enums import * - -class Config(str, Enum): - MAP_CURRENCY_SYM = "pub:map:currency:sym" - MAP_CURRENCY_LABEL = "pub:map:currency:label" - MAP_CURRENCY_UNIT = "pub:map:currency:unit" - MAP_CURRENCY_UNDL = "pub:map:currency:undl" - MAP_CURRENCY_POOL = "pub:map:currency:pool" - MAP_CURRENCY_EXPLORER = "pub:map:currency:explorer" - MAP_CURRENCY_TX_FEE = "pub:map:currency:tx:fee" - MAP_TX_METHOD = "pub:map:tx:method" - - LIST_PAIR_EXCHANGE = "pub:list:pair:exchange" - LIST_PAIR_MARGIN = "pub:list:pair:margin" - LIST_PAIR_FUTURES = "pub:list:pair:futures" - LIST_PAIR_SECURITIES = "pub:list:pair:securities" - LIST_CURRENCY = "pub:list:currency" - LIST_COMPETITIONS = "pub:list:competitions" - - INFO_PAIR = "pub:info:pair" - INFO_PAIR_FUTURES = "pub:info:pair:futures" - INFO_TX_STATUS = "pub:info:tx:status" - - SPEC_MARGIN = "pub:spec:margin" - FEES = "pub:fees" - -class Precision(str, Enum): - P0 = "P0" - P1 = "P1" - P2 = "P2" - P3 = "P3" - P4 = "P4" - -class Sort(int, Enum): - ASCENDING = +1 - DESCENDING = -1 - -class MerchantSettingsKey(str, Enum): - PREFERRED_FIAT = "bfx_pay_preferred_fiat" - RECOMMEND_STORE = "bfx_pay_recommend_store" - NOTIFY_PAYMENT_COMPLETED = "bfx_pay_notify_payment_completed" - NOTIFY_PAYMENT_COMPLETED_EMAIL = "bfx_pay_notify_payment_completed_email" - NOTIFY_AUTOCONVERT_EXECUTED = "bfx_pay_notify_autoconvert_executed" - DUST_BALANCE_UI = "bfx_pay_dust_balance_ui" - MERCHANT_CUSTOMER_SUPPORT_URL = "bfx_pay_merchant_customer_support_url" - MERCHANT_UNDERPAID_THRESHOLD = "bfx_pay_merchant_underpaid_threshold" diff --git a/bfxapi/rest/middleware/middleware.py b/bfxapi/rest/middleware/middleware.py index cf434e5..4d29418 100644 --- a/bfxapi/rest/middleware/middleware.py +++ b/bfxapi/rest/middleware/middleware.py @@ -1,10 +1,11 @@ from typing import TYPE_CHECKING, Optional, Any +from enum import Enum + from http import HTTPStatus import time, hmac, hashlib, json, requests -from ..enums import Error from ..exceptions import ResourceNotFound, RequestParametersError, InvalidCredentialError, UnknownGenericError from ..._utils.json_encoder import JSONEncoder from ..._utils.json_decoder import JSONDecoder @@ -12,6 +13,12 @@ from ..._utils.json_decoder import JSONDecoder if TYPE_CHECKING: from requests.sessions import _Params +class _Error(Enum): + ERR_UNK = 10000 + ERR_GENERIC = 10001 + ERR_PARAMS = 10020 + ERR_AUTH_FAIL = 10100 + class Middleware: TIMEOUT = 30 @@ -53,11 +60,11 @@ class Middleware: data = response.json(cls=JSONDecoder) if len(data) and data[0] == "error": - if data[1] == Error.ERR_PARAMS: + if data[1] == _Error.ERR_PARAMS: raise RequestParametersError("The request was rejected with the " \ f"following parameter error: <{data[2]}>") - if data[1] is None or data[1] == Error.ERR_UNK or data[1] == Error.ERR_GENERIC: + if data[1] is None or data[1] == _Error.ERR_UNK or data[1] == _Error.ERR_GENERIC: raise UnknownGenericError("The server replied to the request with " \ f"a generic error with message: <{data[2]}>.") @@ -86,14 +93,14 @@ class Middleware: data = response.json(cls=JSONDecoder) 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("The request was rejected with the " \ f"following parameter error: <{data[2]}>") - if data[1] == Error.ERR_AUTH_FAIL: + if data[1] == _Error.ERR_AUTH_FAIL: raise InvalidCredentialError("Cannot authenticate with given API-KEY and API-SECRET.") - if data[1] is None or data[1] == Error.ERR_UNK or data[1] == Error.ERR_GENERIC: + if data[1] is None or data[1] == _Error.ERR_UNK or data[1] == _Error.ERR_GENERIC: raise UnknownGenericError("The server replied to the request with " \ f"a generic error with message: <{data[2]}>.") diff --git a/bfxapi/urls.py b/bfxapi/urls.py deleted file mode 100644 index 556e4d9..0000000 --- a/bfxapi/urls.py +++ /dev/null @@ -1,5 +0,0 @@ -REST_HOST = "https://api.bitfinex.com/v2" -PUB_REST_HOST = "https://api-pub.bitfinex.com/v2" - -WSS_HOST = "wss://api.bitfinex.com/ws/2" -PUB_WSS_HOST = "wss://api-pub.bitfinex.com/ws/2" diff --git a/bfxapi/websocket/_client/bfx_websocket_inputs.py b/bfxapi/websocket/_client/bfx_websocket_inputs.py index f14d32d..1753f3a 100644 --- a/bfxapi/websocket/_client/bfx_websocket_inputs.py +++ b/bfxapi/websocket/_client/bfx_websocket_inputs.py @@ -5,10 +5,6 @@ from typing import \ from decimal import Decimal -from bfxapi.enums import \ - OrderType, \ - FundingOfferType - _Handler = Callable[[str, Any], Awaitable[None]] class BfxWebSocketInputs: @@ -16,7 +12,7 @@ class BfxWebSocketInputs: self.__handle_websocket_input = handle_websocket_input async def submit_order(self, - type: OrderType, + type: str, symbol: str, amount: Union[str, float, Decimal], price: Union[str, float, Decimal], @@ -79,7 +75,7 @@ class BfxWebSocketInputs: #pylint: disable-next=too-many-arguments async def submit_funding_offer(self, - type: FundingOfferType, + type: str, symbol: str, amount: Union[str, float, Decimal], rate: Union[str, float, Decimal], diff --git a/bfxapi/websocket/enums.py b/bfxapi/websocket/enums.py deleted file mode 100644 index 8fe6028..0000000 --- a/bfxapi/websocket/enums.py +++ /dev/null @@ -1,9 +0,0 @@ -#pylint: disable-next=wildcard-import,unused-wildcard-import -from bfxapi.enums import * - -class Channel(str, Enum): - TICKER = "ticker" - TRADES = "trades" - BOOK = "book" - CANDLES = "candles" - STATUS = "status" diff --git a/setup.py b/setup.py index 9ca14a8..f79a84f 100644 --- a/setup.py +++ b/setup.py @@ -1,12 +1,12 @@ from distutils.core import setup -version = {} -with open("bfxapi/version.py", encoding="utf-8") as fp: - exec(fp.read(), version) #pylint: disable=exec-used +_version = {} +with open("bfxapi/_version.py", encoding="utf-8") as fp: + exec(fp.read(), _version) #pylint: disable=exec-used setup( name="bitfinex-api-py", - version=version["__version__"], + version=_version["__version__"], description="Official Bitfinex Python API", long_description="A Python reference implementation of the Bitfinex API for both REST and websocket interaction", long_description_content_type="text/markdown",