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

Correcting errors #24

Merged
merged 26 commits into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
3cc2456
improved iterative models
201st-Luka Jul 28, 2023
e52b211
created build.sh
201st-Luka Jul 28, 2023
584bb0b
fix: `PlayerBulkRequest._async_from_clan` accepts `Clan`, `MemberList…
201st-Luka Aug 12, 2023
47e57db
style: line break after 120 characters in client.py
201st-Luka Aug 12, 2023
367ddf7
fix: correct type hinting for `ApiCodes.from_exception`
201st-Luka Aug 12, 2023
7901fd0
feat: added a ToDo-list in the README.md
201st-Luka Aug 12, 2023
3768396
feat: added `__version` to `pyclasher`
201st-Luka Aug 12, 2023
948fe78
feat: added requirements_tests.txt
201st-Luka Aug 13, 2023
46214d8
fix: added `ClanWarResult.NONE` in the `Enums.py` file
201st-Luka Aug 13, 2023
5ae66e3
tests: rewrite and improvement
201st-Luka Aug 13, 2023
6cc970b
fix!: removed `Player.best_versus_trophies`
201st-Luka Aug 13, 2023
7fa449a
fix!: removed `Player.versus_trophies`
201st-Luka Aug 13, 2023
cbf9ad8
refactor: used double equality instead of 2 lines
201st-Luka Aug 13, 2023
d4e9238
fix: `Player.league` can be MISSING
201st-Luka Aug 13, 2023
c6f3dd4
test: added `PlayerRequestTest`
201st-Luka Aug 13, 2023
40d815c
fix: corrected wrong type hint for `ClanLabelsRequest` and `PlayerLab…
201st-Luka Aug 13, 2023
52d2149
test: implemented label tests
201st-Luka Aug 13, 2023
8ed0159
fix: `BaseModel:__init__` default `data` argument is None
201st-Luka Aug 13, 2023
5f331f0
fix: initializing `GoldPassRequest` fix
201st-Luka Aug 13, 2023
c5c54c7
test: `GoldPassRequest` test
201st-Luka Aug 13, 2023
b608720
test: `LocationsRequest` test
201st-Luka Aug 14, 2023
17853f5
fix: `test_locations.py` file was not in the right directory
201st-Luka Aug 14, 2023
10dfd2b
feat: updated README.md
201st-Luka Aug 16, 2023
834c5b5
fix: corrected test workflow
201st-Luka Aug 19, 2023
f84e331
fix: solved import error
201st-Luka Aug 19, 2023
fcf0641
fix: solved import error in `test_client.py`
201st-Luka Aug 19, 2023
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
4 changes: 2 additions & 2 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
python -m pip install flake8
pip install -r requirements_tests.txt
- name: linting
run: |
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,26 @@ good as I can.
- documentation
- real example ([HeadhunterBot][headhunterbot_url] is in development)

### Planned code implementations (ToDo-list)

- `total_wars` attribute for `ClanWarLogEntry`
- war log filtering for `ClanWarLogRequest` (for example filter the number of attacks per member)
- some "average" attributes for `Clan`-classes, `WarLog`-classes, ...
- attributes `king`, `queen`, `warden`, `royal_champion` for the `Player.heroes` attribute
- autosort for the `ClanCurrentWarRequest` members of the `member_list` attribute (sorted by the map position)

---

If you find a bug, an error or want custom functionality, please tell
me via Discord or open an issue or start a discussion on the
GitHub-repository.

---

##### Disclaimer
This material is unofficial and is not endorsed by Supercell. For more information see Supercell's Fan Content Policy:
www.supercell.com/fan-content-policy.



<!---links--->
Expand Down
3 changes: 3 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
rm -r build dist pyclasher.egg-info

