Skip to content

Commit

Permalink
add param to json() because github uses text/plain for some reason
Browse files Browse the repository at this point in the history
  • Loading branch information
brownsugar-bobamilktea committed Jan 17, 2025
1 parent d1e0327 commit e27c309
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion commands/edit_entry/edit_entry_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions commands/fetch_entry/fetch_entry_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand All @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions modules/async_utils.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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.
Expand Down

0 comments on commit e27c309

Please sign in to comment.