diff --git a/bfxapi/websockets/SubscriptionManager.py b/bfxapi/websockets/SubscriptionManager.py index af15bf9..92b1c8b 100644 --- a/bfxapi/websockets/SubscriptionManager.py +++ b/bfxapi/websockets/SubscriptionManager.py @@ -16,6 +16,14 @@ class SubscriptionManager: self.logger = CustomLogger('BfxSubscriptionManager', logLevel=logLevel) async def subscribe(self, channel_name, symbol, timeframe=None, **kwargs): + """ + Subscribe to a new channel + + @param channel_name: the name of the channel i.e 'books', 'candles' + @param symbol: the trading symbol i.e 'tBTCUSD' + @param timeframe: sepecifies the data timeframe between each candle (only required + for the candles channel) + """ # create a new subscription subscription = Subscription(self.bfxapi.ws, channel_name, symbol, timeframe, **kwargs) self.logger.info("Subscribing to channel {}".format(channel_name)) @@ -61,6 +69,12 @@ class SubscriptionManager: return self.subscriptions_chanid[chanId] async def unsubscribe(self, chanId, onComplete=None): + """ + Unsubscribe from the channel with the given chanId + + @param onComplete: function called when the bitfinex websocket resoponds with + a signal that confirms the subscription has been unsubscribed to + """ sub = self.subscriptions_chanid[chanId] if onComplete: self.unsubscribe_callbacks[sub.sub_id] = onComplete @@ -68,6 +82,11 @@ class SubscriptionManager: await self.subscriptions_chanid[chanId].unsubscribe() async def resubscribe(self, chanId): + """ + Unsubscribes and then subscribes to the channel with the given Id + + This function is mostly used to force the channel to produce a fresh snapshot. + """ sub = self.subscriptions_chanid[chanId] async def re_sub(): await sub.subscribe() @@ -79,11 +98,17 @@ class SubscriptionManager: await sub.subscribe() def is_subscribed(self, chanId): + """ + Returns True if the channel with the given chanId is currenly subscribed to + """ if chanId not in self.subscriptions_chanid: return False return self.subscriptions_chanid[chanId].is_subscribed() async def unsubscribe_all(self): + """ + Unsubscribe from all channels. + """ task_batch = [] for chanId in self.subscriptions_chanid: sub = self.get(chanId) @@ -94,6 +119,9 @@ class SubscriptionManager: await asyncio.wait(*[ task_batch ]) async def resubscribe_all(self): + """ + Unsubscribe and then subscribe to all channels + """ task_batch = [] for chanId in self.subscriptions_chanid: task_batch += [