File: users.py

package info (click to toggle)
python-yalexs 9.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,120 kB
  • sloc: python: 7,916; makefile: 3; sh: 2
file content (45 lines) | stat: -rw-r--r-- 1,163 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from __future__ import annotations

from typing import Any

from ._compat import cached_property

USER_CACHE = {}


class YaleUser:
    """Represent a yale access user."""

    def __init__(self, uuid: str, data: dict[str, Any]) -> None:
        """Initialize the YaleUser."""
        self._uuid = uuid
        self._data = data

    @cached_property
    def thumbnail_url(self) -> str | None:
        return self._data.get("imageInfo", {}).get("thumbnail", {}).get("secure_url")

    @cached_property
    def image_url(self) -> str | None:
        return self._data.get("imageInfo", {}).get("original", {}).get("secure_url")

    @cached_property
    def first_name(self) -> str | None:
        return self._data.get("FirstName")

    @cached_property
    def last_name(self) -> str | None:
        return self._data.get("LastName")

    @cached_property
    def user_type(self) -> str | None:
        return self._data.get("UserType")


def get_user_info(uuid: str) -> YaleUser | None:
    return USER_CACHE.get(uuid)


def cache_user_info(uuid: str, data: dict[str, Any]) -> None:
    if uuid not in USER_CACHE:
        USER_CACHE[uuid] = YaleUser(uuid, data)