Skip to content

Commit

Permalink
Rework login/auth for new API endpoint, and bearer token instead of c…
Browse files Browse the repository at this point in the history
…ookies (by @jamestutton)
  • Loading branch information
elliot-100 committed Aug 22, 2023
1 parent c596027 commit 8d708c3
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions spond/spond.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,37 @@ class Spond:
def __init__(self, username, password):
self.username = username
self.password = password
self.api_url = "https://spond.com/api/2.1/"
self.api_url = "https://api.spond.com/core/v1/"
self.clientsession = aiohttp.ClientSession(cookie_jar=aiohttp.CookieJar())
self.chat_url = None
self.auth = None
self.cookie = None
self.token = None
self.groups = None
self.events = None

@property
def auth_headers(self):
headers = {
"content-type": "application/json",
"Authorization": f"Bearer {self.token}",
"auth": f"{self.auth}",
}
return headers

async def login(self):
login_url = f"{self.api_url}login"
data = {"email": self.username, "password": self.password}
async with self.clientsession.post(login_url, json=data) as r:
self.cookie = r.cookies["auth"]
chat_api_url = f"{self.api_url}chat"
headers = {"content-type": "application/json;charset=utf-8"}
r = await self.clientsession.post(chat_api_url, headers=headers)
result = await r.json()
login_result = await r.json()
self.token = login_result["loginToken"]

api_chat_url = f"{self.api_url}chat"
headers = {
"content-type": "application/json",
"Authorization": f"Bearer {self.token}",
}
r = await self.clientsession.post(api_chat_url, headers=headers)
result = await r.json()
self.chat_url = result["url"]
self.auth = result["auth"]

Expand All @@ -41,10 +54,10 @@ async def get_groups(self):
list of dict
Groups; each group is a dict.
"""
if not self.cookie:
if not self.token:
await self.login()
url = f"{self.api_url}groups/"
async with self.clientsession.get(url) as r:
async with self.clientsession.get(url, headers=self.auth_headers) as r:
self.groups = await r.json()
return self.groups

Expand All @@ -62,7 +75,7 @@ async def get_group(self, uid) -> dict:
-------
Details of the group.
"""
if not self.cookie:
if not self.token:
await self.login()
if not self.groups:
await self.get_groups()
Expand All @@ -86,7 +99,7 @@ async def get_person(self, user) -> dict:
-------
Member or guardian's details.
"""
if not self.cookie:
if not self.token:
await self.login()
if not self.groups:
await self.get_groups()
Expand Down Expand Up @@ -115,7 +128,7 @@ async def get_person(self, user) -> dict:
raise IndexError

async def get_messages(self):
if not self.cookie:
if not self.token:
await self.login()
url = f"{self.chat_url}/chats/?max=10"
headers = {"auth": self.auth}
Expand All @@ -140,7 +153,7 @@ async def _continue_chat(self, chat_id, text):
dict
Result of the sending.
"""
if not self.cookie:
if not self.token:
await self.login()
url = f"{self.chat_url}/messages"
data = {"chatId": chat_id, "text": text, "type": "TEXT"}
Expand Down Expand Up @@ -177,7 +190,7 @@ async def send_message(self, text, user=None, group_uid=None, chat_id=None):
"error": "wrong usage, group_id and user_id needed or continue chat with chat_id"
}

if not self.cookie:
if not self.token:
await self.login()
user_obj = await self.get_person(user)
if user_obj:
Expand Down Expand Up @@ -244,7 +257,7 @@ async def get_events(
list of dict
Events; each event is a dict.
"""
if not self.cookie:
if not self.token:
await self.login()
url = (
f"{self.api_url}sponds/?"
Expand Down Expand Up @@ -280,7 +293,7 @@ async def get_event(self, uid) -> dict:
-------
Details of the event.
"""
if not self.cookie:
if not self.token:
await self.login()
if not self.events:
await self.get_events()
Expand All @@ -305,7 +318,7 @@ async def update_event(self, uid, updates: dict):
json results of post command
"""
if not self.cookie:
if not self.token:
await self.login()
if not self.events:
await self.get_events()
Expand Down

0 comments on commit 8d708c3

Please sign in to comment.