40 lines
1.2 KiB
Python
40 lines
1.2 KiB
Python
"""Emby user lookups."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
def _normalize_user(raw: dict) -> dict:
|
|
return {"id": raw.get("Id", ""), "name": raw.get("Name", "")}
|
|
|
|
|
|
async def fetch_users(client) -> list[dict]:
|
|
"""Return all Emby users as ``[{"id", "name"}]``.
|
|
|
|
``GET /Users`` returns a bare JSON array on most Emby builds, but some return
|
|
a ``{"Items": [...]}`` envelope. Handle both.
|
|
"""
|
|
data = await client.get("/Users")
|
|
raw_users = data.get("Items", []) if isinstance(data, dict) else (data or [])
|
|
return [_normalize_user(u) for u in raw_users if u.get("Id")]
|
|
|
|
|
|
async def resolve_user_id_by_name(client, name: str) -> str | None:
|
|
"""Case-insensitive lookup of a user id by display name."""
|
|
if not name:
|
|
return None
|
|
target = name.strip().casefold()
|
|
for user in await fetch_users(client):
|
|
if user["name"].casefold() == target:
|
|
return user["id"]
|
|
return None
|
|
|
|
|
|
async def get_user(client, user_id: str) -> dict | None:
|
|
"""Return ``{"id", "name"}`` for a user id, or ``None`` if not found."""
|
|
if not user_id:
|
|
return None
|
|
for user in await fetch_users(client):
|
|
if user["id"] == user_id:
|
|
return user
|
|
return None
|