Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion coc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
SOFTWARE.
"""

__version__ = "2.1.1"
__version__ = "2.2.0"

from .abc import BasePlayer, BaseClan
from .clans import RankedClan, Clan
Expand Down
135 changes: 104 additions & 31 deletions coc/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import ujson

from .clans import Clan, RankedClan
from .entry_logs import ClanWarLog
from .errors import Forbidden, GatewayError, NotFound, PrivateWarLog
from .enums import WarRound
from .miscmodels import GoldPassSeason, Label, League, Location, LoadGameData
Expand All @@ -44,11 +45,12 @@
CurrentWarIterator,
)
from .players import Player, ClanMember, RankedPlayer
from .raid import RaidLog, RaidLogEntry
from .raid import RaidLogEntry
from .spell import SpellHolder
from .troop import TroopHolder
from .utils import correct_tag, get, parse_army_link
from .wars import ClanWar, ClanWarLog, ClanWarLogEntry, ClanWarLeagueGroup
from .wars import ClanWar, ClanWarLogEntry, ClanWarLeagueGroup
from.entry_logs import ClanWarLog, RaidLog

if TYPE_CHECKING:
from .hero import Hero, Pet
Expand Down Expand Up @@ -197,6 +199,13 @@ def __init__(
self._clans = {}
self._wars = {}

async def __aenter__(self):
self.__init__()
return self

async def __aexit__(self, *args):
await self.close()

def _create_client(self, email, password):
return HTTPClient(
client=self,
Expand Down Expand Up @@ -494,11 +503,18 @@ async def get_warlog(
self,
clan_tag: str,
cls: Type[ClanWarLogEntry] = ClanWarLogEntry,
paginated=True,
**kwargs
page: bool = False,
limit: int = 0
) -> ClanWarLog:
"""Retrieve a clan's clan war log.
Set paginated = False to get the full war log with one API call.
"""
Retrieve a clan's clan war log. By default, this will return
all the clan's log available in the API. This will of course consume
memory. The option of limiting the amount of log items fetched
can be controlled with the `limit` parameter. Additionally, if
`paginate` is set to True, and an async for loop is performed
on this object, then additional log items will be fetched but only
consume the same amount of memory space at all time.


.. note::

Expand All @@ -509,8 +525,20 @@ async def get_warlog(

Parameters
-----------
clan_tag : str
The clan tag to search for.
cls:
Target class to use to model that data returned

clan_tag:
class:`str`: The clan tag to search for.

page:
class:`bool`: Enable fetching logs while only holding the
same amount of logs as `limit`. If `paginate` is set to True,
and `limit` is set to default of 0, then `limit` will be set to
10 automatically.

limit:
class:`int`: Number of logs to retrieve

Raises
------
Expand All @@ -532,38 +560,66 @@ async def get_warlog(

Returns
--------
List[:class:`ClanWarLogEntry`]
:class:`ClanWarLog`:
Entries in the warlog of the requested clan.
"""
if limit < 0:
raise ValueError("Limit cannot be negative")

if not issubclass(cls, ClanWarLogEntry):
raise TypeError("cls must be a subclass of ClanWarLogEntry.")

if self.correct_tags:
clan_tag = correct_tag(clan_tag)

if paginated and not kwargs.get("limit", None):
kwargs["limit"] = 5
# If paginate is enabled and limit is set to default of 0, then
# set limit to a new default of 10
if page:
limit = limit if limit else 10

try:
data = await self.http.get_clan_warlog(clan_tag, **kwargs)
return await ClanWarLog.init_cls(client=self,
clan_tag=clan_tag,
page=page,
limit=limit,
model=cls)
except Forbidden as exception:
raise PrivateWarLog(exception.response, exception.reason) from exception

return ClanWarLog(data=data, client=self, cls=cls, clan_tag=clan_tag)
raise PrivateWarLog(exception.response,
exception.reason) from exception

async def get_raidlog(
self,
clan_tag: str,
cls: Type[RaidLogEntry] = RaidLogEntry,
paginated: bool = True,
**kwargs
self,
clan_tag: str,
cls: Type[RaidLogEntry] = RaidLogEntry,
page: bool = False,
limit: int = 0
) -> RaidLog:
"""Retrieve a clan's raid log.
Set paginated = False to get the full raid log with one API call.
"""
Retrieve a clan's Capital Raid Log. By default, this will return
all the clan's log available in the API. This will of course consume
memory. The option of limiting the amount of log items fetched
can be controlled with the `limit` parameter. Additionally, if
`paginate` is set to True, and an async for loop is performed
on this object, then additional log items will be fetched but only
consume the same amount of memory space at all time.


Parameters
-----------
clan_tag : str
The clan tag to search for.
cls:
Target class to use to model that data returned

clan_tag:
class:`str`: The clan tag to search for.

page:
class:`bool`: Enable fetching logs while only holding the
same amount of logs as `limit`. If `paginate` is set to True,
and `limit` is set to default of 0, then `limit` will be set to
10 automatically.

limit:
class:`int`: Number of logs to retrieve

Raises
------
Expand All @@ -573,28 +629,45 @@ async def get_raidlog(
NotFound
No clan was found with the supplied tag.

PrivateWarLog
The clan's warlog is private.

Maintenance
The API is currently in maintenance.

GatewayError
The API hit an unexpected gateway exception.


Returns
--------
List[:class:`RaidLogEntry`]
Entries in the raid log of the requested clan.
:class:`RaidLog`:
Entries in the capital raid seasons of the requested clan.
"""

if limit < 0:
raise ValueError("Limit cannot be negative")

if not issubclass(cls, RaidLogEntry):
raise TypeError("cls must be a subclass of RaidLogEntry.")
raise TypeError("cls must be a subclass of ClanWarLogEntry.")

if self.correct_tags:
clan_tag = correct_tag(clan_tag)

if paginated and not kwargs.get("limit", None):
kwargs["limit"] = 5
# If paginate is enabled and limit is set to default of 0, then
# set limit to a new default of 10
if page:
limit = limit if limit else 10

data = await self.http.get_clan_raidlog(clan_tag, **kwargs)
return RaidLog(data=data, client=self, cls=cls, clan_tag=clan_tag)
try:
return await RaidLog.init_cls(client=self,
clan_tag=clan_tag,
page=page,
limit=limit,
model=cls)
except Forbidden as exception:
raise PrivateWarLog(exception.response,
exception.reason) from exception

async def get_clan_war(self, clan_tag: str, cls: Type[ClanWar] = ClanWar, **kwargs) -> ClanWar:
"""
Expand Down
Loading