def __init__(self, rooturl, loop, maxtasks=100): self.rooturl = rooturl self.loop = loop self.todo = set() self.busy = set() self.done = {} self.tasks = set() self.sem = asyncio.Semaphore(maxtasks) # session stores cookies between requests and uses connection pool self.session = aiohttp.Session()
def __init__(self, hosts, *, method=None, path=None, ssl=False, session=False, timeout=None, conn_timeout=None, failed_timeout=5.0, resolve=True, loop=None): super().__init__() if isinstance(hosts, str): hosts = (hosts,) if not hosts: raise ValueError('Hosts are required') self._hosts = [] for host in hosts: if isinstance(host, str): if ':' in host: host, port = host.split(':') try: port = int(port) except: raise ValueError('Port has to be integer: %s' % host) else: port = 80 else: host, port = host self._hosts.append((host, port)) self._method = method self._path = path self._ssl = ssl self._timeout = timeout self._conn_timeout = conn_timeout self._schema = 'https' if ssl else 'http' self._session = aiohttp.Session() if session else None self._failed = collections.deque() self._failed_handle = None self._failed_timeout = failed_timeout if loop is None: loop = asyncio.get_event_loop() self._loop = loop self._resolve = resolve self._resolved_hosts = {} if resolve: self._resolve_handle = self._loop.call_later( self._resolve_timeout, self._cleanup_resolved_host)
async def cache_discord(url, bot_type, key=None, custom_token=None, default={}, cachetime=120): if not bot_type in config['tokens']: return False, default if not custom_token: token = config['tokens'].get(bot_type) else: token = custom_token key = token['access_token'] if not key: key = url url = f"{_oauth2['api_base']}/v6/{url}" _t = time.time() if not key in discord_cache or _t - discord_cache.get(key)['s'] > 0: try: rockutils.prefix_print(f"Retrieving {url}", prefix="Cacher") async with aiohttp.Session() as _session: async with _session.get( url, headers={"Authorization": f"Bot {token}"}) as r: data = await r.json() if type(data) == dict and data.get("code"): rockutils.prefix_print( f"Encountered bad response: {data}", prefix="Cacher") discord_cache[key] = { "d": default, "s": _t + cachetime } return False, data.get("code", -1) discord_cache[key] = {"d": data, "s": _t + cachetime} except Exception as e: print(f"[cache_discord] {e}") return False, [] return True, discord_cache[key]['d']
def __init__(self, key, endpoint, arguments={}): self.key = key self.session = aiohttp.Session(headers={'x-api-key': self.key}) self.endpoint = endpoint self.arguments = arguments
def __init__(self, session=None, loop=None port=3030, host="localhost", password=None): """ Params: session: (aiohttp.Session) Session to perform requests with. loop: (asyncio.Loop) Asyncio loop to use when creating the session. port: (int) Port to the API server. host: (str) Host of ths API server. password: (str) Password if set. Loop is only used if session is None as it will be used to create the session. If you provide your own session then loop is not needed. Password is only required when requesting with a host that is not localhost and one is set in the server. """ self.is_async = is_async self.host = host self.session = session if session or aiohttp.Session(loop=loop or asyncio.get_event_loop()) self.port = port self.password = password async def _get(self, endpoint, params=None): headers = None if self.password: headers = { "Authorization": self.password } response = await self.session.get(f"http://{self.host}:{self.port}{endpoint}", params=params, headers=headers) if response.status != 200: if "application/json" in response.headers.get("content-type", ""): raise Exception((await response.json())["message"])