From ec3e27e27bb8e3dee2ecbfbe06f98779d7492958 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Tue, 23 Apr 2024 22:15:24 +0200 Subject: [PATCH] Clean up login logic --- aiounifi/interfaces/connectivity.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/aiounifi/interfaces/connectivity.py b/aiounifi/interfaces/connectivity.py index 2bffd4b7e..5f275f64b 100644 --- a/aiounifi/interfaces/connectivity.py +++ b/aiounifi/interfaces/connectivity.py @@ -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)