A getch method that looks up the cache for the data first then gets it from the API if it is not found.
Parameters: |
-
ext
(Union[ExtensionEnum, ExtensionsL, str] )
–
The extension to get the data from.
-
category
(str )
–
The category to get the data from.
-
id_
(Union[int, str] )
–
The ID of the data to get.
|
Raises: |
-
ValueError
–
If the extension or category is invalid.
-
ResourceNotFound
–
If the data is not found.
|
Examples:
Python Console Session>>> import pokelance
>>> import asyncio
>>> from pokelance.constants import ExtensionEnum
>>> from pokelance.models import Pokemon
>>> client = pokelance.PokeLance()
>>> async def main() -> None:
... pokemon: Pokemon = await client.getch_data(ExtensionEnum.Pokemon, "pokemon", 1)
... print(pokemon.name)
... await client.close()
>>> asyncio.run(main())
bulbasaur
Source code in pokelance/client.py
Python |
---|
| async def getch_data(
self, ext: t.Union[ExtensionEnum, ExtensionsL, str], category: str, id_: t.Union[int, str]
) -> BaseType:
"""
A getch method that looks up the cache for the data first then gets it from the API if it is not found.
Parameters
----------
ext : Union[ExtensionEnum, ExtensionsL, str]
The extension to get the data from.
category : str
The category to get the data from.
id_ : Union[int, str]
The ID of the data to get.
Returns
-------
BaseType
The data.
Raises
------
ValueError
If the extension or category is invalid.
ResourceNotFound
If the data is not found.
Examples
--------
>>> import pokelance
>>> import asyncio
>>> from pokelance.constants import ExtensionEnum
>>> from pokelance.models import Pokemon
>>> client = pokelance.PokeLance()
>>> async def main() -> None:
... pokemon: Pokemon = await client.getch_data(ExtensionEnum.Pokemon, "pokemon", 1)
... print(pokemon.name)
... await client.close()
>>> asyncio.run(main())
bulbasaur
"""
if isinstance(ext, str) and (ext := str(ext).title()) not in ExtensionEnum.__members__:
raise ValueError(f"Invalid extension: {ext}")
categories = ExtensionEnum.get_categories(ext) if isinstance(ext, str) else ext.categories # type: ignore
if (category := category.lower().replace("_", "-")) not in categories:
raise ValueError(f"Invalid category: {category}, valid categories: {categories}")
category = category.replace("-", "_")
ext_ = getattr(self, ext.lower()) if isinstance(ext, str) else getattr(self, ext.name.lower())
get_, fetch_ = getattr(ext_, f"get_{category}"), getattr(ext_, f"fetch_{category}")
return t.cast(BaseType, get_(id_) or await fetch_(id_))
|