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

Small improvements and refactors #110

Merged
merged 3 commits into from
May 23, 2024
Merged
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
21 changes: 12 additions & 9 deletions spond/spond.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
#!/usr/bin/env python3

from datetime import datetime
from typing import List, Optional
from typing import TYPE_CHECKING, List, Optional

import aiohttp

if TYPE_CHECKING:
from datetime import datetime


class AuthenticationError(Exception):
pass


class Spond:

API_BASE_URL = "https://api.spond.com/core/v1/"
DT_FORMAT = "%Y-%m-%dT00:00:00.000Z"

def __init__(self, username, password):
self.username = username
self.password = password
self.api_url = "https://api.spond.com/core/v1/"
self.clientsession = aiohttp.ClientSession(cookie_jar=aiohttp.CookieJar())
self.chat_url = None
self.auth = None
Expand All @@ -34,7 +37,7 @@ def auth_headers(self):
}

async def login(self):
login_url = f"{self.api_url}login"
login_url = f"{self.API_BASE_URL}login"
data = {"email": self.username, "password": self.password}
async with self.clientsession.post(login_url, json=data) as r:
login_result = await r.json()
Expand All @@ -43,7 +46,7 @@ async def login(self):
err_msg = f"Login failed. Response received: {login_result}"
raise AuthenticationError(err_msg)

api_chat_url = f"{self.api_url}chat"
api_chat_url = f"{self.API_BASE_URL}chat"
headers = {
"content-type": "application/json",
"Authorization": f"Bearer {self.token}",
Expand Down Expand Up @@ -76,7 +79,7 @@ async def get_groups(self):
list of dict
Groups; each group is a dict.
"""
url = f"{self.api_url}groups/"
url = f"{self.API_BASE_URL}groups/"
async with self.clientsession.get(url, headers=self.auth_headers) as r:
self.groups = await r.json()
return self.groups
Expand Down Expand Up @@ -286,7 +289,7 @@ async def get_events(
list of dict
Events; each event is a dict.
"""
url = f"{self.api_url}sponds/"
url = f"{self.API_BASE_URL}sponds/"
params = {
"max": str(max_events),
"scheduled": str(include_scheduled),
Expand Down Expand Up @@ -361,7 +364,7 @@ async def update_event(self, uid, updates: dict):
if event["id"] == uid:
break

url = f"{self.api_url}sponds/{uid}"
url = f"{self.API_BASE_URL}sponds/{uid}"

base_event = {
"heading": None,
Expand Down Expand Up @@ -403,9 +406,9 @@ async def update_event(self, uid, updates: dict):
}

for key in base_event:
if event.get(key) != None and not updates.get(key):
if event.get(key) is not None and not updates.get(key):
base_event[key] = event[key]
elif updates.get(key) != None:
elif updates.get(key) is not None:
base_event[key] = updates[key]

data = dict(base_event)
Expand Down
Loading