Skip to content

utility

pokelance.ext.utility ⚓︎

Utility(client) ⚓︎

Bases: BaseExtension

Extension for utility related endpoints.

Attributes:

Name Type Description
cache Utility

The cache for this extension.

Initializes the extension.

Parameters:

Name Type Description Default
client HttpClient

The client to use for requests.

required
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.
    """
    self._client = client
    self._cache = self._client.cache
    self.cache = getattr(self._cache, self.__class__.__name__.lower())

fetch_api_metadata() async ⚓︎

Fetches the API metadata from the API.

Returns:

Type Description
APIMetadata

The API metadata.

Source code in pokelance/ext/utility.py
Python
async def fetch_api_metadata(self) -> APIMetadata:
    """Fetches the API metadata from the API.

    Returns
    -------
    pokelance.models.APIMetadata
        The API metadata.
    """
    route = Endpoint.get_api_metadata()
    data = await self._client.request(route)
    return self.cache.api_metadata.setdefault(route, APIMetadata.from_payload(data))

fetch_language(name) async ⚓︎

Fetches a language from the API.

Parameters:

Name Type Description Default
name Union[str, int]

The name or id of the language.

required

Returns:

Type Description
Language

The language.

Raises:

Type Description
ResourceNotFound

The name or id of the language is invalid.

Source code in pokelance/ext/utility.py
Python
async def fetch_language(self, name: t.Union[str, int]) -> Language:
    """Fetches a language from the API.

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

    Returns
    -------
    pokelance.models.Language
        The language.

    Raises
    ------
    pokelance.exceptions.ResourceNotFound
        The name or id of the language is invalid.
    """
    route = Endpoint.get_language(name)
    self._validate_resource(self.cache.language, name, route)
    data = await self._client.request(route)
    return self.cache.language.setdefault(route, Language.from_payload(data))

get_api_metadata() ⚓︎

Gets the API metadata from the cache.

Returns:

Type Description
Optional[APIMetadata]

The API metadata if it exists in the cache, else None.

Source code in pokelance/ext/utility.py
Python
def get_api_metadata(self) -> t.Optional[APIMetadata]:
    """Gets the API metadata from the cache.

    Returns
    -------
    typing.Optional[pokelance.models.APIMetadata]
        The API metadata if it exists in the cache, else None.
    """
    route = Endpoint.get_api_metadata()
    return self.cache.api_metadata.get(route, None)

get_language(name) ⚓︎

Gets a language from the cache.

Parameters:

Name Type Description Default
name Union[str, int]

The name or id of the language.

required

Returns:

Type Description
Optional[Language]

The language if it exists in the cache, else None.

Raises:

Type Description
ResourceNotFound

The name or id of the language is invalid.

Source code in pokelance/ext/utility.py
Python
def get_language(self, name: t.Union[str, int]) -> t.Optional[Language]:
    """Gets a language from the cache.

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

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

    Raises
    ------
    pokelance.exceptions.ResourceNotFound
        The name or id of the language is invalid.
    """
    route = Endpoint.get_language(name)
    self._validate_resource(self.cache.language, name, route)
    return self.cache.language.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_"):
            endpoint_name = f"get_{item[6:]}_endpoints"
            if not hasattr(Endpoint, endpoint_name):
                continue
            endpoint: t.Callable[[], "Route"] = getattr(Endpoint, endpoint_name)
            data = await self._client.request(endpoint())
            self._cache.load_documents(str(self.__class__.__name__), item[6:], data["results"])

setup(lance) ⚓︎

Sets up the utility extension.

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

Comments