Skip to content

machine

pokelance.ext.machine ⚓︎

Machine(client) ⚓︎

Bases: BaseExtension

Extension for machine related endpoints.

Attributes:

Name Type Description
cache Machine

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_machine(id_) async ⚓︎

Fetches a machine from the API.

Parameters:

Name Type Description Default
id_ int

The id of the machine.

required

Returns:

Type Description
Machine

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

Raises:

Type Description
ResourceNotFound

If the machine does not exist in the API.

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> import asyncio
>>> client = PokeLance()
>>> async def main() -> None:
...     machine = await client.machine.fetch_machine(1)
...     print(machine.item.name)
...     await client.close()
>>> asyncio.run(main())
tm00
Source code in pokelance/ext/machine.py
Python
async def fetch_machine(self, id_: int) -> MachineModel:
    """Fetches a machine from the API.

    Parameters
    ----------
    id_: int
        The id of the machine.

    Returns
    -------
    MachineModel
        The machine if it exists in the API, else raises ResourceNotFound.

    Raises
    ------
    pokelance.exceptions.ResourceNotFound
        If the machine does not exist in the API.

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> import asyncio
    >>> client = PokeLance()
    >>> async def main() -> None:
    ...     machine = await client.machine.fetch_machine(1)
    ...     print(machine.item.name)
    ...     await client.close()
    >>> asyncio.run(main())
    tm00
    """
    route = Endpoint.get_machine(id_)
    self._validate_resource(self.cache.machine, id_, route)
    data = await self._client.request(route)
    return self.cache.machine.setdefault(route, MachineModel.from_payload(data))

get_machine(id_) ⚓︎

Gets a machine from the cache.

Parameters:

Name Type Description Default
id_ int

The id of the machine.

required

Returns:

Type Description
Optional[Machine]

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

Raises:

Type Description
ResourceNotFound

If the machine does not exist in the cache.

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> client = PokeLance()
>>> machine = client.machine.get_machine(1)
>>> machine.item.name
'tm00'
Source code in pokelance/ext/machine.py
Python
def get_machine(self, id_: int) -> t.Optional[MachineModel]:
    """Gets a machine from the cache.

    Parameters
    ----------
    id_: int
        The id of the machine.

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

    Raises
    ------
    pokelance.exceptions.ResourceNotFound
        If the machine does not exist in the cache.

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> client = PokeLance()
    >>> machine = client.machine.get_machine(1)
    >>> machine.item.name
    'tm00'
    """
    route = Endpoint.get_machine(id_)
    self._validate_resource(self.cache.machine, id_, route)
    return self.cache.machine.get(route, None)

get_message(case, data) staticmethod ⚓︎

Gets the error message for a resource not found error.

Parameters:

Name Type Description Default
case str

The case to use for the error message.

required
data Set[str]

The data to use for the error message.

required

Returns:

Type Description
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."

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 machine cog.

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