Skip to content

Commit

Permalink
Merge pull request #84 from elliot-100/82-login-api-no-longer-working
Browse files Browse the repository at this point in the history
Rework login/auth for new API endpoint, and bearer token instead of cookies. Code from @jamestutton.
  • Loading branch information
elliot-100 authored Aug 24, 2023
2 parents dd30bac + 2587e60 commit a30f862
Showing 1 changed file with 40 additions and 28 deletions.
68 changes: 40 additions & 28 deletions spond/spond.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,37 @@ class Spond:
def __init__(self, username, password):
self.username = username
self.password = password
self.apiurl = "https://spond.com/api/2.1/"
self.api_url = "https://api.spond.com/core/v1/"
self.clientsession = aiohttp.ClientSession(cookie_jar=aiohttp.CookieJar())
self.chaturl = None
self.chat_url = None
self.auth = None
self.cookie = None
self.token = None
self.groups = None
self.events = None

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

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

self.chaturl = result["url"]
async with self.clientsession.post(login_url, json=data) as r:
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"]

async def get_groups(self):
Expand All @@ -41,10 +53,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 = self.apiurl + "groups/"
async with self.clientsession.get(url) as r:
url = f"{self.api_url}groups/"
async with self.clientsession.get(url, headers=self.auth_headers) as r:
self.groups = await r.json()
return self.groups

Expand All @@ -62,7 +74,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 +98,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,9 +127,9 @@ 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 = self.chaturl + "/chats/?max=10"
url = f"{self.chat_url}/chats/?max=10"
headers = {"auth": self.auth}
async with self.clientsession.get(url, headers=headers) as r:
return await r.json()
Expand All @@ -140,9 +152,9 @@ 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 = self.chaturl + "/messages"
url = f"{self.chat_url}/messages"
data = {"chatId": chat_id, "text": text, "type": "TEXT"}
headers = {"auth": self.auth}
r = await self.clientsession.post(url, json=data, headers=headers)
Expand Down Expand Up @@ -177,14 +189,14 @@ 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:
user_uid = user_obj["profile"]["id"]
else:
return False
url = self.chaturl + "/messages"
url = f"{self.chat_url}/messages"
data = {
"text": text,
"type": "TEXT",
Expand Down Expand Up @@ -244,10 +256,10 @@ 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.apiurl}sponds/?"
f"{self.api_url}sponds/?"
f"max={max_events}"
f"&scheduled={include_scheduled}"
)
Expand Down Expand Up @@ -280,7 +292,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,15 +317,15 @@ 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()
for event in self.events:
if event["id"] == uid:
break

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

base_event = {
"heading": None,
Expand Down

0 comments on commit a30f862

Please sign in to comment.