diff --git a/anixart/api.py b/anixart/api.py index 5e5a200..7b2ca26 100644 --- a/anixart/api.py +++ b/anixart/api.py @@ -69,7 +69,7 @@ class AnixartAPI: return response - def post(self, method: str, payload: dict = None, is_json: bool = False, **kwargs): + def _post(self, method: str, payload: dict = None, is_json: bool = False, **kwargs): if payload is None: payload = {} url = API_URL + method @@ -94,7 +94,7 @@ class AnixartAPI: self._session.headers["Content-Length"] = "" return self.__parse_response(res) - def get(self, method: str, payload: dict = None, **kwargs): + def _get(self, method: str, payload: dict = None, **kwargs): if payload is None: payload = {} if payload.get("token") is None: @@ -110,12 +110,18 @@ class AnixartAPI: def execute(self, http_method, endpoint, **kwargs): http_method = http_method.upper() if http_method == "GET": - return self.get(endpoint, **kwargs) + return self._get(endpoint, **kwargs) elif http_method == "POST": - return self.post(endpoint, **kwargs) + return self._post(endpoint, **kwargs) else: raise AnixartAPIRequestError("Allow only GET and POST requests.") + def get(self, endpoint, *args, **kwargs): + return self.execute("GET", endpoint.format(*args), **kwargs) + + def post(self, endpoint, *args, **kwargs): + return self.execute("POST", endpoint.format(*args), **kwargs) + def __str__(self): return f'AnixartAPI(account={self.__account!r})' diff --git a/anixart/profile/objects.py b/anixart/profile/objects.py index 6877a66..3bb82db 100644 --- a/anixart/profile/objects.py +++ b/anixart/profile/objects.py @@ -100,6 +100,11 @@ class Profile: is_my_profile: bool = False + @classmethod + def from_response(cls, response: dict) -> "Profile": + profile = {"is_my_profile": response['is_my_profile'], **response['profile']} + return cls(**profile) + class _login: pos_id: int id: int diff --git a/examples/auth.py b/examples/auth.py index 4c2c488..5a09adf 100644 --- a/examples/auth.py +++ b/examples/auth.py @@ -11,9 +11,8 @@ anix = AnixartAPI() # По умолчанию используется гост if __name__ == '__main__': try: - raw = anix.execute("GET", endpoints.PROFILE.format(1)) - profile = {"is_my_profile": raw['is_my_profile'], **raw['profile']} - print(Profile(**profile)) + raw = anix.get(endpoints.PROFILE, 1) + print(Profile.from_response(raw)) except AnixartAPIRequestError as e: print(e.message) print(e.code)