From 22dc4e8be4de65f73fab90370c7b03bffa8b20dd Mon Sep 17 00:00:00 2001 From: Brad Stein Date: Fri, 23 Jan 2026 18:33:02 -0300 Subject: [PATCH] refactor: reduce rotation check branches --- ariadne/services/firefly.py | 60 ++++++++++++++++++++++++------------- ariadne/services/wger.py | 60 ++++++++++++++++++++++++------------- 2 files changed, 80 insertions(+), 40 deletions(-) diff --git a/ariadne/services/firefly.py b/ariadne/services/firefly.py index 075609c..6ba4087 100644 --- a/ariadne/services/firefly.py +++ b/ariadne/services/firefly.py @@ -472,28 +472,48 @@ class FireflyService: def check_rotation_for_user(self, username: str) -> dict[str, Any]: username = (username or "").strip() + status = "error" + detail = "" + rotated = None if not username: - return {"status": "error", "detail": "missing username"} - if not keycloak_admin.ready(): - return {"status": "error", "detail": "keycloak admin not configured"} + detail = "missing username" + elif not keycloak_admin.ready(): + detail = "keycloak admin not configured" + else: + user = keycloak_admin.find_user(username) + if not isinstance(user, dict): + detail = "user not found" + else: + user_id = user.get("id") if isinstance(user.get("id"), str) else "" + if not user_id: + detail = "missing user id" + else: + full = keycloak_admin.get_user(user_id) + prepared = _build_sync_input(full) + if isinstance(prepared, UserSyncOutcome): + if prepared.status == "skipped": + status = "ok" + rotated = False + else: + status = prepared.status + detail = prepared.detail or "" + else: + outcome = self._rotation_outcome(prepared) + if outcome.status == "synced": + status = "ok" + rotated = True + elif outcome.status == "skipped": + status = "ok" + rotated = False + else: + detail = outcome.detail or "rotation check failed" - user = keycloak_admin.find_user(username) - if not isinstance(user, dict): - return {"status": "error", "detail": "user not found"} - user_id = user.get("id") if isinstance(user.get("id"), str) else "" - if not user_id: - return {"status": "error", "detail": "missing user id"} - full = keycloak_admin.get_user(user_id) - prepared = _build_sync_input(full) - if isinstance(prepared, UserSyncOutcome): - return {"status": prepared.status, "detail": prepared.detail or ""} - - outcome = self._rotation_outcome(prepared) - if outcome.status == "synced": - return {"status": "ok", "rotated": True} - if outcome.status == "skipped": - return {"status": "ok", "rotated": False} - return {"status": "error", "detail": outcome.detail or "rotation check failed"} + result = {"status": status} + if status == "ok": + result["rotated"] = bool(rotated) + elif detail: + result["detail"] = detail + return result def sync_user(self, email: str, password: str, wait: bool = True) -> dict[str, Any]: email = (email or "").strip() diff --git a/ariadne/services/wger.py b/ariadne/services/wger.py index d329a59..e6138c2 100644 --- a/ariadne/services/wger.py +++ b/ariadne/services/wger.py @@ -431,28 +431,48 @@ class WgerService: def check_rotation_for_user(self, username: str) -> dict[str, Any]: username = (username or "").strip() + status = "error" + detail = "" + rotated = None if not username: - return {"status": "error", "detail": "missing username"} - if not keycloak_admin.ready(): - return {"status": "error", "detail": "keycloak admin not configured"} + detail = "missing username" + elif not keycloak_admin.ready(): + detail = "keycloak admin not configured" + else: + user = keycloak_admin.find_user(username) + if not isinstance(user, dict): + detail = "user not found" + else: + user_id = user.get("id") if isinstance(user.get("id"), str) else "" + if not user_id: + detail = "missing user id" + else: + full = keycloak_admin.get_user(user_id) + prepared = _build_sync_input(full) + if isinstance(prepared, UserSyncOutcome): + if prepared.status == "skipped": + status = "ok" + rotated = False + else: + status = prepared.status + detail = prepared.detail or "" + else: + outcome = self._rotation_outcome(prepared) + if outcome.status == "synced": + status = "ok" + rotated = True + elif outcome.status == "skipped": + status = "ok" + rotated = False + else: + detail = outcome.detail or "rotation check failed" - user = keycloak_admin.find_user(username) - if not isinstance(user, dict): - return {"status": "error", "detail": "user not found"} - user_id = user.get("id") if isinstance(user.get("id"), str) else "" - if not user_id: - return {"status": "error", "detail": "missing user id"} - full = keycloak_admin.get_user(user_id) - prepared = _build_sync_input(full) - if isinstance(prepared, UserSyncOutcome): - return {"status": prepared.status, "detail": prepared.detail or ""} - - outcome = self._rotation_outcome(prepared) - if outcome.status == "synced": - return {"status": "ok", "rotated": True} - if outcome.status == "skipped": - return {"status": "ok", "rotated": False} - return {"status": "error", "detail": outcome.detail or "rotation check failed"} + result = {"status": status} + if status == "ok": + result["rotated"] = bool(rotated) + elif detail: + result["detail"] = detail + return result def sync_user(self, username: str, email: str, password: str, wait: bool = True) -> dict[str, Any]: username = (username or "").strip()