Get new token via refresh-token #10

This commit is contained in:
Andre Basche 2023-04-09 18:43:57 +02:00
parent 8fa01343bc
commit eb6741145a
2 changed files with 31 additions and 7 deletions

View file

@ -66,23 +66,31 @@ class HonConnectionHandler(HonBaseConnectionHandler):
return {h: v for h, v in self._request_headers.items() if h not in headers}
@asynccontextmanager
async def get(self, *args, loop=0, **kwargs):
async def _intercept(self, method, *args, loop=0, **kwargs):
kwargs["headers"] = await self._check_headers(kwargs.get("headers", {}))
async with self._session.get(*args, **kwargs) as response:
async with method(*args, **kwargs) as response:
if response.status == 403 and not loop:
_LOGGER.info("Try refreshing token...")
await self._auth.refresh()
yield await self._intercept(method, *args, loop=loop + 1, **kwargs)
elif response.status == 403 and loop < 2:
_LOGGER.warning("%s - Error %s - %s", response.request_info.url, response.status, await response.text())
await self.create()
yield await self.get(*args, loop=loop + 1, **kwargs)
yield await self._intercept(method, *args, loop=loop + 1, **kwargs)
elif loop >= 2:
_LOGGER.error("%s - Error %s - %s", response.request_info.url, response.status, await response.text())
raise PermissionError()
raise PermissionError("Login failure")
else:
yield response
@asynccontextmanager
async def get(self, *args, **kwargs):
async with self._intercept(self._session.get, *args, **kwargs) as response:
yield response
@asynccontextmanager
async def post(self, *args, **kwargs):
kwargs["headers"] = await self._check_headers(kwargs.get("headers", {}))
async with self._session.post(*args, **kwargs) as response:
async with self._intercept(self._session.post, *args, **kwargs) as response:
yield response