python setup.py sdist bdist_wheel
2 changes: 2 additions & 0 deletions pyclasher/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
.. author:: 201st-Luka
"""

__version__ = '1.0.0-alpha1'

from .Exceptions import ApiCode, ClientIsRunning, ClientIsNotRunning, ClientAlreadyInitialised, NoClient, \
InvalidType, InvalidLoginData, LoginNotDone, NoneToken, RequestNotDone, InvalidTimeFormat, \
InvalidSeasonFormat, Missing, MISSING, PyClasherException
Expand Down
10 changes: 8 additions & 2 deletions pyclasher/bulk_requests/BulkPlayer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from asyncio import get_running_loop, run

from Exceptions import MISSING
from models import Clan
from .BulkRequestModel import BulkRequestModel
from ..models import BaseClan
from ..requests import PlayerRequest, ClanMembersRequest
Expand All @@ -20,8 +22,12 @@ def tags(self):

@classmethod
async def _async_from_clan(cls, clan):
members = await (ClanMembersRequest(clan.tag).request() if isinstance(clan, BaseClan) else
await ClanMembersRequest(clan).request())
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()
else:
members = await ClanMembersRequest(clan).request()
return cls.from_member_list(members)

@classmethod
Expand Down
9 changes: 8 additions & 1 deletion pyclasher/bulk_requests/BulkRequestModel.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from asyncio import gather, get_running_loop, run

from Exceptions import RequestNotDone


class BulkRequestModel:
_request_model = ...
Expand Down Expand Up @@ -34,7 +36,12 @@ def __len__(self):
return len(self._requests)

def __getitem__(self, item):
return self._requests[item]
self._requests[0].to_dict() # test if the `to_dict()` method raises `RequestNotDone`
if isinstance(item, int):
return self._requests[item]
if isinstance(item, slice):
return (self._requests[i] for i in range(*item.indices(len(self._requests))))
raise NotImplementedError

def __iter__(self):
self._iter = iter(self._requests)
Expand Down
4 changes: 2 additions & 2 deletions pyclasher/bulk_requests/BulkRequestModel.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Coroutine, Iterator
from typing import Any, Coroutine, Iterator, Generator


class BulkRequestModel:
Expand Down Expand Up @@ -77,7 +77,7 @@ class BulkRequestModel:
def __len__(self) -> int:
...

def __getitem__(self, item: int) -> _request_model:
def __getitem__(self, item: int | slice) -> Generator | _request_model:
...

def __iter__(self) -> Iterator[_request_model]:
Expand Down
3 changes: 2 additions & 1 deletion pyclasher/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ async def _request(self, future, url, method, body, status, error):

future.set_result(response_json)
status.set_result(response.status)
error.set_result(None if response.status == 200 else ApiCodes.from_exception(response.status, response_json))
error.set_result(None if response.status == 200 else
ApiCodes.from_exception(response.status, response_json))
return

async def consume(self):
Expand Down
19 changes: 16 additions & 3 deletions pyclasher/models/BaseModels.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ class BaseModel:
_main_attribute = None
_data = MISSING

def __new__(cls, data):
def __new__(cls, data=None):
if data is MISSING:
return MISSING
return super().__new__(cls)

def __init__(self, data):
def __init__(self, data=None):
if data is not None:
self._data = data
return
Expand Down Expand Up @@ -55,6 +55,11 @@ class IterBaseModel:
_main_attribute = None
_iter_rtype = Any

def __new__(cls, data):
if data is MISSING:
return MISSING
return super().__new__(cls)

def __init__(self, data):
self._data = data
if self._data is not None and self._data is not MISSING:
Expand All @@ -69,7 +74,15 @@ def __len__(self):
return self._len

def __getitem__(self, item):
return self._iter_rtype(self._data[item])
if self._data is MISSING:
raise RequestNotDone
if self._data is None:
return None
if isinstance(item, int):
return self._iter_rtype(self._data[item])
if isinstance(item, slice):
return (self._iter_rtype(self._data[i]) for i in range(*item.indices(len(self._data))))
raise NotImplementedError

def __iter__(self):
self._iter = iter(self._data)
Expand Down
4 changes: 2 additions & 2 deletions pyclasher/models/BaseModels.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
base models for this API wrapper client
"""

from typing import Any, Iterator
from typing import Any, Iterator, Generator

from ..Exceptions import MISSING, Missing

Expand Down Expand Up @@ -123,7 +123,7 @@ class IterBaseModel:
def __len__(self) -> int:
...

def __getitem__(self, item: int) -> _iter_rtype:
def __getitem__(self, item: int | slice) -> MISSING | Generator | _iter_rtype:
...

def __iter__(self) -> Iterator[_iter_rtype]:
Expand Down
14 changes: 7 additions & 7 deletions pyclasher/models/Clan.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ class Clan(BaseClan):
...

@property
def war_wins(self) -> int:
def war_wins(self) -> Missing | int:
"""
clan's war wins

Expand All @@ -220,7 +220,7 @@ class Clan(BaseClan):
...

@property
def war_ties(self) -> int:
def war_ties(self) -> Missing | int:
"""
clan's war ties

Expand All @@ -230,7 +230,7 @@ class Clan(BaseClan):
...

@property
def war_losses(self) -> int:
def war_losses(self) -> Missing | int:
"""
clan's war losses

Expand All @@ -250,7 +250,7 @@ class Clan(BaseClan):
...

@property
def chat_language(self) -> Language:
def chat_language(self) -> Missing | Language:
"""
clan's chat language

Expand Down Expand Up @@ -290,7 +290,7 @@ class Clan(BaseClan):
...

@property
def location(self) -> Location:
def location(self) -> Missing | Location:
"""
clan's location

Expand Down Expand Up @@ -320,7 +320,7 @@ class Clan(BaseClan):
...

@property
def description(self) -> str:
def description(self) -> Missing | str:
"""
clan's description

Expand All @@ -330,7 +330,7 @@ class Clan(BaseClan):
...

