Skip to content

Commit

Permalink
Merge pull request #143 from elliot-100/142-tech-debt-and-refactors
Browse files Browse the repository at this point in the history
Refactor: extract duplicated code and reorder tests
  • Loading branch information
elliot-100 authored Aug 2, 2024
2 parents 76a63ca + 2e72a25 commit 7fa6326
Show file tree
Hide file tree
Showing 2 changed files with 212 additions and 182 deletions.
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

0 comments on commit 7fa6326

Please sign in to comment.