Skip to content

Commit

Permalink
Clean up login logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Kane610 committed May 2, 2024
1 parent 554d873 commit ec3e27e
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions aiounifi/interfaces/connectivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,20 @@ async def login(self) -> None:

response, bytes_data = await self._request("post", url, json=auth)

if response.content_type == "application/json":
data: TypedApiResponse = orjson.loads(bytes_data)
if "meta" in data and data["meta"]["rc"] == "error":
LOGGER.error("Login failed '%s'", data)
raise ERRORS.get(data["meta"]["msg"], AiounifiException)
if response.content_type != "application/json":
LOGGER.debug("Login Failed not JSON: '%s'", bytes_data)
raise RequestError("Login Failed: Host starting up")

if (csrf_token := response.headers.get("x-csrf-token")) is not None:
self.headers["x-csrf-token"] = csrf_token
data: TypedApiResponse = orjson.loads(bytes_data)
if data.get("meta", {}).get("rc") == "error":
LOGGER.error("Login failed '%s'", data)
raise ERRORS.get(data["meta"]["msg"], AiounifiException)

if (cookie := response.headers.get("Set-Cookie")) is not None:
self.headers["Cookie"] = cookie
if (csrf_token := response.headers.get("x-csrf-token")) is not None:
self.headers["x-csrf-token"] = csrf_token

else:
LOGGER.debug("Login Failed not JSON: '%s'", bytes_data)
raise RequestError("Login Failed: Host starting up")
if (cookie := response.headers.get("Set-Cookie")) is not None:
self.headers["Cookie"] = cookie

self.can_retry_login = True
LOGGER.debug("Logged in to UniFi %s", url)
Expand Down

0 comments on commit ec3e27e

Please sign in to comment.