2
2
3
3
from __future__ import annotations
4
4
5
- from typing import TYPE_CHECKING , Optional
5
+ from typing import TYPE_CHECKING , ClassVar , Optional
6
6
7
7
from .base import _SpondBase
8
8
@@ -14,6 +14,9 @@ class Spond(_SpondBase):
14
14
15
15
DT_FORMAT = "%Y-%m-%dT00:00:00.000Z"
16
16
17
+ _EVENT : ClassVar = "event"
18
+ _GROUP : ClassVar = "group"
19
+
17
20
def __init__ (self , username : str , password : str ) -> None :
18
21
super ().__init__ (username , password , "https://api.spond.com/core/v1/" )
19
22
self .chat_url = None
@@ -44,7 +47,6 @@ async def get_groups(self) -> list[dict]:
44
47
self .groups = await r .json ()
45
48
return self .groups
46
49
47
- @_SpondBase .require_authentication
48
50
async def get_group (self , uid : str ) -> dict :
49
51
"""
50
52
Get a group by unique ID.
@@ -62,15 +64,9 @@ async def get_group(self, uid: str) -> dict:
62
64
Raises
63
65
------
64
66
KeyError if no group is matched.
65
- """
66
67
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 )
74
70
75
71
@_SpondBase .require_authentication
76
72
async def get_person (self , user : str ) -> dict :
@@ -286,7 +282,6 @@ async def get_events(
286
282
self .events = await r .json ()
287
283
return self .events
288
284
289
- @_SpondBase .require_authentication
290
285
async def get_event (self , uid : str ) -> dict :
291
286
"""
292
287
Get an event by unique ID.
@@ -306,13 +301,7 @@ async def get_event(self, uid: str) -> dict:
306
301
KeyError if no event is matched.
307
302
308
303
"""
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 )
316
305
317
306
@_SpondBase .require_authentication
318
307
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:
434
423
url , headers = self .auth_headers , json = payload
435
424
) as r :
436
425
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