Allow initializing the MQTT Listener without making external requests

This commit is contained in:
Mads Marquart
2020-01-25 15:07:40 +01:00
parent 987993701f
commit 9c03c1035b

View File

@@ -97,48 +97,34 @@ def fetch_sequence_id(session: _session.Session) -> int:
return int(sequence_id) return int(sequence_id)
@attr.s(slots=True, kw_only=kw_only, repr=False, eq=False) @attr.s(slots=True, kw_only=kw_only, eq=False)
class Listener: class Listener:
"""Helper, to listen for incoming Facebook events.""" """Helper, to listen for incoming Facebook events.
Initialize a connection to the Facebook MQTT service.
Args:
session: The session to use when making requests.
chat_on: Whether ...
foreground: Whether ...
Example:
>>> listener = fbchat.Listener(session, chat_on=True, foreground=True)
"""
session = attr.ib(type=_session.Session) session = attr.ib(type=_session.Session)
_chat_on = attr.ib(type=bool) _chat_on = attr.ib(type=bool)
_foreground = attr.ib(type=bool) _foreground = attr.ib(type=bool)
_sequence_id = attr.ib(type=int)
_mqtt = attr.ib(factory=mqtt_factory, type=paho.mqtt.client.Client) _mqtt = attr.ib(factory=mqtt_factory, type=paho.mqtt.client.Client)
_sync_token = attr.ib(None, type=str) _sync_token = attr.ib(None, type=Optional[str])
_tmp_events = attr.ib(None, type=Optional[Iterable[_events.Event]]) _sequence_id = attr.ib(None, type=Optional[int])
_tmp_events = attr.ib(factory=list, type=Iterable[_events.Event])
def __repr__(self) -> str:
# An alternative repr, to illustrate that you can't create the class directly
return "<fbchat.Listener session={} chat_on={} foreground={}>".format(
self.session, self._chat_on, self._foreground
)
def __attrs_post_init__(self): def __attrs_post_init__(self):
# Configure callbacks # Configure callbacks
self._mqtt.on_message = self._on_message_handler self._mqtt.on_message = self._on_message_handler
self._mqtt.on_connect = self._on_connect_handler self._mqtt.on_connect = self._on_connect_handler
@classmethod
def connect(cls, session: _session.Session, chat_on: bool, foreground: bool):
"""Initialize a connection to the Facebook MQTT service.
Args:
session: The session to use when making requests.
chat_on: Whether ...
foreground: Whether ...
Example:
>>> listener = fbchat.Listener.connect(session, chat_on=True, foreground=True)
"""
return cls(
session=session,
chat_on=chat_on,
foreground=foreground,
sequence_id=fetch_sequence_id(session),
)
def _handle_ms(self, j): def _handle_ms(self, j):
"""Handle /t_ms special logic. """Handle /t_ms special logic.
@@ -326,6 +312,9 @@ class Listener:
>>> for event in listener.listen(): >>> for event in listener.listen():
... print(event) ... print(event)
""" """
if not self._sequence_id:
self._sequence_id = fetch_sequence_id(self.session)
# Make sure we're connected # Make sure we're connected
while True: while True:
# Beware, internal API, may have to change this to something more stable! # Beware, internal API, may have to change this to something more stable!