SIMNet/simnet/client/cookies.py
洛水居室 b74b9aef33
Refactor BaseClient class and add cookie property accessor
Added a `cookies` property accessor to the `BaseClient` class, which retrieves cookies from the `httpx` AsyncClient object and returns a `simnet.client.cookies.Cookies` object (which is a custom implementation of the `httpx.Cookies` class). Additionally, the initialization function was refactored to better support using cookies. Now, if `account_id` is not passed in, it will be retrieved from the cookies.
2023-05-09 15:19:36 +08:00

28 lines
840 B
Python

from typing import Optional
from httpx import Cookies as _Cookies
__all__ = ("Cookies",)
class Cookies(_Cookies):
"""An extension of the `httpx.Cookies` class that includes additional functionality."""
COOKIE_USER_ID_NAMES = ("ltuid", "account_id", "stuid", "ltuid_v2", "account_id_v2")
@property
def account_id(self) -> Optional[int]:
"""Return the user account ID if present in the cookies.
If one of the user ID cookies exists in the cookies, return its integer value.
Otherwise, return `None`.
Returns:
Optional[int]: The user account ID, or `None` if it is not present in the cookies.
"""
for name in self.COOKIE_USER_ID_NAMES:
value = self.get(name)
if value is not None:
return int(value)
return None