mirror of
https://github.com/SantaSpeen/anixart.git
synced 2025-07-01 23:47:00 +00:00
[+] Profile.from_response
This commit is contained in:
parent
bb852d2b3a
commit
9c94b68d55
@ -69,7 +69,7 @@ class AnixartAPI:
|
|||||||
|
|
||||||
return response
|
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:
|
if payload is None:
|
||||||
payload = {}
|
payload = {}
|
||||||
url = API_URL + method
|
url = API_URL + method
|
||||||
@ -94,7 +94,7 @@ class AnixartAPI:
|
|||||||
self._session.headers["Content-Length"] = ""
|
self._session.headers["Content-Length"] = ""
|
||||||
return self.__parse_response(res)
|
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:
|
if payload is None:
|
||||||
payload = {}
|
payload = {}
|
||||||
if payload.get("token") is None:
|
if payload.get("token") is None:
|
||||||
@ -110,12 +110,18 @@ class AnixartAPI:
|
|||||||
def execute(self, http_method, endpoint, **kwargs):
|
def execute(self, http_method, endpoint, **kwargs):
|
||||||
http_method = http_method.upper()
|
http_method = http_method.upper()
|
||||||
if http_method == "GET":
|
if http_method == "GET":
|
||||||
return self.get(endpoint, **kwargs)
|
return self._get(endpoint, **kwargs)
|
||||||
elif http_method == "POST":
|
elif http_method == "POST":
|
||||||
return self.post(endpoint, **kwargs)
|
return self._post(endpoint, **kwargs)
|
||||||
else:
|
else:
|
||||||
raise AnixartAPIRequestError("Allow only GET and POST requests.")
|
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):
|
def __str__(self):
|
||||||
return f'AnixartAPI(account={self.__account!r})'
|
return f'AnixartAPI(account={self.__account!r})'
|
||||||
|
|
||||||
|
@ -100,6 +100,11 @@ class Profile:
|
|||||||
|
|
||||||
is_my_profile: bool = False
|
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:
|
class _login:
|
||||||
pos_id: int
|
pos_id: int
|
||||||
id: int
|
id: int
|
||||||
|
@ -11,9 +11,8 @@ anix = AnixartAPI() # По умолчанию используется гост
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
try:
|
try:
|
||||||
raw = anix.execute("GET", endpoints.PROFILE.format(1))
|
raw = anix.get(endpoints.PROFILE, 1)
|
||||||
profile = {"is_my_profile": raw['is_my_profile'], **raw['profile']}
|
print(Profile.from_response(raw))
|
||||||
print(Profile(**profile))
|
|
||||||
except AnixartAPIRequestError as e:
|
except AnixartAPIRequestError as e:
|
||||||
print(e.message)
|
print(e.message)
|
||||||
print(e.code)
|
print(e.code)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user