Skip to content

Commit 7fa6326

Browse files
authored
Merge pull request #143 from elliot-100/142-tech-debt-and-refactors
Refactor: extract duplicated code and reorder tests
2 parents 76a63ca + 2e72a25 commit 7fa6326

File tree

2 files changed

+212
-182
lines changed

2 files changed

+212
-182
lines changed

spond/spond.py

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from __future__ import annotations
44

5-
from typing import TYPE_CHECKING, Optional
5+
from typing import TYPE_CHECKING, ClassVar, Optional
66

77
from .base import _SpondBase
88

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

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

17+
_EVENT: ClassVar = "event"
18+
_GROUP: ClassVar = "group"
19+
1720
def __init__(self, username: str, password: str) -> None:
1821
super().__init__(username, password, "https://api.spond.com/core/v1/")
1922
self.chat_url = None
@@ -44,7 +47,6 @@ async def get_groups(self) -> list[dict]:
4447
self.groups = await r.json()
4548
return self.groups
4649

47-
@_SpondBase.require_authentication
4850
async def get_group(self, uid: str) -> dict:
4951
"""
5052
Get a group by unique ID.
@@ -62,15 +64,9 @@ async def get_group(self, uid: str) -> dict:
6264
Raises
6365
------
6466
KeyError if no group is matched.
65-
"""
6667
67-
if not self.groups:
68-
await self.get_groups()
69-
for group in self.groups:
70-
if group["id"] == uid:
71-
return group
72-
errmsg = f"No group with id='{uid}'."
73-
raise KeyError(errmsg)
68+
"""
69+
return await self._get_entity(self._GROUP, uid)
7470

7571
@_SpondBase.require_authentication
7672
async def get_person(self, user: str) -> dict:
@@ -286,7 +282,6 @@ async def get_events(
286282
self.events = await r.json()
287283
return self.events
288284

289-
@_SpondBase.require_authentication
290285
async def get_event(self, uid: str) -> dict:
291286
"""
292287
Get an event by unique ID.
@@ -306,13 +301,7 @@ async def get_event(self, uid: str) -> dict:
306301
KeyError if no event is matched.
307302
308303
"""
309-
if not self.events:
310-
await self.get_events()
311-
for event in self.events:
312-
if event["id"] == uid:
313-
return event
314-
errmsg = f"No event with id='{uid}'."
315-
raise KeyError(errmsg)
304+
return await self._get_entity(self._EVENT, uid)
316305

317306
@_SpondBase.require_authentication
318307
async def update_event(self, uid: str, updates: dict):
@@ -434,3 +423,45 @@ async def change_response(self, uid: str, user: str, payload: dict) -> dict:
434423
url, headers=self.auth_headers, json=payload
435424
) as r:
436425
return await r.json()
426+
427+
@_SpondBase.require_authentication
428+
async def _get_entity(self, entity_type: str, uid: str) -> dict:
429+
"""
430+
Get an event or group by unique ID.
431+
432+
Subject to authenticated user's access.
433+
434+
Parameters
435+
----------
436+
entity_type : str
437+
self._EVENT or self._GROUP.
438+
uid : str
439+
UID of the entity.
440+
441+
Returns
442+
-------
443+
Details of the entity.
444+
445+
Raises
446+
------
447+
KeyError if no entity is matched.
448+
NotImplementedError if no/unsupported entity type is specified.
449+
450+
"""
451+
if entity_type == self._EVENT:
452+
if not self.events:
453+
await self.get_events()
454+
entities = self.events
455+
elif entity_type == self._GROUP:
456+
if not self.groups:
457+
await self.get_groups()
458+
entities = self.groups
459+
else:
460+
err_msg = f"Entity type '{entity_type}' is not supported."
461+
raise NotImplementedError(err_msg)
462+
463+
for entity in entities:
464+
if entity["id"] == uid:
465+
return entity
466+
errmsg = f"No {entity_type} with id='{uid}'."
467+
raise KeyError(errmsg)

0 commit comments

Comments
 (0)