pokelance.ext.contest ⚓︎

Contest(client) ⚓︎

Bases: BaseExtension

Extension for contest related endpoints.

Attributes:
  • cache (Contest) –

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

Fetches a contest effect from the API.

Parameters:
  • id_ (int) –

    The name or id of the contest effect.

Returns:
  • ContestEffect

    The contest effect if it exists in the API, else None.

Raises:

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> import asyncio
>>> client = PokeLance()
>>> async def main() -> None:
...     effect = await client.contest.fetch_contest_effect(1)
...     print(effect.appeal)
...     await client.close()
>>> asyncio.run(main())
4
Source code in pokelance/ext/contest.py
Python
async def fetch_contest_effect(self, id_: int) -> ContestEffect:
    """Fetches a contest effect from the API.

    Parameters
    ----------
    id_: int
        The name or id of the contest effect.

    Returns
    -------
    ContestEffect
        The contest effect if it exists in the API, else None.

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

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> import asyncio
    >>> client = PokeLance()
    >>> async def main() -> None:
    ...     effect = await client.contest.fetch_contest_effect(1)
    ...     print(effect.appeal)
    ...     await client.close()
    >>> asyncio.run(main())
    4
    """
    route = Endpoint.get_contest_effect(id_)
    self._validate_resource(self.cache.contest_effect, id_, route)
    data = await self._client.request(route)
    return self.cache.contest_effect.setdefault(route, ContestEffect.from_payload(data))

fetch_contest_type(name) async ⚓︎

Fetches a contest type from the API.

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

    The name or id of the contest type.

Returns:
  • ContestType

    The contest type if it exists in the API, else None.

Raises:

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> import asyncio
>>> client = PokeLance()
>>> async def main() -> None:
...     type_ = await client.contest.fetch_contest_type("cool")
...     print(type_.name)
...     await client.close()
>>> asyncio.run(main())
cool
Source code in pokelance/ext/contest.py
Python
async def fetch_contest_type(self, name: t.Union[str, int]) -> ContestType:
    """Fetches a contest type from the API.

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

    Returns
    -------
    ContestType
        The contest type if it exists in the API, else None.

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

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> import asyncio
    >>> client = PokeLance()
    >>> async def main() -> None:
    ...     type_ = await client.contest.fetch_contest_type("cool")
    ...     print(type_.name)
    ...     await client.close()
    >>> asyncio.run(main())
    cool
    """
    route = Endpoint.get_contest_type(name)
    self._validate_resource(self.cache.contest_type, name, route)
    data = await self._client.request(route)
    return self.cache.contest_type.setdefault(route, ContestType.from_payload(data))

fetch_super_contest_effect(id_) async ⚓︎

Fetches a super contest effect from the API.

Parameters:
  • id_ (int) –

    The name or id of the super contest effect.

Returns:
Raises:

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> import asyncio
>>> client = PokeLance()
>>> async def main() -> None:
...     effect = await client.contest.fetch_super_contest_effect(1)
...     print(effect.appeal)
...     await client.close()
>>> asyncio.run(main())
2
Source code in pokelance/ext/contest.py
Python
async def fetch_super_contest_effect(self, id_: int) -> SuperContestEffect:
    """Fetches a super contest effect from the API.

    Parameters
    ----------
    id_: int
        The name or id of the super contest effect.

    Returns
    -------
    SuperContestEffect
        The super contest effect if it exists in the API, else None.

    Raises
    ------
    pokelance.exceptions.ResourceNotFound
        The name or id of the super contest effect is invalid.

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> import asyncio
    >>> client = PokeLance()
    >>> async def main() -> None:
    ...     effect = await client.contest.fetch_super_contest_effect(1)
    ...     print(effect.appeal)
    ...     await client.close()
    >>> asyncio.run(main())
    2
    """
    route = Endpoint.get_super_contest_effect(id_)
    self._validate_resource(self.cache.super_contest_effect, id_, route)
    data = await self._client.request(route)
    return self.cache.super_contest_effect.setdefault(route, SuperContestEffect.from_payload(data))

get_contest_effect(id_) ⚓︎

Gets a contest effect from the cache.

Parameters:
  • id_ (int) –

    The name or id of the contest effect.

Returns:
Raises:

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> client = PokeLance()
>>> effect = client.contest.get_contest_effect(1)  # None if not cached
>>> effect.appeal
4
Source code in pokelance/ext/contest.py
Python
def get_contest_effect(self, id_: int) -> t.Optional[ContestEffect]:
    """Gets a contest effect from the cache.

    Parameters
    ----------
    id_: int
        The name or id of the contest effect.

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

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

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> client = PokeLance()
    >>> effect = client.contest.get_contest_effect(1)  # None if not cached
    >>> effect.appeal
    4
    """
    route = Endpoint.get_contest_effect(id_)
    self._validate_resource(self.cache.contest_effect, id_, route)
    return self.cache.contest_effect.get(route, None)

get_contest_type(name) ⚓︎

Gets a contest type from the cache.

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

    The name or id of the contest type.

Returns:
Raises:

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> client = PokeLance()
>>> type_ = client.contest.get_contest_type("cool")  # None if not cached
>>> type_.name
'cool'
Source code in pokelance/ext/contest.py
Python
def get_contest_type(self, name: t.Union[str, int]) -> t.Optional[ContestType]:
    """Gets a contest type from the cache.

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

    Returns
    -------
    typing.Optional[pokelance.models.ContestType]
        The contest type if it exists in the cache, else None.

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

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> client = PokeLance()
    >>> type_ = client.contest.get_contest_type("cool")  # None if not cached
    >>> type_.name
    'cool'
    """
    route = Endpoint.get_contest_type(name)
    self._validate_resource(self.cache.contest_type, name, route)
    return self.cache.contest_type.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_super_contest_effect(id_) ⚓︎

Gets a super contest effect from the cache.

Parameters:
  • id_ (int) –

    The name or id of the super contest effect.

Returns:
Raises:

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> client = PokeLance()
>>> effect = client.contest.get_super_contest_effect(1)
>>> effect.appeal
2
Source code in pokelance/ext/contest.py
Python
def get_super_contest_effect(self, id_: int) -> t.Optional[SuperContestEffect]:
    """Gets a super contest effect from the cache.

    Parameters
    ----------
    id_: int
        The name or id of the super contest effect.

    Returns
    -------
    typing.Optional[pokelance.models.SuperContestEffect]
        The super contest effect if it exists in the cache, else None.

    Raises
    ------
    pokelance.exceptions.ResourceNotFound
        The name or id of the super contest effect is invalid.

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> client = PokeLance()
    >>> effect = client.contest.get_super_contest_effect(1)
    >>> effect.appeal
    2
    """
    route = Endpoint.get_super_contest_effect(id_)
    self._validate_resource(self.cache.super_contest_effect, id_, route)
    return self.cache.super_contest_effect.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 contest cog.

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