From e27c3094d68b87d0d2ffdbdbec6f00543f315b8f Mon Sep 17 00:00:00 2001 From: brownsugar-bobamilktea <22185824+brownsugar-bobamilktea@users.noreply.github.com> Date: Fri, 17 Jan 2025 05:02:46 +0100 Subject: [PATCH] add param to json() because github uses text/plain for some reason --- commands/edit_entry/edit_entry_cmd.py | 2 +- commands/fetch_entry/fetch_entry_main.py | 7 ++++--- modules/async_utils.py | 5 +++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/commands/edit_entry/edit_entry_cmd.py b/commands/edit_entry/edit_entry_cmd.py index 7c0d995..ada964f 100644 --- a/commands/edit_entry/edit_entry_cmd.py +++ b/commands/edit_entry/edit_entry_cmd.py @@ -37,7 +37,7 @@ async def edit_entry_cmd( selected_entry_name = entry.name # entry always exists selected_field = field.value # field is mandatory here initial_value = await _fetch_entry_with_json( - interaction, selected_entry_id, selected_lang, selected_field, fromI18n=True + interaction, selected_entry_id, selected_lang, selected_field, fromI18n=True, expected_content_type=None ) form = submit_entry_modal.SubmitEntryModal( client, diff --git a/commands/fetch_entry/fetch_entry_main.py b/commands/fetch_entry/fetch_entry_main.py index 17860cb..99ee7bb 100644 --- a/commands/fetch_entry/fetch_entry_main.py +++ b/commands/fetch_entry/fetch_entry_main.py @@ -13,7 +13,7 @@ ) -async def get_json(how="url", json_url="") -> dict: +async def get_json(how="url", json_url="", expected_content_type='application/json') -> dict: """A multi-purpose function to fetch json. Right now, the only source is a json url, but in case things cahnge in the future, we can just modify this function. @@ -27,12 +27,12 @@ async def get_json(how="url", json_url="") -> dict: """ assert how in ("url",) if how == "url": - result = await async_utils._async_get_json(json_url) + result = await async_utils._async_get_json(json_url, expected_content_type) return result async def _fetch_entry_with_json( - interaction: discord.Interaction, entry: str, lang: str, field: str = None, fromI18n: bool = False + interaction: discord.Interaction, entry: str, lang: str, field: str = None, fromI18n: bool = False, expected_content_type='application/json' ): """Function to fetch entry based on json entries. This is called by both the cmd and ui version of fectch_entry, @@ -52,6 +52,7 @@ async def _fetch_entry_with_json( result_json = await get_json( how="url", json_url=link_to_fetch, + expected_content_type=expected_content_type ) # * if some error happens, notify user and stop if result_json is None: diff --git a/modules/async_utils.py b/modules/async_utils.py index e9bafe8..1c4c0bc 100644 --- a/modules/async_utils.py +++ b/modules/async_utils.py @@ -1,7 +1,7 @@ import aiohttp -async def _async_get_json(url: str) -> str: +async def _async_get_json(url: str, expected_content_type='application/json') -> str: """An async version of getting a JSON file. This is because discord doesn't like it when you use blocking IO (non async) for HTTP requests, @@ -10,13 +10,14 @@ async def _async_get_json(url: str) -> str: Args: url (str): _description_ + expected_content_type: passed to response.json(). Use None to disable content type checking. Returns: str: _description_ """ async with aiohttp.ClientSession() as session: async with session.get(url) as response: - return await response.json() + return await response.json(content_type=expected_content_type) async def _async_get_html(url: str) -> str: """An async version of getting a HTML file.