| 11 | from bot.utils.logger import SessionLogger |
| 12 | |
| 13 | class BlumApi: |
| 14 | gateway_url = "https://gateway.blum.codes" |
| 15 | wallet_url = "https://wallet-domain.blum.codes" |
| 16 | subscription_url = "https://subscription.blum.codes" |
| 17 | |
| 18 | game_url = "https://game-domain.blum.codes" |
| 19 | earn_domain = "https://earn-domain.blum.codes" |
| 20 | user_url = "https://user-domain.blum.codes" |
| 21 | tribe_url = "https://tribe-domain.blum.codes" |
| 22 | |
| 23 | _session: ClientSession |
| 24 | _log: SessionLogger |
| 25 | _refresh_token: str | None |
| 26 | |
| 27 | def __init__(self, session: ClientSession, logger: SessionLogger): |
| 28 | self._log = SessionLogger("API | " + logger.session_name) |
| 29 | self._session = session |
| 30 | |
| 31 | @staticmethod |
| 32 | def error_wrapper(method): |
| 33 | async def wrapper(self, *arg, **kwargs): |
| 34 | try: |
| 35 | return await method(self, *arg, **kwargs) |
| 36 | except NeedRefreshTokenError: |
| 37 | await self.refresh_tokens() |
| 38 | return await method(self, *arg, **kwargs) |
| 39 | except NeedReLoginError: |
| 40 | raise NeedReLoginError |
| 41 | except Exception as e: |
| 42 | self._log.error(f"Error on BlumApi.{method.__name__} | {type(e).__name__}: {e}") |
| 43 | return wrapper |
| 44 | |
| 45 | async def get(self, url: str): |
| 46 | option_headers = {"access-control-request-method": "GET", **self._session.headers} |
| 47 | response = await self._session.options(url=url, headers=option_headers) |
| 48 | self._log.trace(f"[{response.status}] OPTIONS GET: {url}") |
| 49 | response = await self._session.get(url=url) |
| 50 | if response.status == 401: |
| 51 | raise NeedRefreshTokenError() |
| 52 | self._log.trace(f"[{response.status}] GET: {url}") |
| 53 | return response |
| 54 | |
| 55 | async def post(self, url: str, data: dict = None): |
| 56 | option_headers = {"access-control-request-method": "POST", **self._session.headers} |
| 57 | response = await self._session.options(url=url, headers=option_headers) |
| 58 | self._log.trace(f"[{response.status}] OPTIONS POST: {url}") |
| 59 | response = await self._session.post(url=url, json=data) |
| 60 | if response.status == 401: |
| 61 | raise NeedRefreshTokenError() |
| 62 | self._log.trace(f"[{response.status}] POST: {url}") |
| 63 | return response |
| 64 | |
| 65 | async def auth_with_web_data(self, web_data) -> dict: |
| 66 | resp = await self.post(url=f"{self.user_url}/api/v1/auth/provider/PROVIDER_TELEGRAM_MINI_APP", data=web_data) |
| 67 | resp_json = await resp.json() |
| 68 | if resp.status == 200: |
| 69 | return resp_json |
| 70 | if resp.status == 520: |