diff --git a/manual_test_functions.py b/manual_test_functions.py index 39c80e3..605c72d 100644 --- a/manual_test_functions.py +++ b/manual_test_functions.py @@ -33,13 +33,13 @@ async def main() -> None: for i, event in enumerate(events): print(f"[{i}] {_event_summary(event)}") - # MESSAGES + # CHATS (MESSAGES) - print("\nGetting up to 10 messages...") - messages = await s.get_messages() - print(f"{len(messages)} messages:") - for i, message in enumerate(messages): - print(f"[{i}] {_message_summary(message)}") + print("\nGetting up to 10 chats...") + messages = await s.get_messages(max_chats=10) + print(f"{len(messages)} chats:") + for i, chat in enumerate(messages): + print(f"[{i}] {_message_summary(chat)}") # ATTENDANCE EXPORT diff --git a/spond/spond.py b/spond/spond.py index de9d269..053da2d 100644 --- a/spond/spond.py +++ b/spond/spond.py @@ -25,6 +25,7 @@ def __init__(self, username: str, password: str) -> None: self.auth = None self.groups = None self.events = None + self.messages = None async def login_chat(self) -> None: api_chat_url = f"{self.api_url}chat" @@ -118,10 +119,20 @@ async def get_person(self, user: str) -> DictFromJSON: raise KeyError(errmsg) @_SpondBase.require_authentication - async def get_messages(self) -> Optional[DictFromJSON]: + async def get_messages( + self, + max_chats: int = 100, + ) -> Optional[DictFromJSON]: """ Retrieve messages (chats). + Parameters + ---------- + max_chats : int, optional + Set a limit on the number of chats returned. + For performance reasons, defaults to 100. + Uses `max` API parameter. + Returns ------- list[dict] or None @@ -130,9 +141,14 @@ async def get_messages(self) -> Optional[DictFromJSON]: """ if not self.auth: await self.login_chat() - url = f"{self.chat_url}/chats/?max=10" - async with self.clientsession.get(url, headers={"auth": self.auth}) as r: - return await r.json() + url = f"{self.chat_url}/chats/" + async with self.clientsession.get( + url, + headers={"auth": self.auth}, + params={"max": str(max_chats)}, + ) as r: + self.messages = await r.json() + return self.messages @_SpondBase.require_authentication async def _continue_chat(self, chat_id: str, text: str):