pokelance.ext.game ⚓︎

Game(client) ⚓︎

Bases: BaseExtension

Extension for game related endpoints.

Attributes:
  • cache (Game) –

    The cache to use for caching resources.

Initializes the extension.

Parameters:
  • client (HttpClient) –

    The client to use for requests.

Returns:
Source code in pokelance/ext/_base.py
Python
def __init__(self, client: "HttpClient") -> None:
    """Initializes the extension.

    Parameters
    ----------
    client: pokelance.http.HttpClient
        The client to use for requests.

    Returns
    -------
    pokelance.ext.BaseExtension
        The extension.
    """
    self._client = client
    self._cache = self._client.cache
    self.cache = getattr(self._cache, self.__class__.__name__.lower())

fetch_generation(name) async ⚓︎

Fetches a generation from the API.

Parameters:
  • name (Union[str, int]) –

    The name or id of the generation.

Returns:
  • Generation

    The generation if it exists in the API, else raises ResourceNotFound.

Raises:

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> import asyncio
>>> client = PokeLance()
>>> async def main() -> None:
...     generation = await client.game.fetch_generation(1)
...     print(generation.id)
...     await client.close()
>>> asyncio.run(main())
1
Source code in pokelance/ext/game.py
Python
async def fetch_generation(self, name: t.Union[str, int]) -> Generation:
    """Fetches a generation from the API.

    Parameters
    ----------
    name: typing.Union[str, int]
        The name or id of the generation.

    Returns
    -------
    pokelance.models.Generation
        The generation if it exists in the API, else raises ResourceNotFound.

    Raises
    ------
    pokelance.exceptions.ResourceNotFound
        The name or id of the generation is invalid.

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> import asyncio
    >>> client = PokeLance()
    >>> async def main() -> None:
    ...     generation = await client.game.fetch_generation(1)
    ...     print(generation.id)
    ...     await client.close()
    >>> asyncio.run(main())
    1
    """
    route = Endpoint.get_generation(name)
    self._validate_resource(self.cache.generation, name, route)
    data = await self._client.request(route)
    return self.cache.generation.setdefault(route, Generation.from_payload(data))

fetch_pokedex(name) async ⚓︎

Fetches a pokedex from the API.

Parameters:
  • name (Union[str, int]) –

    The name or id of the pokedex.

Returns:
  • Pokedex

    The pokedex if it exists in the API, else raises ResourceNotFound.

Raises:

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> import asyncio
>>> client = PokeLance()
>>> async def main() -> None:
...     pokedex = await client.game.fetch_pokedex(1)
...     print(pokedex.region)
...     await client.close()
>>> asyncio.run(main())
None
Source code in pokelance/ext/game.py
Python
async def fetch_pokedex(self, name: t.Union[str, int]) -> Pokedex:
    """Fetches a pokedex from the API.

    Parameters
    ----------
    name: typing.Union[str, int]
        The name or id of the pokedex.

    Returns
    -------
    pokelance.models.Pokedex
        The pokedex if it exists in the API, else raises ResourceNotFound.

    Raises
    ------
    pokelance.exceptions.ResourceNotFound
        The name or id of the pokedex is invalid.

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> import asyncio
    >>> client = PokeLance()
    >>> async def main() -> None:
    ...     pokedex = await client.game.fetch_pokedex(1)
    ...     print(pokedex.region)
    ...     await client.close()
    >>> asyncio.run(main())
    None
    """
    route = Endpoint.get_pokedex(name)
    self._validate_resource(self.cache.pokedex, name, route)
    data = await self._client.request(route)
    return self.cache.pokedex.setdefault(route, Pokedex.from_payload(data))

fetch_version(name) async ⚓︎

Fetches a version from the API.

Parameters:
  • name (Union[str, int]) –

    The name or id of the version.

Returns:
  • Version

    The version if it exists in the API, else raises ResourceNotFound.

