This commit is contained in:
əlemi 2021-11-11 22:53:03 +01:00
parent fd4a4408d8
commit d6aec8cfad
2 changed files with 5 additions and 1 deletions

View file

@ -107,12 +107,15 @@ class Client:
if self.username and self.password:
self.token = await Token.authenticate(self.username, self.password)
self._logger.info("Authenticated from credentials")
self._authenticated = True
return True
raise AuthException("No token or credentials provided")
try:
await self.token.validate() # will raise an exc if token is invalid
self._authenticated = True
except AuthException:
await self.token.refresh()
self._authenticated = True
self._logger.warning("Refreshed Token")
return True
@ -212,6 +215,7 @@ class Client:
if packet.__class__ in self._packet_callbacks[self.dispatcher.state]: # callback for this packet
for cb in self._packet_callbacks[self.dispatcher.state][packet.__class__]:
await cb(packet)
self.dispatcher.incoming.task_done()
except asyncio.TimeoutError:
pass # need this to recheck self._processing periodically

View file

@ -101,7 +101,7 @@ class Token:
async with aiohttp.ClientSession() as sess:
async with sess.post(endpoint, headers=cls.HEADERS, data=json.dumps(data).encode('utf-8')) as res:
data = await res.json(content_type=None)
logging.info(f"Auth request | {data}")
logging.info(f"Auth request | {res.status} | {data}")
if res.status >= 400:
raise AuthException(f"Action '{endpoint.rsplit('/',1)[1]}' did not succeed")
return data