1
0
Fork 0
mirror of synced 2024-05-20 20:42:27 +12:00

Added a generic request method to the utilities

This commit is contained in:
Phxntxm 2017-01-08 14:05:37 -06:00
parent 0682ad8838
commit 442e398f1b

View file

@ -1,5 +1,6 @@
import aiohttp
import io
import inspect
from . import config
@ -61,6 +62,36 @@ async def download_image(url):
image = io.BytesIO(await r.read())
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):
# We're using the Harkness scale to rate
# http://opnetchessclub.wikidot.com/harkness-rating-system