Skip to content

Commit 5be6413

Browse files
authored
enable multiple calls to subscribe for same event type (#114)
* enable multiple calls to subscribe for same event type * fix streamer test * fix lint
1 parent 09fad18 commit 5be6413

File tree

2 files changed

+5
-11
lines changed

2 files changed

+5
-11
lines changed

Diff for: tastytrade/streamer.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,6 @@ async def _connect(self) -> None:
678678
Connect to the websocket server using the URL and
679679
authorization token provided during initialization.
680680
"""
681-
682681
async with websockets.connect( # type: ignore
683682
self._wss_url
684683
) as websocket:
@@ -701,8 +700,7 @@ async def _connect(self) -> None:
701700
elif message['type'] == 'CHANNEL_OPENED':
702701
channel = next((k for k, v in self._channels.items()
703702
if v == message['channel']))
704-
self._subscription_state[channel] \
705-
= message['type']
703+
self._subscription_state[channel] = message['type']
706704
elif message['type'] == 'CHANNEL_CLOSED':
707705
pass
708706
elif message['type'] == 'FEED_CONFIG':
@@ -712,7 +710,7 @@ async def _connect(self) -> None:
712710
elif message['type'] == 'KEEPALIVE':
713711
pass
714712
else:
715-
raise TastytradeError(message)
713+
raise TastytradeError('Unknown message type:', message)
716714

717715
async def _setup_connection(self):
718716
message = {
@@ -782,12 +780,12 @@ async def subscribe(
782780
:param event_type: type of subscription to add
783781
:param symbols: list of symbols to subscribe for
784782
"""
785-
await self._channel_request(event_type)
786-
event_type_str = str(event_type).split('.')[1].capitalize()
783+
if self._subscription_state[event_type] != 'CHANNEL_OPENED':
784+
await self._channel_request(event_type)
787785
message = {
788786
'type': 'FEED_SUBSCRIPTION',
789787
'channel': self._channels[event_type],
790-
'add': [{'symbol': symbol, "type": event_type_str}
788+
'add': [{'symbol': symbol, 'type': event_type}
791789
for symbol in symbols]
792790
}
793791
logger.debug('sending subscription: %s', message)

Diff for: tests/test_streamer.py

-4
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ async def test_account_streamer(session):
2020

2121
@pytest.mark.asyncio
2222
async def test_dxlink_streamer(session):
23-
message = "[{'eventType': 'Quote', 'eventSymbol': 'SPY', 'eventTime': 0, 'sequence': 0, 'timeNanoPart': 0, 'bidTime': 0, 'bidExchangeCode': 'Q', 'bidPrice': 450.5, 'bidSize': 796.0, 'askTime': 0, 'askExchangeCode': 'Q', 'askPrice': 450.55, 'askSize': 1100.0}, {'eventType': 'Quote', 'eventSymbol': 'AAPL', 'eventTime': 0, 'sequence': 0, 'timeNanoPart': 0, 'bidTime': 0, 'bidExchangeCode': 'Q', 'bidPrice': 190.39, 'bidSize': 1.0, 'askTime': 0, 'askExchangeCode': 'Q', 'askPrice': 190.44, 'askSize': 3.0}]" # noqa: E501
24-
2523
async with DXLinkStreamer(session) as streamer:
2624
subs = ['SPY', 'AAPL']
2725
await streamer.subscribe(EventType.QUOTE, subs)
@@ -32,5 +30,3 @@ async def test_dxlink_streamer(session):
3230
break
3331
await streamer.unsubscribe_candle(subs[0], '1d')
3432
await streamer.unsubscribe(EventType.QUOTE, subs[1])
35-
36-
streamer._map_message(message)

0 commit comments

Comments
 (0)