Added a generic request method to the utilities
This commit is contained in:
parent
0682ad8838
commit
442e398f1b
|
@ -1,5 +1,6 @@
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import io
|
import io
|
||||||
|
import inspect
|
||||||
|
|
||||||
from . import config
|
from . import config
|
||||||
|
|
||||||
|
@ -61,6 +62,36 @@ async def download_image(url):
|
||||||
image = io.BytesIO(await r.read())
|
image = io.BytesIO(await r.read())
|
||||||
return image
|
return image
|
||||||
|
|
||||||
|
async def request(url, *, payload=None, method='GET', attr='json'):
|
||||||
|
"""Handles requesting to a URL"""
|
||||||
|
headers = {'User-Agent': config.user_agent}
|
||||||
|
|
||||||
|
# Attempt to connect up to our max retries
|
||||||
|
for x in range(5):
|
||||||
|
try:
|
||||||
|
with aiohttp.ClientSession(headers=headers) as session:
|
||||||
|
async with session.request(method=method, url=url, params=payload) as r:
|
||||||
|
# If we failed to connect, attempt again
|
||||||
|
|
||||||
|
if r.status != 200:
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Get the requested returned value
|
||||||
|
obj = getattr(r, attr)
|
||||||
|
# Try to call it (in case it's a method)
|
||||||
|
try:
|
||||||
|
obj = obj()
|
||||||
|
# If it's not a method, we just want the object
|
||||||
|
except TypeError:
|
||||||
|
obj = obj
|
||||||
|
# Now check if this is awaitable....if so, await it
|
||||||
|
if inspect.isawaitable(obj):
|
||||||
|
obj = await obj
|
||||||
|
# Then just return the requested object
|
||||||
|
return obj
|
||||||
|
except:
|
||||||
|
continue
|
||||||
|
|
||||||
async def update_records(key, winner, loser):
|
async def update_records(key, winner, loser):
|
||||||
# We're using the Harkness scale to rate
|
# We're using the Harkness scale to rate
|
||||||
# http://opnetchessclub.wikidot.com/harkness-rating-system
|
# http://opnetchessclub.wikidot.com/harkness-rating-system
|
||||||
|
|
Loading…
Reference in a new issue