mirror of
https://github.com/SantaSpeen/anixart.git
synced 2026-10-26 22:09:59 +00:00
[+] Fixes for post
This commit is contained in:
1 parent
aee5c542b4
commit
8a944319d6
2 files changed
+24
-39
No files matched your search
+23
-37
@@ -4,19 +4,17 @@ import requests
|
|||||||
from .__meta__ import __version__, __build__
|
from .__meta__ import __version__, __build__
|
||||||
from .auth import AnixartAccount, AnixartAccountGuest
|
from .auth import AnixartAccount, AnixartAccountGuest
|
||||||
from .enums import AnixartApiErrors
|
from .enums import AnixartApiErrors
|
||||||
from .endpoints import API_URL
|
|
||||||
from .exceptions import AnixartAPIRequestError, AnixartAPIError
|
from .exceptions import AnixartAPIRequestError, AnixartAPIError
|
||||||
from .exceptions import AnixartInitError
|
from .exceptions import AnixartInitError
|
||||||
|
|
||||||
|
debug = True
|
||||||
|
|
||||||
class AnixartAPI:
|
class AnixartAPI:
|
||||||
|
API_URL = "https://api.anixart.tv/"
|
||||||
|
|
||||||
def __init__(self, account: AnixartAccount = None):
|
def __init__(self, account: AnixartAccount = None):
|
||||||
if account is None:
|
if account is None:
|
||||||
account = AnixartAccountGuest()
|
account = AnixartAccountGuest()
|
||||||
if not isinstance(account, AnixartAccount):
|
|
||||||
raise AnixartInitError(f'Use class "AnixartAccount" for user. But {type(account)} given.')
|
|
||||||
|
|
||||||
self.use_account(account)
|
self.use_account(account)
|
||||||
|
|
||||||
def use_account(self, account: AnixartAccount):
|
def use_account(self, account: AnixartAccount):
|
||||||
@@ -37,6 +35,9 @@ class AnixartAPI:
|
|||||||
return self.__account
|
return self.__account
|
||||||
|
|
||||||
def __parse_response(self, res: requests.Response):
|
def __parse_response(self, res: requests.Response):
|
||||||
|
if debug:
|
||||||
|
print(f"[D] -> {res.request.method} body='{res.request.body!s}' url='{res.url!s}'")
|
||||||
|
print(f"[D] <- {res.status_code}, {len(res.text)=}")
|
||||||
if res.status_code != 200:
|
if res.status_code != 200:
|
||||||
e = AnixartAPIRequestError("Bad Request: Invalid request parameters.")
|
e = AnixartAPIRequestError("Bad Request: Invalid request parameters.")
|
||||||
e.message = (
|
e.message = (
|
||||||
@@ -56,7 +57,8 @@ class AnixartAPI:
|
|||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
raise AnixartAPIError("Failed to parse JSON response")
|
raise AnixartAPIError("Failed to parse JSON response")
|
||||||
|
|
||||||
# print(response)
|
if debug:
|
||||||
|
print(response)
|
||||||
if response['code'] != 0:
|
if response['code'] != 0:
|
||||||
code = response['code']
|
code = response['code']
|
||||||
if code in AnixartApiErrors:
|
if code in AnixartApiErrors:
|
||||||
@@ -69,42 +71,26 @@ class AnixartAPI:
|
|||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
def _post(self, method: str, payload: dict = None, is_json: bool = False, **kwargs):
|
def _post(self, method: str, _json: bool = False, **kwargs):
|
||||||
if payload is None:
|
url = self.API_URL + method
|
||||||
payload = {}
|
kwargs["token"] = kwargs.get("token", self.__token)
|
||||||
url = API_URL + method
|
if kwargs["token"]:
|
||||||
if payload.get("token") is None:
|
url += f"?token={self.__token}"
|
||||||
if self.__token is not None:
|
|
||||||
payload.update({"token": self.__token})
|
req_settings = {"url": url}
|
||||||
url += "?token=" + self.__token
|
if _json:
|
||||||
else:
|
|
||||||
token = kwargs.get("token")
|
|
||||||
if token is not None:
|
|
||||||
payload.update({"token": token})
|
|
||||||
url += "?token=" + token
|
|
||||||
kwargs = {"url": url}
|
|
||||||
if is_json:
|
|
||||||
self._session.headers["Content-Type"] = "application/json; charset=UTF-8"
|
self._session.headers["Content-Type"] = "application/json; charset=UTF-8"
|
||||||
self._session.headers["Content-Length"] = str(len(payload))
|
req_settings.update({"json": kwargs})
|
||||||
kwargs.update({"json": payload})
|
|
||||||
else:
|
else:
|
||||||
kwargs.update({"data": payload})
|
req_settings.update({"data": kwargs})
|
||||||
res = self._session.post(**kwargs)
|
res = self._session.post(**req_settings)
|
||||||
self._session.headers["Content-Type"] = ""
|
|
||||||
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, **kwargs):
|
||||||
if payload is None:
|
if self.__token:
|
||||||
payload = {}
|
kwargs["token"] = kwargs.get("token", self.__token)
|
||||||
if payload.get("token") is None:
|
|
||||||
if self.__token is not None:
|
res = self._session.get(self.API_URL + method, params=kwargs)
|
||||||
payload.update({"token": self.__token})
|
|
||||||
else:
|
|
||||||
token = kwargs.get("token")
|
|
||||||
if token is not None:
|
|
||||||
payload.update({"token": token})
|
|
||||||
res = self._session.get(API_URL + method, params=payload)
|
|
||||||
return self.__parse_response(res)
|
return self.__parse_response(res)
|
||||||
|
|
||||||
def execute(self, http_method, endpoint, **kwargs):
|
def execute(self, http_method, endpoint, **kwargs):
|
||||||
|
|||||||
@@ -55,8 +55,7 @@ class AnixartAccount:
|
|||||||
|
|
||||||
def login(self):
|
def login(self):
|
||||||
"""Login to Anixart and return the token."""
|
"""Login to Anixart and return the token."""
|
||||||
payload = {"login": self.username, "password": self._password}
|
res = self._api.post(endpoints.SING_IN, login=self.username, password=self._password)
|
||||||
res = self._api.post(endpoints.SING_IN, payload)
|
|
||||||
uid = res["profile"]["id"]
|
uid = res["profile"]["id"]
|
||||||
token = res["profileToken"]["token"]
|
token = res["profileToken"]["token"]
|
||||||
self._id = uid
|
self._id = uid
|
||||||
|
|||||||
Reference in new issue
Block a user