import aiohttp, os, sys from colors import * import traceback from steam import ID as SteamID class BackendClient: up = False warn = False def __init__(self): if not os.getenv("BACKEND_URL", ""): error("BACKEND_URL not setted in env") sys.exit(10) if not os.getenv("SECRET_KEY", ""): error("SECRET_KEY not setted in env") sys.exit(11) self.secret_key = os.getenv("SECRET_KEY") self.pulse_url = f"{os.getenv('BACKEND_URL')}/api/pulse" self.vip_url = f"{os.getenv('BACKEND_URL')}/api/external/vip" async def pulse(self, exit_if_error = False): async with aiohttp.ClientSession(cookies={ "secretkey":self.secret_key}) as session: try: async with session.get(self.pulse_url, ssl=False) as response: await response.text() self.up = True except: error("Cannot create http session") if exit_if_error: error("exit if backend is down") sys.exit(200) self.up = False self.warn = False return self.up async def vip(self, steamid: SteamID, amount: int, extra: str, unique: str = ""): async with aiohttp.ClientSession(cookies={ "secretkey":self.secret_key}) as session: async with session.post(self.vip_url + f"?steam={steamid.id64}&amount={int(amount)}&service=steam&extra={extra}&unique={unique}", ssl=False) as response: try: result = int(await response.text()) if result == 0: warning(f"[S64:{steamid.id64}] VIP as not be added, maybe permition already exists") return 99 elif result > 0: info(f"[S64:{steamid.id64}] VIP has be added!") return 100 elif result < 0: info(f"[S64:{steamid.id64}] VIP has be extends!") return 101 except: error(f"[S64:{steamid.id64}] Backend returned error") traceback.print_exc() return False return False async def prices(self, exit_if_error = False): async with aiohttp.ClientSession() as session: try: async with session.get(self.vip_url) as response: return await response.json() except: traceback.print_exc() if exit_if_error: error("exit if cannot get prices") sys.exit(200)