pokelance.ext.item ⚓︎

Item(client) ⚓︎

Bases: BaseExtension

Extension for item related endpoints.

Attributes:
  • cache (Item) –

    The cache for this extension.

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_item(name) async ⚓︎

Fetches an item from the API.

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

    The name or id of the item.

Returns:
  • Item

    The item 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:
...     item = await client.item.fetch_item("potion")
...     print(item.id)
...     await client.close()
>>> asyncio.run(main())
17
Source code in pokelance/ext/item.py
Python
async def fetch_item(self, name: t.Union[str, int]) -> ItemModel:
    """Fetches an item from the API.

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

    Returns
    -------
    ItemModel
        The item if it exists in the API, else raises ResourceNotFound.

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

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> import asyncio
    >>> client = PokeLance()
    >>> async def main() -> None:
    ...     item = await client.item.fetch_item("potion")
    ...     print(item.id)
    ...     await client.close()
    >>> asyncio.run(main())
    17
    """
    route = Endpoint.get_item(name)
    self._validate_resource(self.cache.item, name, route)
    data = await self._client.request(route)
    return self.cache.item.setdefault(route, ItemModel.from_payload(data))

fetch_item_attribute(name) async ⚓︎

Fetches an item attribute from the API.

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

    The name or id of the item attribute.

Returns:
  • ItemAttribute

    The item attribute 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:
...     item_attribute = await client.item.fetch_item_attribute("holdable")
...     print(item_attribute.id)
...     await client.close()
>>> asyncio.run(main())
5
Source code in pokelance/ext/item.py
Python
async def fetch_item_attribute(self, name: t.Union[str, int]) -> ItemAttribute:
    """Fetches an item attribute from the API.

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

    Returns
    -------
    ItemAttribute
        The item attribute if it exists in the API, else raises ResourceNotFound.

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

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> import asyncio
    >>> client = PokeLance()
    >>> async def main() -> None:
    ...     item_attribute = await client.item.fetch_item_attribute("holdable")
    ...     print(item_attribute.id)
    ...     await client.close()
    >>> asyncio.run(main())
    5
    """
    route = Endpoint.get_item_attribute(name)
    self._validate_resource(self.cache.item_attribute, name, route)
    data = await self._client.request(route)
    return self.cache.item_attribute.setdefault(route, ItemAttribute.from_payload(data))

fetch_item_category(name) async ⚓︎

Fetches an item category from the API.

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

    The name or id of the item category.

Returns:
  • ItemCategory

    The item category 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:
...     item_category = await client.item.fetch_item_category(1)
...     print(item_category.name)
...     await client.close()
>>> asyncio.run(main())
stat-boosts
Source code in pokelance/ext/item.py
Python
async def fetch_item_category(self, name: t.Union[str, int]) -> ItemCategory:
    """Fetches an item category from the API.

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

    Returns
    -------
    ItemCategory
        The item category if it exists in the API, else raises ResourceNotFound.

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

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> import asyncio
    >>> client = PokeLance()
    >>> async def main() -> None:
    ...     item_category = await client.item.fetch_item_category(1)
    ...     print(item_category.name)
    ...     await client.close()
    >>> asyncio.run(main())
    stat-boosts
    """
    route = Endpoint.get_item_category(name)
    self._validate_resource(self.cache.item_category, name, route)
    data = await self._client.request(route)
    return self.cache.item_category.setdefault(route, ItemCategory.from_payload(data))

fetch_item_fling_effect(name) async ⚓︎

Fetches an item fling effect from the API.

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

    The name or id of the item fling effect.

Returns:
  • ItemFlingEffect

    The item fling effect 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:
...     item_fling_effect = await client.item.fetch_item_fling_effect(1)
...     print(item_fling_effect.name)
...     await client.close()
>>> asyncio.run(main())
badly-poison
Source code in pokelance/ext/item.py
Python
async def fetch_item_fling_effect(self, name: t.Union[str, int]) -> ItemFlingEffect:
    """Fetches an item fling effect from the API.

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

    Returns
    -------
    ItemFlingEffect
        The item fling effect if it exists in the API, else raises ResourceNotFound.

    Raises
    ------
    pokelance.exceptions.ResourceNotFound
        The name or id of the item fling effect is invalid.

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> import asyncio
    >>> client = PokeLance()
    >>> async def main() -> None:
    ...     item_fling_effect = await client.item.fetch_item_fling_effect(1)
    ...     print(item_fling_effect.name)
    ...     await client.close()
    >>> asyncio.run(main())
    badly-poison
    """
    route = Endpoint.get_item_fling_effect(name)
    self._validate_resource(self.cache.item_fling_effect, name, route)
    data = await self._client.request(route)
    return self.cache.item_fling_effect.setdefault(route, ItemFlingEffect.from_payload(data))

fetch_item_pocket(name) async ⚓︎

Fetches an item pocket from the API.

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

    The name or id of the item pocket.

Returns:
  • ItemPocket

    The item pocket 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:
