diff --git a/anixart/auth/__init__.py b/anixart/auth/__init__.py index 4b16e94..0499d40 100644 --- a/anixart/auth/__init__.py +++ b/anixart/auth/__init__.py @@ -1 +1 @@ -from .account import AnixartAccount, AnixartAccountSaved, AnixartAccountToken +from .account import AnixartAccount, AnixartAccountSaved, AnixartAccountGuest diff --git a/anixart/auth/account.py b/anixart/auth/account.py index 467075e..42863c6 100644 --- a/anixart/auth/account.py +++ b/anixart/auth/account.py @@ -3,19 +3,31 @@ from pathlib import Path import requests +from anixart import endpoints from anixart.exceptions import AnixartInitError class AnixartAccount: + guest = False def __init__(self, username: str, password: str): self._username = username self._password = password if not isinstance(username, str) or not isinstance(password, str): raise AnixartInitError("Auth data must be strings.") + self._id = None self._token = None self._session = requests.Session() + self._api = None + + def _set_api(self, api): + self._api = api + + @property + def id(self): + return self._id + @property def username(self): return self._username @@ -43,7 +55,12 @@ class AnixartAccount: def login(self): """Login to Anixart and return the token.""" - # TODO: Implement login logic here + payload = {"login": self.username, "password": self._password} + res = self._api.post(endpoints.SING_IN, payload) + uid = res["profile"]["id"] + token = res["profileToken"]["token"] + self._id = uid + self._token = token def __str__(self): return f'AnixartAccount(login={self._username!r}, password={"*" * len(self._password)!r})' @@ -60,8 +77,7 @@ class AnixartAccountSaved(AnixartAccount): def save(self): """Save the account information to a file.""" data = { - "username": self._username, - "password": self._password, + "id": self._id, "token": self._token } with open(self._file, 'w') as f: @@ -73,11 +89,11 @@ class AnixartAccountSaved(AnixartAccount): raise AnixartInitError(f"Account file {self._file} does not exist.") with open(self._file, 'r') as f: data = json.load(f) + self._id = data.get("id") self._username = data.get("username") - self._password = data.get("password") self._token = data.get("token") - if not self._username or not self._password: - raise AnixartInitError("Login and password must be provided in the account file.") + if not self._id or not self._token: + raise AnixartInitError("id and token must be provided in the account file.") @classmethod def from_account(cls, account_file: str | Path, account: AnixartAccount): @@ -97,16 +113,14 @@ class AnixartAccountSaved(AnixartAccount): def __str__(self): return f'AnixartAccountSaved(account_file={self._file!r}")' -class AnixartAccountToken(AnixartAccount): +class AnixartAccountGuest(AnixartAccount): + guest = True + def __init__(self): + super().__init__("", "") + self._token = "" - def __init__(self, token): - super().__init__("mradx", "") # Пасхалка) - self._token = token - - def login(self): - """Login to Anixart and return information about the tokens.""" - # TODO: Implement login logic here - pass + def login(self): ... def __str__(self): - return f'AnixartAccountToken(token={self._token!r}")' + return f'AnixartAccountGuest(token={self._token!r}")' + diff --git a/anixart/auth/error_handler.py b/anixart/auth/error_handler.py new file mode 100644 index 0000000..83b45f8 --- /dev/null +++ b/anixart/auth/error_handler.py @@ -0,0 +1,12 @@ +from enum import IntEnum + + +class AnixartAuthErrors(IntEnum): + """ Error codes for AnixartApi authentication.""" + INCORRECT_LOGIN = 1 + INCORRECT_PASSWORD = 2 + + +def errors_handler(error): + """Handle errors and return a JSON response.""" + pass diff --git a/anixart/auth/handler.py b/anixart/auth/handler.py deleted file mode 100644 index c361c8a..0000000 --- a/anixart/auth/handler.py +++ /dev/null @@ -1,3 +0,0 @@ -def errors_handler(error): - """Handle errors and return a JSON response.""" - pass diff --git a/anixart/auth/objects.py b/anixart/auth/objects.py new file mode 100644 index 0000000..4945364 --- /dev/null +++ b/anixart/auth/objects.py @@ -0,0 +1,7 @@ +from dataclasses import dataclass + + +@dataclass +class ProfileToken: + id: int + token: str