@property
def clan_capital(self) -> ClanCapital:
def clan_capital(self) -> Missing | ClanCapital:
"""
clan's capital

Expand Down
3 changes: 2 additions & 1 deletion pyclasher/models/ClanCapitalRaidSeasons.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Iterator

from ..Exceptions import Missing
from .BaseModels import BaseModel, IterBaseModel, BaseClanMember, BaseClan, Time


Expand Down Expand Up @@ -526,7 +527,7 @@ class ClanCapitalRaidSeason(BaseModel):
...

@property
def members(self) -> ClanCapitalRaidSeasonMemberList:
def members(self) -> Missing | ClanCapitalRaidSeasonMemberList:
"""
member list

Expand Down
3 changes: 2 additions & 1 deletion pyclasher/models/ClanMember.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from ..Exceptions import Missing
from .BaseModels import BaseClanMember
from .Enums import ClanRole
from .Leagues import League, BuilderBaseLeague
Expand Down Expand Up @@ -110,7 +111,7 @@ class ClanMember(BaseClanMember):
...

@property
def player_house(self) -> PlayerHouse:
def player_house(self) -> Missing | PlayerHouse:
"""
player house

Expand Down
1 change: 1 addition & 0 deletions pyclasher/models/Enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,7 @@ class ClanWarResult(Enum):
LOSE = "lose"
WIN = "win"
TIE = "tie"
NONE = None


class ClanRole(Enum):
Expand Down
3 changes: 2 additions & 1 deletion pyclasher/models/Enums.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ApiCodes(Enum):
def from_exception(cls,
code: int,
response_json: dict
) -> ApiCode:
) -> ApiCodes:
"""
class method that allows to initialise ApiCodes with a failed request response

Expand Down Expand Up @@ -510,6 +510,7 @@ class ClanWarResult(Enum):
LOSE = "lose"
WIN = "win"
TIE = "tie"
NONE = None


class ClanRole(Enum):
Expand Down
8 changes: 0 additions & 8 deletions pyclasher/models/Player.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,6 @@ def attack_wins(self):
def defense_wins(self):
return self._get_data('defenseWins')

@property
def versus_trophies(self):
return self._get_data('versusTrophies')

@property
def best_versus_trophies(self):
return self._get_data('bestVersusTrophies')

@property
def town_hall_level(self):
return self._get_data('townHallLevel')
Expand Down
11 changes: 2 additions & 9 deletions pyclasher/models/Player.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Iterator

from ..Exceptions import Missing
from .BaseModels import BaseModel, IterBaseModel, BaseClan
from .Enums import ClanRole, WarPreference, Village
from .Labels import LabelList
Expand Down Expand Up @@ -175,7 +176,7 @@ class PlayerAchievementProgressList(IterBaseModel):

class Player(BaseModel):
@property
def league(self) -> League:
def league(self) -> Missing | League:
...

@property
Expand All @@ -202,14 +203,6 @@ class Player(BaseModel):
def defense_wins(self) -> int:
...

@property
def versus_trophies(self) -> int:
...

@property
def best_versus_trophies(self) -> int:
...

@property
def town_hall_level(self) -> int:
...
Expand Down
2 changes: 1 addition & 1 deletion pyclasher/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from .ClanWarLog import ClanWarLogEntry, ClanWarLog
from .Enums import ApiCodes, ClanType, WarFrequency, Locations, Leagues, CapitalLeagues, \
BuilderBaseLeagues, WarLeagues, Labels, Languages, ClanWarState, ClanWarLeagueGroupState, \
ClanWarResult, WarPreference, PlayerHouseElementType, Village, TokenStatus
ClanWarResult, WarPreference, PlayerHouseElementType, Village, TokenStatus, ClanRole
# gold pass season
from .GoldPassSeason import GoldPassSeason
# labels
Expand Down
3 changes: 3 additions & 0 deletions pyclasher/requests/ClanLabels.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ class ClanLabelsRequest(IterRequestModel):
_iter_rtype = Label
_list_rtype = LabelList

def __init__(self) -> None:
...

@property
def items(self) -> _list_rtype:
...
Expand Down
6 changes: 3 additions & 3 deletions pyclasher/requests/ClanSearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ClanSearchRequest(IterRequestModel):
change in the future releases of the API.
"""

name = None
clan_name = None
_iter_rtype = Clan
_list_rtype = ClanList

Expand Down Expand Up @@ -48,7 +48,7 @@ def __init__(self, name=None, war_frequency=None, location=None, min_members=Non
:type before: str
"""

self.name = name
self.clan_name = name
IterRequestModel.__init__(self, "clans",
kwargs={
'name': name,
Expand All @@ -60,5 +60,5 @@ def __init__(self, name=None, war_frequency=None, location=None, min_members=Non
'minClanLevel': min_clan_level,
'labelIds': ",".join((label.value.id for label in label_ids)) if label_ids is not None else None,
'limit': limit, 'after': after, 'before': before})
self._main_attribute = self.name
self._main_attribute = self.clan_name
return
Loading