Skip to content

Commit

Permalink
Merge pull request #110 from elliot-100/misc-improvements2
Browse files Browse the repository at this point in the history
Small style/refactor improvements
  • Loading branch information
elliot-100 authored May 23, 2024
2 parents 5a09879 + 187bbc9 commit 6b4a9fc
Showing 1 changed file with 12 additions and 9 deletions.
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

0 comments on commit 6b4a9fc

Please sign in to comment.