1
0
Fork 0
mirror of synced 2024-05-18 19:42:28 +12:00

Allow the use of forcing content_type for json

This commit is contained in:
phxntxm 2017-07-10 00:20:30 -05:00
parent 28e912728b
commit 46a1280256
2 changed files with 12 additions and 5 deletions

View file

@ -30,7 +30,7 @@ class Stats:
# We need to page through, so lets create a loop and break when we find out we're done
while True:
# Simply get data based on the URL
data = await utils.request(url, headers=headers)
data = await utils.request(url, headers=headers, force_content_type_json=True)
# First check if the data failed to retrieve, if so just return
if data is None:
return
@ -40,11 +40,12 @@ class Stats:
# We only carry about the user's
if include['type'] != 'user':
continue
# This check checks the user's connected campaign (should only exist for *our* user) and checks if it matches
# This check checks the user's connected campaign (should only exist for *our* user) and checks if it
# matches
if include.get('relationshipos', {}).get('campaign', {}).get('data', {}).get('id', {}) == str(utils.patreon_id):
continue
# Otherwuse the only way this user was included, was if they are a patron, so include them
# Otherwise the only way this user was included, was if they are a patron, so include them
name = include['attributes']['full_name']
if name:
names.append(name)

View file

@ -60,7 +60,7 @@ async def download_image(url):
return image
async def request(url, *, headers=None, payload=None, method='GET', attr='json'):
async def request(url, *, headers=None, payload=None, method='GET', attr='json', force_content_type_json=False):
# Make sure our User Agent is what's set, and ensure it's sent even if no headers are passed
if headers is None:
headers = {}
@ -83,7 +83,13 @@ async def request(url, *, headers=None, payload=None, method='GET', attr='json')
return_value = getattr(response, attr)
# Next check if this can be called
if callable(return_value):
return_value = return_value()
# This is use for json; it checks the mimetype instead of checking if the actual data
# This causes some places with different mimetypes to fail, even if it's valid json
# This check allows us to force the content_type to use whatever content type is given
if force_content_type_json:
return_value = return_value(content_type=response.headers['content-type'])
else:
return_value = return_value()
# If this is awaitable, await it
if inspect.isawaitable(return_value):
return_value = await return_value