...     item_pocket = await client.item.fetch_item_pocket(1)
...     print(item_pocket.name)
...     await client.close()
>>> asyncio.run(main())
misc
Source code in pokelance/ext/item.py
Python
async def fetch_item_pocket(self, name: t.Union[str, int]) -> ItemPocket:
    """Fetches an item pocket from the API.

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

    Returns
    -------
    ItemPocket
        The item pocket if it exists in the API, else raises ResourceNotFound.

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

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> import asyncio
    >>> client = PokeLance()
    >>> async def main() -> None:
    ...     item_pocket = await client.item.fetch_item_pocket(1)
    ...     print(item_pocket.name)
    ...     await client.close()
    >>> asyncio.run(main())
    misc
    """
    route = Endpoint.get_item_pocket(name)
    self._validate_resource(self.cache.item_pocket, name, route)
    data = await self._client.request(route)
    return self.cache.item_pocket.setdefault(route, ItemPocket.from_payload(data))

get_item(name) ⚓︎

Gets an item from the cache.

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

    The name or id of the item.

Returns:
  • Optional[Item]

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

Raises:

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> client = PokeLance()
>>> item = client.item.get_item("potion")
>>> item.id
17
Source code in pokelance/ext/item.py
Python
def get_item(self, name: t.Union[str, int]) -> t.Optional[ItemModel]:
    """Gets an item from the cache.

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

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

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

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> client = PokeLance()
    >>> item = client.item.get_item("potion")
    >>> item.id
    17
    """
    route = Endpoint.get_item(name)
    self._validate_resource(self.cache.item, name, route)
    return self.cache.item.get(route, None)

get_item_attribute(name) ⚓︎

Gets an item attribute from the cache.

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

    The name or id of the item attribute.

Returns:
Raises:

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> client = PokeLance()
>>> item_attribute = client.item.get_item_attribute("holdable")
>>> item_attribute.id
5
Source code in pokelance/ext/item.py
Python
def get_item_attribute(self, name: t.Union[str, int]) -> t.Optional[ItemAttribute]:
    """Gets an item attribute from the cache.

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

    Returns
    -------
    typing.Optional[pokelance.models.ItemAttribute]
        The item attribute if it exists in the cache, else None.

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

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> client = PokeLance()
    >>> item_attribute = client.item.get_item_attribute("holdable")
    >>> item_attribute.id
    5
    """
    route = Endpoint.get_item_attribute(name)
    self._validate_resource(self.cache.item_attribute, name, route)
    return self.cache.item_attribute.get(route, None)

get_item_category(name) ⚓︎

Gets an item category from the cache.

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

    The name or id of the item category.

Returns:
Raises:

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> client = PokeLance()
>>> item_category = client.item.get_item_category(1)
>>> item_category.name
'stat-boosts'
Source code in pokelance/ext/item.py
Python
def get_item_category(self, name: t.Union[str, int]) -> t.Optional[ItemCategory]:
    """Gets an item category from the cache.

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

    Returns
    -------
    typing.Optional[pokelance.models.ItemCategory]
        The item category if it exists in the cache, else None.

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

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> client = PokeLance()
    >>> item_category = client.item.get_item_category(1)
    >>> item_category.name
    'stat-boosts'
    """
    route = Endpoint.get_item_category(name)
    self._validate_resource(self.cache.item_category, name, route)
    return self.cache.item_category.get(route, None)

get_item_fling_effect(name) ⚓︎

Gets an item fling effect from the cache.

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

    The name or id of the item fling effect.

Returns:
Raises:

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> client = PokeLance()
>>> item_fling_effect = client.item.get_item_fling_effect(1)
>>> item_fling_effect.name
'badly-poison'
Source code in pokelance/ext/item.py
Python
def get_item_fling_effect(self, name: t.Union[str, int]) -> t.Optional[ItemFlingEffect]:
    """Gets an item fling effect from the cache.

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

    Returns
    -------
    typing.Optional[pokelance.models.ItemFlingEffect]
        The item fling effect if it exists in the cache, else None.

    Raises
    ------
    pokelance.exceptions.ResourceNotFound
        The name or id of the item fling effect is invalid.

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> client = PokeLance()
    >>> item_fling_effect = client.item.get_item_fling_effect(1)
    >>> item_fling_effect.name
    'badly-poison'
    """
    route = Endpoint.get_item_fling_effect(name)
    self._validate_resource(self.cache.item_fling_effect, name, route)
    return self.cache.item_fling_effect.get(route, None)

get_item_pocket(name) ⚓︎

Gets an item pocket from the cache.

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

    The name or id of the item pocket.

Returns:
Raises:

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> client = PokeLance()
>>> item_pocket = client.item.get_item_pocket(1)
>>> item_pocket.name
'misc'
Source code in pokelance/ext/item.py
Python
def get_item_pocket(self, name: t.Union[str, int]) -> t.Optional[ItemPocket]:
    """Gets an item pocket from the cache.

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

    Returns
    -------
    typing.Optional[pokelance.models.ItemPocket]
        The item pocket if it exists in the cache, else None.

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

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> client = PokeLance()
    >>> item_pocket = client.item.get_item_pocket(1)
    >>> item_pocket.name
    'misc'
    """
    route = Endpoint.get_item_pocket(name)
    self._validate_resource(self.cache.item_pocket, name, route)
    return self.cache.item_pocket.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."

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

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