Raises:

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> import asyncio
>>> client = PokeLance()
>>> async def main() -> None:
...     version = await client.game.fetch_version(1)
...     print(version.name)
...     await client.close()
>>> asyncio.run(main())
red
Source code in pokelance/ext/game.py
Python
async def fetch_version(self, name: t.Union[str, int]) -> Version:
    """Fetches a version from the API.

    Parameters
    ----------
    name: typing.Union[str, int]
        The name or id of the version.

    Returns
    -------
    pokelance.models.Version
        The version if it exists in the API, else raises ResourceNotFound.

    Raises
    ------
    pokelance.exceptions.ResourceNotFound
        The name or id of the version is invalid.

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> import asyncio
    >>> client = PokeLance()
    >>> async def main() -> None:
    ...     version = await client.game.fetch_version(1)
    ...     print(version.name)
    ...     await client.close()
    >>> asyncio.run(main())
    red
    """
    route = Endpoint.get_version(name)
    self._validate_resource(self.cache.version, name, route)
    data = await self._client.request(route)
    return self.cache.version.setdefault(route, Version.from_payload(data))

fetch_version_group(name) async ⚓︎

Fetches a version group from the API.

Parameters:
  • name (Union[str, int]) –

    The name or id of the version group.

Returns:
  • VersionGroup

    The version group if it exists in the API, else raises ResourceNotFound.

Raises:

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> import asyncio
>>> client = PokeLance()
>>> async def main() -> None:
...     version_group = await client.game.fetch_version_group(1)
...     print(version_group.id)
...     await client.close()
>>> asyncio.run(main())
1
Source code in pokelance/ext/game.py
Python
async def fetch_version_group(self, name: t.Union[str, int]) -> VersionGroup:
    """Fetches a version group from the API.

    Parameters
    ----------
    name: typing.Union[str, int]
        The name or id of the version group.

    Returns
    -------
    pokelance.models.VersionGroup
        The version group if it exists in the API, else raises ResourceNotFound.

    Raises
    ------
    pokelance.exceptions.ResourceNotFound
        The name or id of the version group is invalid.

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> import asyncio
    >>> client = PokeLance()
    >>> async def main() -> None:
    ...     version_group = await client.game.fetch_version_group(1)
    ...     print(version_group.id)
    ...     await client.close()
    >>> asyncio.run(main())
    1
    """
    route = Endpoint.get_version_group(name)
    self._validate_resource(self.cache.version_group, name, route)
    data = await self._client.request(route)
    return self.cache.version_group.setdefault(route, VersionGroup.from_payload(data))

get_generation(name) ⚓︎

Gets a generation from the cache.

Parameters:
  • name (Union[str, int]) –

    The name or id of the generation.

Returns:
Raises:

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> client = PokeLance()
>>> generation = client.game.get_generation(1)
>>> generation.id
1
Source code in pokelance/ext/game.py
Python
def get_generation(self, name: t.Union[str, int]) -> t.Optional[Generation]:
    """Gets a generation from the cache.

    Parameters
    ----------
    name: typing.Union[str, int]
        The name or id of the generation.

    Returns
    -------
    typing.Optional[pokelance.models.Generation]
        The generation if it exists in the cache, else None.

    Raises
    ------
    pokelance.exceptions.ResourceNotFound
        The name or id of the generation is invalid.

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> client = PokeLance()
    >>> generation = client.game.get_generation(1)
    >>> generation.id
    1
    """
    route = Endpoint.get_generation(name)
    self._validate_resource(self.cache.generation, name, route)
    return self.cache.generation.get(route, None)

get_message(case, data) staticmethod ⚓︎

Gets the error message for a resource not found error.

Parameters:
  • case (str) –

    The case to use for the error message.

  • data (Set[str]) –

    The data to use for the error message.

Returns:
  • str

    The error message.

Source code in pokelance/ext/_base.py
Python
@staticmethod
def get_message(case: str, data: t.Set[str]) -> str:
    """Gets the error message for a resource not found error.

    Parameters
    ----------
    case: str
        The case to use for the error message.
    data: typing.Set[str]
        The data to use for the error message.

    Returns
    -------
    str
        The error message.
    """
    matches = get_close_matches(case, data, n=10, cutoff=0.5)
    if matches:
        return f"Resource not found. Did you mean {', '.join(matches)}?"
    return "Resource not found."

