[+] Profile.from_response

This commit is contained in:
Maxim Khomutov 2025-04-02 18:07:54 +03:00
parent bb852d2b3a
commit 9c94b68d55
3 changed files with 17 additions and 7 deletions

View File

@ -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})'

View File

@ -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

View File

@ -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)