PokeLance
pokelance.client
⚓︎
PokeLance(*, audio_cache_size=128, image_cache_size=128, cache_size=100, logger=None, file_logging=False, cache_endpoints=True, session=None)
⚓︎
Main class to interact with the PokeAPI.
Attributes:
Name | Type | Description |
---|---|---|
_http |
HttpClient
|
The HTTP client used to make requests to the PokeAPI. |
_logger |
Union[Logger, Logger]
|
The logger used to log information about the client. |
_ext_tasks |
List[Tuple[Callable[[], Coroutine[Any, Any, None]], str]]
|
A list of coroutines to load extension data. |
cache_endpoints |
bool
|
Whether to cache endpoints. Defaults to True. |
EXTENSIONS |
Path
|
The path to the extensions directory. |
berry |
Berry
|
The berry extension. |
contest |
Contest
|
The contest extension. |
encounter |
Encounter
|
The encounter extension. |
evolution |
Evolution
|
The evolution extension. |
game |
Game
|
The game extension. |
item |
Item
|
The item extension. |
location |
Location
|
The location extension. |
machine |
Machine
|
The machine extension. |
move |
Move
|
The move extension. |
pokemon |
Pokemon
|
The pokemon extension. |
Examples:
>>> import pokelance
>>> import asyncio
>>> client = pokelance.PokeLance()
>>> async def main() -> None:
... print(await client.ping())
... await client.close()
>>> asyncio.run(main())
Parameters:
Name | Type | Description | Default |
---|---|---|---|
audio_cache_size |
int
|
The size of the audio cache. Defaults to 128. |
128
|
image_cache_size |
int
|
The size of the image cache. Defaults to 128. |
128
|
cache_size |
int
|
The size of the cache to use for the HTTP client. |
100
|
logger |
Optional[Logger]
|
The logger to use. If not provided, a new logger will be created. |
None
|
file_logging |
bool
|
Whether to log to a file. Defaults to False. |
False
|
session |
Optional[ClientSession]
|
The session to use for the HTTP client. It is recommended to use the default. |
None
|
Source code in pokelance/client.py
audio_cache_size: int
property
writable
⚓︎
ext_tasks: t.List[t.Tuple[t.Callable[[], t.Coroutine[t.Any, t.Any, None]], str]]
property
⚓︎
http: HttpClient
property
⚓︎
The HTTP client used to make requests to the PokeAPI.
Returns:
Type | Description |
---|---|
HttpClient
|
The HTTP client. |
image_cache_size: int
property
writable
⚓︎
logger: t.Union[logging.Logger, Logger]
property
⚓︎
add_extension(name, extension)
⚓︎
Adds an extension to the client. This is called automatically when an extension is loaded.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
The name of the extension. |
required |
extension |
BaseExtension
|
The extension to add. |
required |
Source code in pokelance/client.py
close()
async
⚓︎
Closes the client session. Recommended to use this when the client is no longer needed. Not needed if the client is used in a context manager.
Source code in pokelance/client.py
from_url(url)
async
⚓︎
Constructs a request from urls present in the data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url |
str
|
The URL to construct the request from. |
required |
Returns:
Type | Description |
---|---|
BaseType
|
The data from the URL. |
Raises:
Type | Description |
---|---|
ValueError
|
If the url is invalid. |
ResourceNotFound
|
If the data is not found. |
Source code in pokelance/client.py
get_audio_async(url)
async
⚓︎
get_image_async(url)
async
⚓︎
getch_data(ext, category, id_)
async
⚓︎
A getch method that looks up the cache for the data first then gets it from the API if it is not found.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ext |
Union[ExtensionEnum, ExtensionsL, str]
|
The extension to get the data from. |
required |
category |
str
|
The category to get the data from. |
required |
id_ |
Union[int, str]
|
The ID of the data to get. |
required |
Returns:
Type | Description |
---|---|
BaseType
|
The data. |
Raises:
Type | Description |
---|---|
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
Source code in pokelance/client.py
ping()
async
⚓︎
Pings the PokeAPI and returns the latency.
Returns:
Type | Description |
---|---|
float
|
The latency of the PokeAPI. |
setup_hook()
⚓︎
The setup hook to be called after the client is created. This is called automatically when the client is created. It is not recommended to call this manually.
Source code in pokelance/client.py
wait_until_ready()
async
⚓︎
Waits until the http client caches all the endpoints.
Source code in pokelance/client.py
Python | |
---|---|