Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New features #37

Merged
merged 9 commits into from
Sep 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.11", "3.10", "3.9", "3.8"]
python-version: ["3.11", "3.10"]

steps:
- uses: actions/checkout@v3
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ password of the ClashOfClans developer portal
- Control over the number of requests per second and the number of used tokens
- Open source
- Type hinted
- Sopports Python 3.8 -> 3.11
- Supports Python 3.8 -> 3.11

---

Expand All @@ -58,7 +58,6 @@ I'm planning to keep the API wrapper up to date and improve it as good as I can.
### Planned features

- more bulk requests
- pytests for the models
- events

### Planned utils
Expand Down
2 changes: 1 addition & 1 deletion pyclasher/api/bulk_requests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .b_player import PlayerBulkRequest
from .b_request_model import BulkRequestModel
from .abc import BulkRequestModel

__all__ = (
"PlayerBulkRequest",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

class BulkRequestModel:
_request_model = ...
_main_attribute = None
_requests = None

@property
Expand All @@ -21,19 +20,11 @@ def __get_properties(self):
if isinstance(prop, property)
}

async def _async_request(self):
self._tasks = [request.request() for request in self._requests]
async def request(self, client_id=None):
self._tasks = [request.request(client_id) for request in self._requests]
await gather(*self._tasks)
return self

def request(self):
try:
get_running_loop()
except RuntimeError:
return run(self._async_request())
else:
return self._async_request()

def __len__(self):
return len(self._requests)

Expand All @@ -54,7 +45,7 @@ def __next__(self):
return next(self._iter)

def __str__(self):
return f"{self.__class__.__name__}({self._main_attribute})"
return f"{self.__class__.__name__}()"

