From a6431ca4c3a91541a7c6c632768dff4205c8e5b8 Mon Sep 17 00:00:00 2001 From: Myned Date: Wed, 18 Mar 2020 02:17:58 -0400 Subject: [PATCH] Add POST requests to fetch method for Kheina API --- src/utils/utils.py | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/src/utils/utils.py b/src/utils/utils.py index 10ef267..f9ab5ea 100644 --- a/src/utils/utils.py +++ b/src/utils/utils.py @@ -84,19 +84,36 @@ last_commands = {} asession = aiohttp.ClientSession() -async def fetch(url, *, json=False, response=False, text=False): +async def fetch(url, *, post={}, response=False, text=False, json=False): if '.json' in url and ('e621' in url or 'e926' in url): url += f'&login=BotMyned&api_key={config["e621_api"]}' - async with asession.get(url, headers={ - 'User-Agent': 'Myned/Modufur (https://github.com/Myned/Modufur)'}, ssl=False) as r: - if response: - return r - elif json: - return await r.json() - elif text: - return await r.text() - else: - return await r.read() + + if post: + async with asession.post(url, data=post, headers={ + 'User-Agent': 'Myned/Modufur (https://github.com/Myned/Modufur)'}, ssl=False) as r: + if r.status != 200: + return r.status + elif response: + return r + elif text: + return await r.text() + elif json: + return await r.json() + else: + return await r.read() + else: + async with asession.get(url, headers={ + 'User-Agent': 'Myned/Modufur (https://github.com/Myned/Modufur)'}, ssl=False) as r: + if r.status != 200: + return r.status + elif response: + return r + elif text: + return await r.text() + elif json: + return await r.json() + else: + return await r.read() def generate_embed(ctx, *, title=d.Embed.Empty, kind='rich', description=d.Embed.Empty, url=d.Embed.Empty, timestamp=d.Embed.Empty, colour=color, footer={}, image=d.Embed.Empty, thumbnail=d.Embed.Empty, author={}, fields=[]):