diff --git a/bfxapi/__init__.py b/bfxapi/__init__.py index c11c9ab..4fbdfd6 100644 --- a/bfxapi/__init__.py +++ b/bfxapi/__init__.py @@ -1 +1,6 @@ -from .client import Client, Constants \ No newline at end of file +from .client import Client + +from .urls import REST_HOST, PUB_REST_HOST, STAGING_REST_HOST, \ + WSS_HOST, PUB_WSS_HOST, STAGING_WSS_HOST + +NAME = "bfxapi" \ No newline at end of file diff --git a/bfxapi/client.py b/bfxapi/client.py index aa7eaf2..f3121ac 100644 --- a/bfxapi/client.py +++ b/bfxapi/client.py @@ -1,39 +1,27 @@ from .rest import BfxRestInterface from .websocket import BfxWebsocketClient +from .urls import REST_HOST, WSS_HOST from typing import List, Optional -from enum import Enum - -class Constants(str, Enum): - REST_HOST = "https://api.bitfinex.com/v2" - PUB_REST_HOST = "https://api-pub.bitfinex.com/v2" - STAGING_REST_HOST = "https://api.staging.bitfinex.com/v2" - - WSS_HOST = "wss://api.bitfinex.com/ws/2" - PUB_WSS_HOST = "wss://api-pub.bitfinex.com/ws/2" - STAGING_WSS_HOST = "wss://api.staging.bitfinex.com/ws/2" - class Client(object): def __init__( self, - REST_HOST: str = Constants.REST_HOST, - WSS_HOST: str = Constants.WSS_HOST, + REST_HOST: str = REST_HOST, + WSS_HOST: str = WSS_HOST, API_KEY: Optional[str] = None, API_SECRET: Optional[str] = None, filter: Optional[List[str]] = None, log_level: str = "INFO" ): - credentials = { - "API_KEY": API_KEY, - "API_SECRET": API_SECRET, - "filter": filter - } + credentials = None + + if API_KEY and API_SECRET: + credentials = { "API_KEY": API_KEY, "API_SECRET": API_SECRET, "filter": filter } self.rest = BfxRestInterface( host=REST_HOST, - API_KEY=API_KEY, - API_SECRET=API_SECRET + credentials=credentials ) self.wss = BfxWebsocketClient( diff --git a/bfxapi/rest/endpoints/bfx_rest_interface.py b/bfxapi/rest/endpoints/bfx_rest_interface.py index a2dc6ec..b117fa6 100644 --- a/bfxapi/rest/endpoints/bfx_rest_interface.py +++ b/bfxapi/rest/endpoints/bfx_rest_interface.py @@ -7,7 +7,10 @@ from .rest_merchant_endpoints import RestMerchantEndpoints class BfxRestInterface(object): VERSION = 2 - def __init__(self, host: str, API_KEY: Optional[str] = None, API_SECRET: Optional[str] = None): + def __init__(self, host, credentials = None): + API_KEY, API_SECRET = credentials and \ + (credentials["API_KEY"], credentials["API_SECRET"]) or (None, None) + self.public = RestPublicEndpoints(host=host) self.auth = RestAuthenticatedEndpoints(host=host, API_KEY=API_KEY, API_SECRET=API_SECRET) self.merchant = RestMerchantEndpoints(host=host, API_KEY=API_KEY, API_SECRET=API_SECRET) \ No newline at end of file diff --git a/bfxapi/rest/endpoints/rest_public_endpoints.py b/bfxapi/rest/endpoints/rest_public_endpoints.py index 3810e97..b5313fd 100644 --- a/bfxapi/rest/endpoints/rest_public_endpoints.py +++ b/bfxapi/rest/endpoints/rest_public_endpoints.py @@ -25,7 +25,7 @@ class RestPublicEndpoints(Middleware): if isinstance(pairs, str) and pairs == "ALL": return [ cast(TradingPairTicker, sub_data) for sub_data in self.get_tickers([ "ALL" ]) if cast(str, sub_data.symbol).startswith("t") ] - data = self.get_tickers([ "t" + pair for pair in pairs ]) + data = self.get_tickers([ pair for pair in pairs ]) return cast(List[TradingPairTicker], data) @@ -33,7 +33,7 @@ class RestPublicEndpoints(Middleware): if isinstance(currencies, str) and currencies == "ALL": return [ cast(FundingCurrencyTicker, sub_data) for sub_data in self.get_tickers([ "ALL" ]) if cast(str, sub_data.symbol).startswith("f") ] - data = self.get_tickers([ "f" + currency for currency in currencies ]) + data = self.get_tickers([ currency for currency in currencies ]) return cast(List[FundingCurrencyTicker], data) @@ -52,25 +52,25 @@ class RestPublicEndpoints(Middleware): def get_t_trades(self, pair: str, limit: Optional[int] = None, start: Optional[str] = None, end: Optional[str] = None, sort: Optional[Sort] = None) -> List[TradingPairTrade]: params = { "limit": limit, "start": start, "end": end, "sort": sort } - data = self._GET(f"trades/{'t' + pair}/hist", params=params) + data = self._GET(f"trades/{pair}/hist", params=params) return [ serializers.TradingPairTrade.parse(*sub_data) for sub_data in data ] def get_f_trades(self, currency: str, limit: Optional[int] = None, start: Optional[str] = None, end: Optional[str] = None, sort: Optional[Sort] = None) -> List[FundingCurrencyTrade]: params = { "limit": limit, "start": start, "end": end, "sort": sort } - data = self._GET(f"trades/{'f' + currency}/hist", params=params) + data = self._GET(f"trades/{currency}/hist", params=params) return [ serializers.FundingCurrencyTrade.parse(*sub_data) for sub_data in data ] def get_t_book(self, pair: str, precision: Literal["P0", "P1", "P2", "P3", "P4"], len: Optional[Literal[1, 25, 100]] = None) -> List[TradingPairBook]: - return [ serializers.TradingPairBook.parse(*sub_data) for sub_data in self._GET(f"book/{'t' + pair}/{precision}", params={ "len": len }) ] + return [ serializers.TradingPairBook.parse(*sub_data) for sub_data in self._GET(f"book/{pair}/{precision}", params={ "len": len }) ] def get_f_book(self, currency: str, precision: Literal["P0", "P1", "P2", "P3", "P4"], len: Optional[Literal[1, 25, 100]] = None) -> List[FundingCurrencyBook]: - return [ serializers.FundingCurrencyBook.parse(*sub_data) for sub_data in self._GET(f"book/{'f' + currency}/{precision}", params={ "len": len }) ] + return [ serializers.FundingCurrencyBook.parse(*sub_data) for sub_data in self._GET(f"book/{currency}/{precision}", params={ "len": len }) ] def get_t_raw_book(self, pair: str, len: Optional[Literal[1, 25, 100]] = None) -> List[TradingPairRawBook]: - return [ serializers.TradingPairRawBook.parse(*sub_data) for sub_data in self._GET(f"book/{'t' + pair}/R0", params={ "len": len }) ] + return [ serializers.TradingPairRawBook.parse(*sub_data) for sub_data in self._GET(f"book/{pair}/R0", params={ "len": len }) ] def get_f_raw_book(self, currency: str, len: Optional[Literal[1, 25, 100]] = None) -> List[FundingCurrencyRawBook]: - return [ serializers.FundingCurrencyRawBook.parse(*sub_data) for sub_data in self._GET(f"book/{'f' + currency}/R0", params={ "len": len }) ] + return [ serializers.FundingCurrencyRawBook.parse(*sub_data) for sub_data in self._GET(f"book/{currency}/R0", params={ "len": len }) ] def get_stats_hist( self, diff --git a/bfxapi/urls.py b/bfxapi/urls.py new file mode 100644 index 0000000..c9a622b --- /dev/null +++ b/bfxapi/urls.py @@ -0,0 +1,7 @@ +REST_HOST = "https://api.bitfinex.com/v2" +PUB_REST_HOST = "https://api-pub.bitfinex.com/v2" +STAGING_REST_HOST = "https://api.staging.bitfinex.com/v2" + +WSS_HOST = "wss://api.bitfinex.com/ws/2" +PUB_WSS_HOST = "wss://api-pub.bitfinex.com/ws/2" +STAGING_WSS_HOST = "wss://api.staging.bitfinex.com/ws/2" \ No newline at end of file diff --git a/bfxapi/websocket/client/bfx_websocket_client.py b/bfxapi/websocket/client/bfx_websocket_client.py index adea696..7dc06ed 100644 --- a/bfxapi/websocket/client/bfx_websocket_client.py +++ b/bfxapi/websocket/client/bfx_websocket_client.py @@ -96,8 +96,8 @@ class BfxWebsocketClient(object): if await asyncio.gather(*[on_open_event.wait() for on_open_event in self.on_open_events]): self.event_emitter.emit("open") - if credentials and credentials["API_KEY"] and credentials["API_SECRET"]: - await self.__authenticate(**credentials) + if self.credentials: + await self.__authenticate(**self.credentials) async for message in websocket: message = json.loads(message) diff --git a/examples/rest/claim_position.py b/examples/rest/claim_position.py index ba3e4e0..084c9d0 100644 --- a/examples/rest/claim_position.py +++ b/examples/rest/claim_position.py @@ -2,10 +2,10 @@ import os -from bfxapi.client import Client, Constants +from bfxapi.client import Client, REST_HOST bfx = Client( - REST_HOST=Constants.REST_HOST, + REST_HOST=REST_HOST, API_KEY=os.getenv("BFX_API_KEY"), API_SECRET=os.getenv("BFX_API_SECRET") ) diff --git a/examples/rest/create_funding_offer.py b/examples/rest/create_funding_offer.py index c1031d8..f462325 100644 --- a/examples/rest/create_funding_offer.py +++ b/examples/rest/create_funding_offer.py @@ -2,11 +2,11 @@ import os -from bfxapi.client import Client, Constants +from bfxapi.client import Client, REST_HOST from bfxapi.enums import FundingOfferType, Flag bfx = Client( - REST_HOST=Constants.REST_HOST, + REST_HOST=REST_HOST, API_KEY=os.getenv("BFX_API_KEY"), API_SECRET=os.getenv("BFX_API_SECRET") ) diff --git a/examples/rest/create_order.py b/examples/rest/create_order.py index ea70265..607f7c9 100644 --- a/examples/rest/create_order.py +++ b/examples/rest/create_order.py @@ -1,11 +1,11 @@ # python -c "import examples.rest.create_order" import os -from bfxapi.client import Client, Constants +from bfxapi.client import Client, REST_HOST from bfxapi.enums import OrderType, Flag bfx = Client( - REST_HOST=Constants.REST_HOST, + REST_HOST=REST_HOST, API_KEY=os.getenv("BFX_API_KEY"), API_SECRET=os.getenv("BFX_API_SECRET") ) diff --git a/examples/rest/derivatives.py b/examples/rest/derivatives.py index 4aedd00..58a0031 100644 --- a/examples/rest/derivatives.py +++ b/examples/rest/derivatives.py @@ -2,10 +2,10 @@ import os -from bfxapi.client import Client, Constants +from bfxapi.client import Client, REST_HOST bfx = Client( - REST_HOST=Constants.REST_HOST, + REST_HOST=REST_HOST, API_KEY=os.getenv("BFX_API_KEY"), API_SECRET=os.getenv("BFX_API_SECRET") ) diff --git a/examples/rest/extra_calcs.py b/examples/rest/extra_calcs.py index e380aef..8ef93cb 100644 --- a/examples/rest/extra_calcs.py +++ b/examples/rest/extra_calcs.py @@ -1,9 +1,9 @@ # python -c "import examples.rest.extra_calcs" -from bfxapi.client import Client, Constants +from bfxapi.client import Client, REST_HOST bfx = Client( - REST_HOST=Constants.REST_HOST + REST_HOST=REST_HOST ) t_symbol_response = bfx.rest.public.get_trading_market_average_price( diff --git a/examples/rest/funding_auto_renew.py b/examples/rest/funding_auto_renew.py index 11ee7ca..f546707 100644 --- a/examples/rest/funding_auto_renew.py +++ b/examples/rest/funding_auto_renew.py @@ -2,10 +2,10 @@ import os -from bfxapi.client import Client, Constants +from bfxapi.client import Client, REST_HOST bfx = Client( - REST_HOST=Constants.REST_HOST, + REST_HOST=REST_HOST, API_KEY=os.getenv("BFX_API_KEY"), API_SECRET=os.getenv("BFX_API_SECRET") ) diff --git a/examples/rest/get_authenticated_data.py b/examples/rest/get_authenticated_data.py index ada724a..c3226af 100644 --- a/examples/rest/get_authenticated_data.py +++ b/examples/rest/get_authenticated_data.py @@ -3,10 +3,10 @@ import os import time -from bfxapi.client import Client, Constants +from bfxapi.client import Client, REST_HOST bfx = Client( - REST_HOST=Constants.REST_HOST, + REST_HOST=REST_HOST, API_KEY=os.getenv("BFX_API_KEY"), API_SECRET=os.getenv("BFX_API_SECRET") ) diff --git a/examples/rest/get_candles_hist.py b/examples/rest/get_candles_hist.py index 98f9da7..d8d9881 100644 --- a/examples/rest/get_candles_hist.py +++ b/examples/rest/get_candles_hist.py @@ -1,9 +1,9 @@ # python -c "import examples.rest.get_candles_hist" -from bfxapi.client import Client, Constants +from bfxapi.client import Client, REST_HOST bfx = Client( - REST_HOST=Constants.REST_HOST + REST_HOST=REST_HOST ) print(f"Candles: {bfx.rest.public.get_candles_hist(symbol='tBTCUSD')}") diff --git a/examples/rest/get_funding_info.py b/examples/rest/get_funding_info.py index 83d0635..82bf150 100644 --- a/examples/rest/get_funding_info.py +++ b/examples/rest/get_funding_info.py @@ -2,10 +2,10 @@ import os -from bfxapi.client import Client, Constants +from bfxapi.client import Client, REST_HOST bfx = Client( - REST_HOST=Constants.REST_HOST, + REST_HOST=REST_HOST, API_KEY=os.getenv("BFX_API_KEY"), API_SECRET=os.getenv("BFX_API_SECRET") ) diff --git a/examples/rest/get_funding_trades_history.py b/examples/rest/get_funding_trades_history.py index c1cc8e6..3af19d8 100644 --- a/examples/rest/get_funding_trades_history.py +++ b/examples/rest/get_funding_trades_history.py @@ -2,10 +2,10 @@ import os -from bfxapi.client import Client, Constants +from bfxapi.client import Client, REST_HOST bfx = Client( - REST_HOST=Constants.REST_HOST, + REST_HOST=REST_HOST, API_KEY=os.getenv("BFX_API_KEY"), API_SECRET=os.getenv("BFX_API_SECRET") ) diff --git a/examples/rest/get_liquidations.py b/examples/rest/get_liquidations.py index 6113a25..588c83a 100644 --- a/examples/rest/get_liquidations.py +++ b/examples/rest/get_liquidations.py @@ -2,10 +2,10 @@ import time -from bfxapi.client import Client, Constants +from bfxapi.client import Client, REST_HOST bfx = Client( - REST_HOST=Constants.REST_HOST + REST_HOST=REST_HOST ) now = int(round(time.time() * 1000)) diff --git a/examples/rest/get_positions.py b/examples/rest/get_positions.py index 7e71824..62cd309 100644 --- a/examples/rest/get_positions.py +++ b/examples/rest/get_positions.py @@ -3,10 +3,10 @@ import os import time -from bfxapi.client import Client, Constants +from bfxapi.client import Client, REST_HOST bfx = Client( - REST_HOST=Constants.REST_HOST, + REST_HOST=REST_HOST, API_KEY=os.getenv("BFX_API_KEY"), API_SECRET=os.getenv("BFX_API_SECRET") ) diff --git a/examples/rest/get_public_data.py b/examples/rest/get_public_data.py index a6c388b..125f97c 100644 --- a/examples/rest/get_public_data.py +++ b/examples/rest/get_public_data.py @@ -2,10 +2,10 @@ import time -from bfxapi.client import Client, Constants +from bfxapi.client import Client, REST_HOST bfx = Client( - REST_HOST=Constants.REST_HOST + REST_HOST=REST_HOST ) now = int(round(time.time() * 1000)) diff --git a/examples/rest/get_pulse_data.py b/examples/rest/get_pulse_data.py index 75b55ae..b0cc369 100644 --- a/examples/rest/get_pulse_data.py +++ b/examples/rest/get_pulse_data.py @@ -2,10 +2,10 @@ import time -from bfxapi.client import Client, Constants +from bfxapi.client import Client, REST_HOST bfx = Client( - REST_HOST=Constants.REST_HOST + REST_HOST=REST_HOST ) now = int(round(time.time() * 1000)) diff --git a/examples/rest/increase_position.py b/examples/rest/increase_position.py index 65595c8..add66e3 100644 --- a/examples/rest/increase_position.py +++ b/examples/rest/increase_position.py @@ -2,10 +2,10 @@ import os -from bfxapi.client import Client, Constants +from bfxapi.client import Client, REST_HOST bfx = Client( - REST_HOST=Constants.REST_HOST, + REST_HOST=REST_HOST, API_KEY=os.getenv("BFX_API_KEY"), API_SECRET=os.getenv("BFX_API_SECRET") ) diff --git a/examples/rest/keep_taken_funding.py b/examples/rest/keep_taken_funding.py index 1314ffa..21e60f4 100644 --- a/examples/rest/keep_taken_funding.py +++ b/examples/rest/keep_taken_funding.py @@ -2,10 +2,10 @@ import os -from bfxapi.client import Client, Constants +from bfxapi.client import Client, REST_HOST bfx = Client( - REST_HOST=Constants.REST_HOST, + REST_HOST=REST_HOST, API_KEY=os.getenv("BFX_API_KEY"), API_SECRET=os.getenv("BFX_API_SECRET") ) diff --git a/examples/rest/merchant.py b/examples/rest/merchant.py index ec6727b..84d9b9b 100644 --- a/examples/rest/merchant.py +++ b/examples/rest/merchant.py @@ -2,10 +2,10 @@ import os -from bfxapi.client import Client, Constants +from bfxapi.client import Client, REST_HOST bfx = Client( - REST_HOST=Constants.REST_HOST, + REST_HOST=REST_HOST, API_KEY=os.getenv("BFX_API_KEY"), API_SECRET=os.getenv("BFX_API_SECRET") ) diff --git a/examples/rest/return_taken_funding.py b/examples/rest/return_taken_funding.py index ccb0c2b..73d5a33 100644 --- a/examples/rest/return_taken_funding.py +++ b/examples/rest/return_taken_funding.py @@ -2,10 +2,10 @@ import os -from bfxapi.client import Client, Constants +from bfxapi.client import Client, REST_HOST bfx = Client( - REST_HOST=Constants.REST_HOST, + REST_HOST=REST_HOST, API_KEY=os.getenv("BFX_API_KEY"), API_SECRET=os.getenv("BFX_API_SECRET") ) diff --git a/examples/rest/transfer_wallet.py b/examples/rest/transfer_wallet.py index 8de15fd..9384bd8 100644 --- a/examples/rest/transfer_wallet.py +++ b/examples/rest/transfer_wallet.py @@ -2,10 +2,10 @@ import os -from bfxapi.client import Client, Constants +from bfxapi.client import Client, REST_HOST bfx = Client( - REST_HOST=Constants.REST_HOST, + REST_HOST=REST_HOST, API_KEY=os.getenv("BFX_API_KEY"), API_SECRET=os.getenv("BFX_API_SECRET") ) diff --git a/examples/websocket/create_order.py b/examples/websocket/create_order.py index f72f9d4..48f4957 100644 --- a/examples/websocket/create_order.py +++ b/examples/websocket/create_order.py @@ -2,12 +2,12 @@ import os -from bfxapi.client import Client, Constants +from bfxapi.client import Client, WSS_HOST from bfxapi.websocket.enums import Error, OrderType from bfxapi.websocket.types import Notification, Order bfx = Client( - WSS_HOST=Constants.WSS_HOST, + WSS_HOST=WSS_HOST, API_KEY=os.getenv("BFX_API_KEY"), API_SECRET=os.getenv("BFX_API_SECRET") ) diff --git a/examples/websocket/order_book.py b/examples/websocket/order_book.py index a419454..55e4ae3 100644 --- a/examples/websocket/order_book.py +++ b/examples/websocket/order_book.py @@ -4,7 +4,7 @@ from collections import OrderedDict from typing import List -from bfxapi import Client, Constants +from bfxapi import Client, PUB_WSS_HOST from bfxapi.websocket import subscriptions from bfxapi.websocket.enums import Channel, Error @@ -38,7 +38,7 @@ SYMBOLS = [ "tBTCUSD", "tLTCUSD", "tLTCBTC", "tETHUSD", "tETHBTC" ] order_book = OrderBook(symbols=SYMBOLS) -bfx = Client(WSS_HOST=Constants.PUB_WSS_HOST) +bfx = Client(WSS_HOST=PUB_WSS_HOST) @bfx.wss.on("wss-error") def on_wss_error(code: Error, msg: str): diff --git a/examples/websocket/raw_order_book.py b/examples/websocket/raw_order_book.py index a039060..3ce9c6d 100644 --- a/examples/websocket/raw_order_book.py +++ b/examples/websocket/raw_order_book.py @@ -4,7 +4,7 @@ from collections import OrderedDict from typing import List -from bfxapi import Client, Constants +from bfxapi import Client, PUB_WSS_HOST from bfxapi.websocket import subscriptions from bfxapi.websocket.enums import Channel, Error @@ -38,7 +38,7 @@ SYMBOLS = [ "tBTCUSD", "tLTCUSD", "tLTCBTC", "tETHUSD", "tETHBTC" ] raw_order_book = RawOrderBook(symbols=SYMBOLS) -bfx = Client(WSS_HOST=Constants.PUB_WSS_HOST) +bfx = Client(WSS_HOST=PUB_WSS_HOST) @bfx.wss.on("wss-error") def on_wss_error(code: Error, msg: str): diff --git a/examples/websocket/ticker.py b/examples/websocket/ticker.py index a335a28..d4b4c91 100644 --- a/examples/websocket/ticker.py +++ b/examples/websocket/ticker.py @@ -1,12 +1,12 @@ # python -c "import examples.websocket.ticker" -from bfxapi import Client, Constants +from bfxapi import Client, PUB_WSS_HOST from bfxapi.websocket import subscriptions from bfxapi.websocket.enums import Channel from bfxapi.websocket.types import TradingPairTicker -bfx = Client(WSS_HOST=Constants.PUB_WSS_HOST) +bfx = Client(WSS_HOST=PUB_WSS_HOST) @bfx.wss.on("t_ticker_update") def on_t_ticker_update(subscription: subscriptions.Ticker, data: TradingPairTicker):