-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.py
41 lines (26 loc) · 1.06 KB
/
http.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import asyncio
import aiohttp
from utils import cache
# Removes the aiohttp ClientSession instance warning.
class HTTPSession(aiohttp.ClientSession):
""" Abstract class for aiohttp. """
def __init__(self, loop=None):
super().__init__(loop=loop or asyncio.get_event_loop())
def __del__(self):
"""
Closes the ClientSession instance
cleanly when the instance is deleted.
Useful for things like when the interpreter closes.
This would be perfect if discord.py had this as well. :thinking:
"""
if not self.closed:
self.close()
session = HTTPSession()
@cache.async_cache()
async def query(url, method="get", res_method="text", *args, **kwargs):
async with getattr(session, method.lower())(url, *args, **kwargs) as res:
return await getattr(res, res_method)()
async def get(url, *args, **kwargs):
return await query(url, "get", *args, **kwargs)
async def post(url, *args, **kwargs):
return await query(url, "post", *args, **kwargs)