diff --git a/docs/index.md b/docs/index.md index 8c780aa..15f2c89 100644 --- a/docs/index.md +++ b/docs/index.md @@ -42,14 +42,26 @@ async with await KworkClient.login("username", "password") as client: # Client automatically closes ``` -### Restore from token +### Save and restore session ```python -# Save token after login -token = client._token +# Save credentials after login +client = await KworkClient.login("username", "password") + +# Option 1: Save token only +token = client.token + +# Option 2: Save full credentials (token + cookies) +creds = client.credentials +import json +with open("session.json", "w") as f: + json.dump(creds, f) # Later, restore session client = KworkClient(token=token) +# or +client = KworkClient(**creds) + user_info = await client.user.get_info() ``` diff --git a/src/kwork_api/client.py b/src/kwork_api/client.py index 446a0fe..62c51c2 100644 --- a/src/kwork_api/client.py +++ b/src/kwork_api/client.py @@ -131,6 +131,67 @@ class KworkClient: # Initialize HTTP client self._client: Optional[httpx.AsyncClient] = None + @property + def token(self) -> Optional[str]: + """ + Web auth token для аутентификации. + + Returns: + Токен или None если клиент не аутентифицирован. + + Example: + # Сохранение токена для последующего использования + client = await KworkClient.login("user", "pass") + token = client.token + + # Позже: восстановление сессии + client = KworkClient(token=token) + """ + return self._token + + @property + def cookies(self) -> dict[str, str]: + """ + Session cookies. + + Returns: + Словарь cookies включая web_auth_token. + + Example: + # Сохранение полной сессии + client = await KworkClient.login("user", "pass") + creds = client.credentials + + # Восстановление + client = KworkClient(**creds) + """ + return self._cookies.copy() + + @property + def credentials(self) -> dict[str, Optional[str]]: + """ + Учётные данные для восстановления сессии. + + Returns: + Словарь с token и cookies для передачи в KworkClient(). + + Example: + # Сохранение + client = await KworkClient.login("user", "pass") + import json + with open("session.json", "w") as f: + json.dump(client.credentials, f) + + # Восстановление + with open("session.json") as f: + creds = json.load(f) + client = KworkClient(**creds) + """ + return { + "token": self._token, + "cookies": self._cookies.copy() if self._cookies else None, + } + @classmethod async def login( cls,