Skip to content

Commit

Permalink
Spond.get_messages: add max_chats parameter and populate `Spond.m…
Browse files Browse the repository at this point in the history
…essages`.
  • Loading branch information
elliot-100 committed Aug 2, 2024
1 parent 5580c5f commit c1d3af5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
12 changes: 6 additions & 6 deletions manual_test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 20 additions & 4 deletions spond/spond.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down

0 comments on commit c1d3af5

Please sign in to comment.