Skip to content

session

numerous.session

Package for managing user sessions and cookie-based authentication.

Session

A session with Numerous.

Source code in numerous/session/session.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class Session:
    """A session with Numerous."""

    def __init__(self, cg: CookieGetter, _client: GraphQLClient | None = None) -> None:
        self._cg = cg
        self._user: User | None = None
        self._client = _client

    def _user_info(self) -> dict[str, str]:
        cookies = self._cg.cookies()
        user_info_b64 = cookies.get("numerous_user_info")
        if not user_info_b64:
            msg = "Invalid user info in cookie or cookie is missing"
            raise ValueError(msg)
        try:
            user_info_json = base64.b64decode(user_info_b64).decode("utf-8")
            return {
                str(key): str(val) for key, val in json.loads(user_info_json).items()
            }
        except ValueError as err:
            msg = "Invalid user info in cookie or cookie is missing"
            raise ValueError(msg) from err

    @property
    def user(self) -> User:
        """The user associated with the current session."""
        if self._user is None:
            user_info = self._user_info()
            self._user = User.from_user_info(user_info, self._client)
        return self._user

    @property
    def cookies(self) -> dict[str, str]:
        """The cookies associated with the current session."""
        return self._cg.cookies()

cookies: dict[str, str] property

The cookies associated with the current session.

user: User property

The user associated with the current session.

User dataclass

Represents a Numerous platform user.

Attributes:

Name Type Description
id str

The unique identifier for the user.

name str

The full name of the user.

email str | None

The email of the user.

Source code in numerous/session/user.py
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
@dataclass
class User:
    """
    Represents a Numerous platform user.

    Attributes:
        id (str): The unique identifier for the user.
        name (str): The full name of the user.
        email (str | None): The email of the user.

    """

    id: str
    name: str
    email: str | None = None
    _client: Client | None = None

    @property
    def collection(self) -> CollectionReference:
        """
        A user's collection.

        A collection scoped for the current user. It is a child-collection of the
        root collection with the key "users", and has the user's ID as key.

        Equivalent to:
        >>> collection("users").collection(user.id)

        Returns:
            The collection for this user.

        """
        return collection("users", self._client).collection(self.id)

    @staticmethod
    def from_user_info(
        user_info: dict[str, Any], _client: Client | None = None
    ) -> User:
        """
        Create a User instance from a dictionary of user information.

        Args:
            user_info: A dictionary containing user information.

        Returns:
            A new User instance created from the provided information.

        """
        return User(
            id=user_info["user_id"],
            name=user_info["user_full_name"],
            email=user_info.get("user_email"),
            _client=_client,
        )

collection: CollectionReference property

A user's collection.

A collection scoped for the current user. It is a child-collection of the root collection with the key "users", and has the user's ID as key.

Equivalent to:

collection("users").collection(user.id)

Returns:

Type Description
CollectionReference

The collection for this user.

from_user_info(user_info, _client=None) staticmethod

Create a User instance from a dictionary of user information.

Parameters:

Name Type Description Default
user_info dict[str, Any]

A dictionary containing user information.

required

Returns:

Type Description
User

A new User instance created from the provided information.

Source code in numerous/session/user.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
@staticmethod
def from_user_info(
    user_info: dict[str, Any], _client: Client | None = None
) -> User:
    """
    Create a User instance from a dictionary of user information.

    Args:
        user_info: A dictionary containing user information.

    Returns:
        A new User instance created from the provided information.

    """
    return User(
        id=user_info["user_id"],
        name=user_info["user_full_name"],
        email=user_info.get("user_email"),
        _client=_client,
    )