Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up login logic #649

Merged
merged 1 commit into from
May 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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