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

Mark members as non-public and move public AuthenticationError #163

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
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