def __repr__(self):
props = ', '.join(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ class BulkRequestModel:

:cvar _request_model: the request model that is used to make the bulk request
:type _request_model: Any
:cvar _main_attribute: the main attribute used for the string representation (default is None)
:type _main_attribute: Any
:cvar _requests: list of requests
:type _requests: list
"""

_request_model: Any = ...
_main_attribute: Any = None
_requests: list = None

@property
Expand Down Expand Up @@ -52,26 +49,14 @@ class BulkRequestModel:
"""
...

async def _async_request(self) -> BulkRequestModel:
async def request(self, client_id: int | str = None) -> BulkRequestModel:
"""
asynchronous method that executes the requests

:return: the instance of the bulk request model
:rtype: BulkRequestModel
"""
self._tasks = [request.request() for request in self._requests]
...

def request(self) -> BulkRequestModel | Coroutine[Any, Any, BulkRequestModel]:
"""
method that executes the request

this method can be used in an asynchronous context using the ``await`` keyword
but can also be used in non-asynchronous context without awaiting the method

:return: the instance of the model
:rtype: BulkRequestModel | Coroutine[Any, Any, BulkRequestModel]
"""
self._tasks = [request.request(client_id) for request in self._requests]
...

def __len__(self) -> int:
Expand Down
18 changes: 4 additions & 14 deletions pyclasher/api/bulk_requests/b_player.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from asyncio import get_running_loop, run

from .b_request_model import BulkRequestModel
from .abc import BulkRequestModel
from ..models import BaseClan
from ..models import Clan
from ..requests import PlayerRequest, ClanMembersRequest
Expand All @@ -13,32 +13,22 @@ class PlayerBulkRequest(BulkRequestModel):
def __init__(self, tags):
self._tags = tags
self._requests = list(self._request_model(tag) for tag in self.tags)
self._main_attribute = self.tags
return

@property
def tags(self):
return self._tags

@classmethod
async def _async_from_clan(cls, clan):
async def from_clan(cls, clan, client_id=None):
if isinstance(clan, Clan) and clan.member_list is not MISSING:
members = clan.member_list
elif isinstance(clan, BaseClan):
members = await ClanMembersRequest(clan.tag).request()
members = await ClanMembersRequest(clan.tag).request(client_id)
else:
members = await ClanMembersRequest(clan).request()
members = await ClanMembersRequest(clan).request(client_id)
return cls.from_member_list(members)

@classmethod
def from_clan(cls, clan):
try:
get_running_loop()
except RuntimeError:
return run(cls._async_from_clan(clan))
else:
return cls._async_from_clan(clan)

@classmethod
def from_member_list(cls, member_list):
return cls((member.tag for member in member_list))
Expand Down
6 changes: 3 additions & 3 deletions pyclasher/api/bulk_requests/b_player.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Iterable, Coroutine, Any, Iterator

from .b_request_model import BulkRequestModel
from .abc import BulkRequestModel
from ..models import BaseClan, ClanMemberList, ClanWarMemberList, \
ClanWarLeagueClanMemberList, \
ClanCapitalRaidSeasonMemberList
Expand Down Expand Up @@ -43,13 +43,13 @@ class PlayerBulkRequest(BulkRequestModel):
...

@classmethod
def from_clan(cls, clan: BaseClan | str) -> PlayerBulkRequest | Coroutine[Any, Any, PlayerBulkRequest]:
async def from_clan(cls, clan: BaseClan | str, client_id: int | str = None) -> PlayerBulkRequest:
"""
class method to create an instance using a clan or a clan tag

:param cls: PlayerBulkRequest
:param clan: clan or clan tag
:rtype: PlayerBulkRequest | Coroutine[Any, Any, PlayerBulkRequest]
:rtype: PlayerBulkRequest
"""
...

Expand Down
1 change: 0 additions & 1 deletion pyclasher/api/models/base_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class IconUrl(ImageUrl):
class IconUrls(BaseModel):
def __init__(self, data: dict):
super().__init__(data)
self._main_attribute = self.small
return

@property
Expand Down
2 changes: 0 additions & 2 deletions pyclasher/api/models/clan.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
class ClanDistrictData(BaseModel):
def __init__(self, data):
super().__init__(data)
self._main_attribute = self.id
return

@property
Expand All @@ -34,7 +33,6 @@ class ClanDistrictDataList(IterBaseModel):
class ClanCapital(BaseModel):
def __init__(self, data):
super().__init__(data)
self._main_attribute = self.capital_hall_level
return

@property
Expand Down
5 changes: 0 additions & 5 deletions pyclasher/api/models/clan_capital_raid_seasons.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
class ClanCapitalRaidSeasonClanInfo(BaseModel):
def __init__(self, data):
super().__init__(data)
self._main_attribute = self.tag
return

@property
Expand All @@ -28,7 +27,6 @@ def badge_urls(self):
class ClanCapitalRaidSeasonAttacker(BaseModel):
def __init__(self, data):
super().__init__(data)
self._main_attribute = self.tag
return

@property
Expand All @@ -43,7 +41,6 @@ def name(self):
class ClanCapitalRaidSeasonAttack(BaseModel):
def __init__(self, data):
super().__init__(data)
self._main_attribute = self.destruction_percent
return

@property
Expand All @@ -66,7 +63,6 @@ class ClanCapitalRaidSeasonAttackList(IterBaseModel):
class ClanCapitalRaidSeasonDistrict(BaseModel):
def __init__(self, data):
super().__init__(data)
self._main_attribute = self.name
return

@property
Expand Down Expand Up @@ -192,7 +188,6 @@ def average_resources_looted(self):
class ClanCapitalRaidSeason(BaseModel):
def __init__(self, data):
super().__init__(data)
self._main_attribute = self.raids_completed
return

@property
Expand Down
2 changes: 0 additions & 2 deletions pyclasher/api/models/clan_war_league_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def __next__(self):
class ClanWarLeagueClan(BaseClan):
def __init__(self, data):
super().__init__(data)
self._main_attribute = self.tag
return

@property
Expand All @@ -63,7 +62,6 @@ def __next__(self):
class ClanWarLeagueGroup(BaseModel):
def __init__(self, data):
super().__init__(data)
self._main_attribute = self.tag
return

@property
Expand Down
1 change: 0 additions & 1 deletion pyclasher/api/models/labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
class Label(BaseModel):
def __init__(self, data):
super().__init__(data)
self._main_attribute = self.id
return

@property
Expand Down
1 change: 0 additions & 1 deletion pyclasher/api/models/language.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
class Language(BaseModel):
def __init__(self, data: dict):
super().__init__(data)
self._main_attribute = self.id
return

@property
Expand Down
1 change: 0 additions & 1 deletion pyclasher/api/models/leagues.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class WarLeagueList(IterBaseModel):
class LeagueSeason(BaseModel):
def __init__(self, data):
super().__init__(data)
self._main_attribute = self.id
return

@property
Expand Down
1 change: 0 additions & 1 deletion pyclasher/api/models/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
class Location(BaseModel):
def __init__(self, data):
super().__init__(data)
self._main_attribute = self.id if data is not None else None
return

@property
Expand Down
1 change: 0 additions & 1 deletion pyclasher/api/models/login/login_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def detail(self):
class Auth(BaseModel):
def __init__(self, data):
super().__init__(data)
self._main_attribute = self.uid
return

@property
Expand Down
3 changes: 0 additions & 3 deletions pyclasher/api/models/misc/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
class ClientError(BaseModel):
def __init__(self, data):
super().__init__(data)
self._main_attribute = self.reason
return

@property
Expand All @@ -29,7 +28,6 @@ class Replay(BaseModel):

def __init__(self, data):
super().__init__(data)
self._main_attribute = self.replay_tag
return

@property
Expand All @@ -46,7 +44,6 @@ class ServiceVersion(BaseModel):

def __init__(self, data):
super().__init__(data)
self._main_attribute = (self.major, self.minor)
return

@property
Expand Down
2 changes: 0 additions & 2 deletions pyclasher/api/models/misc/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
class VerifyTokenResponse(BaseModel):
def __init__(self, data):
super().__init__(data)
self._main_attribute = self.tag
return

@property
Expand All @@ -24,7 +23,6 @@ def status(self):
class DeepLinkCreationResponse(BaseModel):
def __init__(self, data):
super().__init__(data)
self._main_attribute = self.link
return

@property
Expand Down
1 change: 0 additions & 1 deletion pyclasher/api/models/misc/war_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
class WarStatus(BaseModel):
def __init__(self, data):
super().__init__(data)
self._main_attribute = self.clan_tag
return

@property
Expand Down
2 changes: 0 additions & 2 deletions pyclasher/api/models/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def clan_level(self):
class LegendLeagueTournamentSeasonResult(BaseModel):
def __init__(self, data):
super().__init__(data)
self._main_attribute = self.id
return

@property
Expand Down Expand Up @@ -136,7 +135,6 @@ class PlayerAchievementProgressList(IterBaseModel):
class Player(BaseModel):
def __init__(self, data):
super().__init__(data)
self._main_attribute = self.tag
return

@property
Expand Down
1 change: 0 additions & 1 deletion pyclasher/api/models/player_house.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
class PlayerHouseElement(BaseModel):
def __init__(self, data):
super().__init__(data)
self._main_attribute = self.id
return

@property
Expand Down
1 change: 0 additions & 1 deletion pyclasher/api/models/war_clan.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
class ClanWarAttack(BaseModel):
def __init__(self, data):
super().__init__(data)
self._main_attribute = self.attacker_tag
return

@property
Expand Down
1 change: 0 additions & 1 deletion pyclasher/api/requests/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class for requesting
"""

_data = MISSING
_main_attribute = None
_url = None
_url_kwargs = None
_len = None
Expand Down
1 change: 0 additions & 1 deletion pyclasher/api/requests/abc.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ request_id: int = 0

class RequestModel(ABC):
_data: dict = MISSING
_main_attribute: Any = None
_url: str = None
_url_kwargs: dict | None = None
_len: int = None
Expand Down
Loading