get_pokedex(name) ⚓︎

Gets a pokedex from the cache.

Parameters:
  • name (Union[str, int]) –

    The name or id of the pokedex.

Returns:
Raises:

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> client = PokeLance()
>>> pokedex = client.game.get_pokedex(1)
>>> pokedex.region
None
Source code in pokelance/ext/game.py
Python
def get_pokedex(self, name: t.Union[str, int]) -> t.Optional[Pokedex]:
    """Gets a pokedex from the cache.

    Parameters
    ----------
    name: typing.Union[str, int]
        The name or id of the pokedex.

    Returns
    -------
    typing.Optional[pokelance.models.Pokedex]
        The pokedex if it exists in the cache, else None.

    Raises
    ------
    pokelance.exceptions.ResourceNotFound
        The name or id of the pokedex is invalid.

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> client = PokeLance()
    >>> pokedex = client.game.get_pokedex(1)
    >>> pokedex.region
    None
    """
    route = Endpoint.get_pokedex(name)
    self._validate_resource(self.cache.pokedex, name, route)
    return self.cache.pokedex.get(route, None)

get_version(name) ⚓︎

Gets a version from the cache.

Parameters:
  • name (Union[str, int]) –

    The name or id of the version.

Returns:
Raises:

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> client = PokeLance()
>>> version = client.game.get_version(1)
>>> version.name
'red'
Source code in pokelance/ext/game.py
Python
def get_version(self, name: t.Union[str, int]) -> t.Optional[Version]:
    """Gets a version from the cache.

    Parameters
    ----------
    name: typing.Union[str, int]
        The name or id of the version.

    Returns
    -------
    typing.Optional[pokelance.models.Version]
        The version if it exists in the cache, else None.

    Raises
    ------
    pokelance.exceptions.ResourceNotFound
        The name or id of the version is invalid.

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> client = PokeLance()
    >>> version = client.game.get_version(1)
    >>> version.name
    'red'
    """
    route = Endpoint.get_version(name)
    self._validate_resource(self.cache.version, name, route)
    return self.cache.version.get(route, None)

get_version_group(name) ⚓︎

Gets a version group from the cache.

Parameters:
  • name (Union[str, int]) –

    The name or id of the version group.

Returns:
Raises:

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> client = PokeLance()
>>> version_group = client.game.get_version_group(1)
>>> version_group.id
1
Source code in pokelance/ext/game.py
Python
def get_version_group(self, name: t.Union[str, int]) -> t.Optional[VersionGroup]:
    """Gets a version group from the cache.

    Parameters
    ----------
    name: typing.Union[str, int]
        The name or id of the version group.

    Returns
    -------
    typing.Optional[pokelance.models.VersionGroup]
        The version group if it exists in the cache, else None.

    Raises
    ------
    pokelance.exceptions.ResourceNotFound
        The name or id of the version group is invalid.

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> client = PokeLance()
    >>> version_group = client.game.get_version_group(1)
    >>> version_group.id
    1
    """
    route = Endpoint.get_version_group(name)
    self._validate_resource(self.cache.version_group, name, route)
    return self.cache.version_group.get(route, None)

setup() async ⚓︎

Sets up the extension.

Source code in pokelance/ext/_base.py
Python
async def setup(self) -> None:
    """Sets up the extension."""
    for item in dir(self):
        if item.startswith("fetch_"):
            data = await self._client.request(
                t.cast(t.Callable[[], "Route"], getattr(Endpoint, f"get_{item[6:]}_endpoints"))()
            )
            self._cache.load_documents(str(self.__class__.__name__), item[6:], data["results"])

setup(lance) ⚓︎

Sets up the game cog.

Source code in pokelance/ext/game.py
Python
def setup(lance: "PokeLance") -> None:
    """Sets up the game cog."""
    lance.add_extension("game", Game(lance.http))