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

Refactor: extract duplicated code and reorder tests #143

Merged
merged 2 commits into from
Aug 2, 2024
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
67 changes: 49 additions & 18 deletions spond/spond.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import annotations

from typing import TYPE_CHECKING, Optional
from typing import TYPE_CHECKING, ClassVar, Optional

from .base import _SpondBase

Expand All @@ -14,6 +14,9 @@ class Spond(_SpondBase):

DT_FORMAT = "%Y-%m-%dT00:00:00.000Z"

_EVENT: ClassVar = "event"
_GROUP: ClassVar = "group"

def __init__(self, username: str, password: str) -> None:
super().__init__(username, password, "https://api.spond.com/core/v1/")
self.chat_url = None
Expand Down Expand Up @@ -44,7 +47,6 @@ async def get_groups(self) -> list[dict]:
self.groups = await r.json()
return self.groups

@_SpondBase.require_authentication
async def get_group(self, uid: str) -> dict:
"""
Get a group by unique ID.
Expand All @@ -62,15 +64,9 @@ async def get_group(self, uid: str) -> dict:
Raises
------
KeyError if no group is matched.
"""

if not self.groups:
await self.get_groups()
for group in self.groups:
if group["id"] == uid:
return group
errmsg = f"No group with id='{uid}'."
raise KeyError(errmsg)
"""
return await self._get_entity(self._GROUP, uid)

@_SpondBase.require_authentication
async def get_person(self, user: str) -> dict:
Expand Down Expand Up @@ -286,7 +282,6 @@ async def get_events(
self.events = await r.json()
return self.events

@_SpondBase.require_authentication
async def get_event(self, uid: str) -> dict:
"""
Get an event by unique ID.
Expand All @@ -306,13 +301,7 @@ async def get_event(self, uid: str) -> dict:
KeyError if no event is matched.

"""
if not self.events:
await self.get_events()
for event in self.events:
if event["id"] == uid:
return event
errmsg = f"No event with id='{uid}'."
raise KeyError(errmsg)
return await self._get_entity(self._EVENT, uid)

@_SpondBase.require_authentication
async def update_event(self, uid: str, updates: dict):
Expand Down Expand Up @@ -434,3 +423,45 @@ async def change_response(self, uid: str, user: str, payload: dict) -> dict:
url, headers=self.auth_headers, json=payload
) as r:
return await r.json()

@_SpondBase.require_authentication
async def _get_entity(self, entity_type: str, uid: str) -> dict:
"""
Get an event or group by unique ID.

Subject to authenticated user's access.

Parameters
----------
entity_type : str
self._EVENT or self._GROUP.
uid : str
UID of the entity.

Returns
-------
Details of the entity.

Raises
------
KeyError if no entity is matched.
NotImplementedError if no/unsupported entity type is specified.

"""
if entity_type == self._EVENT:
if not self.events:
await self.get_events()
entities = self.events
elif entity_type == self._GROUP:
if not self.groups:
await self.get_groups()
entities = self.groups
else:
err_msg = f"Entity type '{entity_type}' is not supported."
raise NotImplementedError(err_msg)

for entity in entities:
if entity["id"] == uid:
return entity
errmsg = f"No {entity_type} with id='{uid}'."
raise KeyError(errmsg)
Loading