pokelance.ext.encounter ⚓︎

Encounter(client) ⚓︎

Bases: BaseExtension

Extension for encounter related endpoints.

Attributes:
  • cache (Encounter) –

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

Fetches an encounter condition from the API.

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

    The name or id of the encounter condition.

Returns:
  • EncounterCondition

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

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

    Returns
    -------
    EncounterCondition
        The encounter condition if it exists in the API, else raises ResourceNotFound.

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

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> import asyncio
    >>> client = PokeLance()
    >>> async def main() -> None:
    ...     condition = await client.encounter.fetch_encounter_condition("swarm")
    ...     print(condition.name)
    ...     await client.close()
    >>> asyncio.run(main())
    swarm
    """
    route = Endpoint.get_encounter_condition(name)
    self._validate_resource(self.cache.encounter_condition, name, route)
    data = await self._client.request(route)
    return self.cache.encounter_condition.setdefault(route, EncounterCondition.from_payload(data))

fetch_encounter_condition_value(name) async ⚓︎

Fetches an encounter condition value from the API.

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

    The name or id of the encounter condition value.

Returns:
Raises:
  • ResourceNotFound

    The name or id of the encounter condition value is invalid.

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> import asyncio
>>> client = PokeLance()
>>> async def main() -> None:
...     condition = await client.encounter.fetch_encounter_condition_value("swarm-yes")
...     print(condition.name)
...     await client.close()
>>> asyncio.run(main())
swarm-yes
Source code in pokelance/ext/encounter.py
Python
async def fetch_encounter_condition_value(self, name: t.Union[str, int]) -> EncounterConditionValue:
    """Fetches an encounter condition value from the API.

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

    Returns
    -------
    EncounterConditionValue
        The encounter condition value if it exists in the API, else raises ResourceNotFound.

    Raises
    ------
    pokelance.exceptions.ResourceNotFound
        The name or id of the encounter condition value is invalid.

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> import asyncio
    >>> client = PokeLance()
    >>> async def main() -> None:
    ...     condition = await client.encounter.fetch_encounter_condition_value("swarm-yes")
    ...     print(condition.name)
    ...     await client.close()
    >>> asyncio.run(main())
    swarm-yes
    """
    route = Endpoint.get_encounter_condition_value(name)
    self._validate_resource(self.cache.encounter_condition_value, name, route)
    data = await self._client.request(route)
    return self.cache.encounter_condition_value.setdefault(route, EncounterConditionValue.from_payload(data))

fetch_encounter_method(name) async ⚓︎

Fetches an encounter method from the API.

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

    The name or id of the encounter method.

Returns:
  • EncounterMethod

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

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

    Returns
    -------
    EncounterMethod
        The encounter method if it exists in the API, else raises ResourceNotFound.

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

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> import asyncio
    >>> client = PokeLance()
    >>> async def main() -> None:
    ...     method = await client.encounter.fetch_encounter_method("walk")
    ...     print(method.name)
    ...     await client.close()
    >>> asyncio.run(main())
    walk
    """
    route = Endpoint.get_encounter_method(name)
    self._validate_resource(self.cache.encounter_method, name, route)
    data = await self._client.request(route)
    return self.cache.encounter_method.setdefault(route, EncounterMethod.from_payload(data))

get_encounter_condition(name) ⚓︎

Gets an encounter condition from the cache.

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

    The name or id of the encounter condition.

Returns:
Raises:

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> client = PokeLance()
>>> condition = client.encounter.get_encounter_condition("swarm")
>>> condition.name
'swarm'
Source code in pokelance/ext/encounter.py
Python
def get_encounter_condition(self, name: t.Union[str, int]) -> t.Optional[EncounterCondition]:
    """Gets an encounter condition from the cache.

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

    Returns
    -------
    typing.Optional[pokelance.models.EncounterCondition]
        The encounter condition if it exists in the cache, else None.

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

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> client = PokeLance()
    >>> condition = client.encounter.get_encounter_condition("swarm")
    >>> condition.name
    'swarm'
    """
    route = Endpoint.get_encounter_condition(name)
    self._validate_resource(self.cache.encounter_condition, name, route)
    return self.cache.encounter_condition.get(route, None)

get_encounter_condition_value(name) ⚓︎

Gets an encounter condition value from the cache.

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

    The name or id of the encounter condition value.

Returns:
Raises:
  • ResourceNotFound

    The name or id of the encounter condition value is invalid.

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> client = PokeLance()
>>> condition = client.encounter.get_encounter_condition_value("swarm-yes")
>>> condition.name
'swarm-yes'
Source code in pokelance/ext/encounter.py
Python
def get_encounter_condition_value(self, name: t.Union[str, int]) -> t.Optional[EncounterConditionValue]:
    """Gets an encounter condition value from the cache.

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

    Returns
    -------
    typing.Optional[pokelance.models.EncounterConditionValue]
        The encounter condition value if it exists in the cache, else None.

    Raises
    ------
    pokelance.exceptions.ResourceNotFound
        The name or id of the encounter condition value is invalid.

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> client = PokeLance()
    >>> condition = client.encounter.get_encounter_condition_value("swarm-yes")
    >>> condition.name
    'swarm-yes'
    """
    route = Endpoint.get_encounter_condition_value(name)
    self._validate_resource(self.cache.encounter_condition_value, name, route)
    return self.cache.encounter_condition_value.get(route, None)

get_encounter_method(name) ⚓︎

Gets an encounter method from the cache.

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

    The name or id of the encounter method.

Returns:
Raises:

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> client = PokeLance()
>>> method = client.encounter.get_encounter_method("walk")
>>> method.name
'walk'
Source code in pokelance/ext/encounter.py
Python
def get_encounter_method(self, name: t.Union[str, int]) -> t.Optional[EncounterMethod]:
    """Gets an encounter method from the cache.

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

    Returns
    -------
    typing.Optional[pokelance.models.EncounterMethod]
        The encounter method if it exists in the cache, else None.

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

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> client = PokeLance()
    >>> method = client.encounter.get_encounter_method("walk")
    >>> method.name
    'walk'
    """
    route = Endpoint.get_encounter_method(name)
    self._validate_resource(self.cache.encounter_method, name, route)
    return self.cache.encounter_method.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 encounter cog.

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