rework for api v2#76
Conversation
Words are stored and accessed differently, in order to make future changes in the API easier to manage: - Now using the V2 API instead of the deprecated V1 API. - Words now use the "Words" class instead of a typed dictionary. - Individual translations are now only downloaded from the API when needed, as the V2 API doesn't support requesting every translation at once. - The etymology section of `/nimi` has been modified slightly due to API changes. Only surface level bug testing has been done, there may still be some bugs introduced by these changes that I haven't yet found
| _FETCHED_LANGS: list[str] = [] | ||
|
|
||
| async def fetch_lang_and_defer(lang: str, ctx: discord.Interaction, ephemeral = False): | ||
| """Does the same thing as fetch_lang(), but defers an interaction if the translation has not already been downloaded. Use this in commands""" |
There was a problem hiding this comment.
why leave this choice up to the user?
you can have fetch_lang alone, and always defer if necessary
is there a circumstance in which the dev would want to fetch lang and not defer if necessary?
There was a problem hiding this comment.
Defer requires running inside of an async function, I didn't want fetch_lang to be async when async behaviour was not needed in many instances.
fetch_lang is used in get_word, get_non_sandbox_words, get_sandbox_words, get_words, word.get_commentary, word.get_definition, and word.get_etymology. I'd rather have these functions synchronous, it doesn't make sense to have them be async because of a feature of a function that these functions never use. So I separated out the async behaviour into a separate function.
Deferring every time fetch_lang is called would mean commands defer many times, and the interaction context would need to be passed down through many functions. It is simpler to just call fetch_lang_and_defer at the start of a command.
There was a problem hiding this comment.
- deferring is completely free, since discord will consume the deferral as soon as the command returns (which may be immediate)
- async is also completely free
- all of these commands potentially fetch data via an API anyway, so them being asynchronous is beneficial since other things can be done while waiting out the network fetch. and, well, most of Linku is async anyway because all operations on the discord api are async
| """Unsafe to call without previously having fetched the language using fetch_lang() or fetch_lang_and_defer()""" | ||
| return word if (word := get_word(word_str, lang)) and word.usage_category == "sandbox" else None | ||
|
|
||
| def get_word(word_str: str, lang: str = "en") -> Optional[Word]: |
There was a problem hiding this comment.
rather than have a safe/unsafe distinction, you can have a function like ensure_lang(lang) which is always run, and it fetches the lang + defers if necessary within itself
There was a problem hiding this comment.
These comments are outdated, get_word fetches the lang using fetch_lang, so it is safe. the ensure_lang function you're describing is the behaviour of fetch_lang_and_defer.
| @@ -0,0 +1,58 @@ | |||
| from typing import Required, Optional, TypedDict | |||
There was a problem hiding this comment.
instead of duplicating this work, use this tool to regenerate the sona data for v2, then use that as a reference
There was a problem hiding this comment.
This reverts commit bda56c7. Fixed in ijo commit.
No description provided.