Skip to content

Commit

Permalink
Fix KeyError for missing httoken, Andre0512/speedport#2
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre0512 committed Oct 17, 2023
1 parent 885d55b commit 60a816d
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions speedport/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ async def get(self, path: str, auth: bool = False, referer: str = ""):
url = f"{self._url}/{path}"
if referer:
referer = f"{self._url}/{referer}"
url += f"?_tn={await self._get_httoken(referer)}"
if token := await self._get_httoken(referer):
url += f"?_tn={token}"
async with self._session.get(url, headers={"Referer": referer}) as response:
_LOGGER.debug("GET - %s - %s", url, response.status)
key = self._login_key if auth else const.DEFAULT_KEY
Expand All @@ -98,7 +99,8 @@ async def get(self, path: str, auth: bool = False, referer: str = ""):
async def post(self, path: str, data: dict[str, str], referer: str):
url = f"{self._url}/{path}"
referer = f"{self._url}/{referer}"
data.update({"httoken": await self._get_httoken(referer)})
if token := await self._get_httoken(referer):
data.update({"httoken": token})
data = "&".join([f"{k}={v}" for k, v in data.items()])
data = encode(data, key=self._login_key)
async with self._session.post(
Expand All @@ -110,10 +112,12 @@ async def post(self, path: str, data: dict[str, str], referer: str):
_LOGGER.debug("POST - %s - %s", url, response.status)
return decode(await response.text(), key=self._login_key)

async def _get_httoken(self, url: str):
async def _get_httoken(self, url: str) -> str:
async with self._session.get(url) as response:
_LOGGER.debug("GET - %s - %s", url, response.status)
return re.findall("_httoken = (\\d+)", await response.text())[0]
if token := re.findall("httoken = (\\d+)", await response.text()):
return token[0]
return ""

async def _get_login_key(self):
data = {"getChallenge": "1"}
Expand Down

0 comments on commit 60a816d

Please sign in to comment.