Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable multiple calls to subscribe for same event type #114

Merged
merged 3 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions tastytrade/streamer.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,6 @@ async def _connect(self) -> None:
Connect to the websocket server using the URL and
authorization token provided during initialization.
"""

async with websockets.connect( # type: ignore
self._wss_url
) as websocket:
Expand All @@ -701,8 +700,7 @@ async def _connect(self) -> None:
elif message['type'] == 'CHANNEL_OPENED':
channel = next((k for k, v in self._channels.items()
if v == message['channel']))
self._subscription_state[channel] \
= message['type']
self._subscription_state[channel] = message['type']
elif message['type'] == 'CHANNEL_CLOSED':
pass
elif message['type'] == 'FEED_CONFIG':
Expand All @@ -712,7 +710,7 @@ async def _connect(self) -> None:
elif message['type'] == 'KEEPALIVE':
pass
else:
raise TastytradeError(message)
raise TastytradeError('Unknown message type:', message)

async def _setup_connection(self):
message = {
Expand Down Expand Up @@ -782,12 +780,12 @@ async def subscribe(
:param event_type: type of subscription to add
:param symbols: list of symbols to subscribe for
"""
await self._channel_request(event_type)
event_type_str = str(event_type).split('.')[1].capitalize()
if self._subscription_state[event_type] != 'CHANNEL_OPENED':
await self._channel_request(event_type)
message = {
'type': 'FEED_SUBSCRIPTION',
'channel': self._channels[event_type],
'add': [{'symbol': symbol, "type": event_type_str}
'add': [{'symbol': symbol, 'type': event_type}
for symbol in symbols]
}
logger.debug('sending subscription: %s', message)
Expand Down
4 changes: 0 additions & 4 deletions tests/test_streamer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ async def test_account_streamer(session):

@pytest.mark.asyncio
async def test_dxlink_streamer(session):
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

async with DXLinkStreamer(session) as streamer:
subs = ['SPY', 'AAPL']
await streamer.subscribe(EventType.QUOTE, subs)
Expand All @@ -32,5 +30,3 @@ async def test_dxlink_streamer(session):
break
await streamer.unsubscribe_candle(subs[0], '1d')
await streamer.unsubscribe(EventType.QUOTE, subs[1])

streamer._map_message(message)