Skip to content

Commit

Permalink
Mark spond.spond.Spond.chat_url, ...Spond.auth, `...Spond.login_c…
Browse files Browse the repository at this point in the history
…hat()` as non-public; move `spond.base.AuthenticationError` -> `spond.AuthenticationError`

Mark `spond.spond.Spond.chat_url`, `...Spond.auth`, `...Spond.login_chat()` as non-public
  • Loading branch information
elliot-100 committed Oct 22, 2024
1 parent 997ea5e commit eeb748e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
6 changes: 6 additions & 0 deletions spond/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@

JSONDict: TypeAlias = Dict[str, Any]
"""Simple alias for type hinting `dict`s that can be passed to/from JSON-handling functions."""


class AuthenticationError(Exception):
"""Error raised on Spond authentication failure."""

pass
4 changes: 1 addition & 3 deletions spond/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

import aiohttp


class AuthenticationError(Exception):
pass
from spond import AuthenticationError


class _SpondBase(ABC):
Expand Down
34 changes: 17 additions & 17 deletions spond/spond.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ class Spond(_SpondBase):

def __init__(self, username: str, password: str) -> None:
super().__init__(username, password, "https://api.spond.com/core/v1/")
self.chat_url = None
self.auth = None
self._chat_url = None
self._auth = None
self.groups: list[JSONDict] | None = None
self.events: list[JSONDict] | None = None
self.messages: list[JSONDict] | None = None

async def login_chat(self) -> None:
async def _login_chat(self) -> None:
api_chat_url = f"{self.api_url}chat"
r = await self.clientsession.post(api_chat_url, headers=self.auth_headers)
result = await r.json()
self.chat_url = result["url"]
self.auth = result["auth"]
self._chat_url = result["url"]
self._auth = result["auth"]

@_SpondBase.require_authentication
async def get_groups(self) -> list[JSONDict] | None:
Expand Down Expand Up @@ -140,12 +140,12 @@ async def get_messages(self, max_chats: int = 100) -> list[JSONDict] | None:
are available.
"""
if not self.auth:
await self.login_chat()
url = f"{self.chat_url}/chats/"
if not self._auth:
await self._login_chat()
url = f"{self._chat_url}/chats/"
async with self.clientsession.get(
url,
headers={"auth": self.auth},
headers={"auth": self._auth},
params={"max": str(max_chats)},
) as r:
self.messages = await r.json()
Expand All @@ -170,11 +170,11 @@ async def _continue_chat(self, chat_id: str, text: str) -> JSONDict:
JSONDict
Result of the sending.
"""
if not self.auth:
await self.login_chat()
url = f"{self.chat_url}/messages"
if not self._auth:
await self._login_chat()
url = f"{self._chat_url}/messages"
data = {"chatId": chat_id, "text": text, "type": "TEXT"}
r = await self.clientsession.post(url, json=data, headers={"auth": self.auth})
r = await self.clientsession.post(url, json=data, headers={"auth": self._auth})
return await r.json()

@_SpondBase.require_authentication
Expand Down Expand Up @@ -208,8 +208,8 @@ async def send_message(
dict
Result of the sending.
"""
if self.auth is None:
await self.login_chat()
if self._auth is None:
await self._login_chat()

if chat_id is not None:
return self._continue_chat(chat_id, text)
Expand All @@ -223,14 +223,14 @@ async def send_message(
user_uid = user_obj["profile"]["id"]
else:
return False
url = f"{self.chat_url}/messages"
url = f"{self._chat_url}/messages"
data = {
"text": text,
"type": "TEXT",
"recipient": user_uid,
"groupId": group_uid,
}
r = await self.clientsession.post(url, json=data, headers={"auth": self.auth})
r = await self.clientsession.post(url, json=data, headers={"auth": self._auth})
return await r.json()

@_SpondBase.require_authentication
Expand Down

0 comments on commit eeb748e

Please sign in to comment.