Skip to content

Commit

Permalink
ChallengeSentType no longer needed (#992)
Browse files Browse the repository at this point in the history
The Lichess API was updated so that the JSON response to creating a
challenge is no longer nested under a single `challenge` heading. All
information has been moved to the top level. This commit aligns
lichess-bot with the API change.

Closes #991.
  • Loading branch information
MarkZH authored Jul 22, 2024
1 parent 067bff6 commit 38ab296
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 14 deletions.
10 changes: 5 additions & 5 deletions lib/lichess.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from typing import Optional, Union, cast
import chess.engine
from lib.types import (UserProfileType, REQUESTS_PAYLOAD_TYPE, GameType, PublicDataType, OnlineType,
ChallengeSentType, TOKEN_TESTS_TYPE, BackoffDetails)
ChallengeType, TOKEN_TESTS_TYPE, BackoffDetails)


ENDPOINTS = {
Expand Down Expand Up @@ -199,7 +199,7 @@ def api_post(self,
headers: Optional[dict[str, str]] = None,
params: Optional[dict[str, str]] = None,
payload: Optional[REQUESTS_PAYLOAD_TYPE] = None,
raise_for_status: bool = True) -> Union[ChallengeSentType, Optional[TOKEN_TESTS_TYPE]]:
raise_for_status: bool = True) -> Union[ChallengeType, Optional[TOKEN_TESTS_TYPE]]:
"""
Send a POST to lichess.org.
Expand All @@ -223,7 +223,7 @@ def api_post(self,
if raise_for_status:
response.raise_for_status()

json_response: Union[ChallengeSentType, Optional[TOKEN_TESTS_TYPE]] = response.json()
json_response: Union[ChallengeType, Optional[TOKEN_TESTS_TYPE]] = response.json()
return json_response

def get_path_template(self, endpoint_name: str) -> str:
Expand Down Expand Up @@ -366,9 +366,9 @@ def get_online_bots(self) -> list[UserProfileType]:
except Exception:
return []

def challenge(self, username: str, payload: REQUESTS_PAYLOAD_TYPE) -> ChallengeSentType:
def challenge(self, username: str, payload: REQUESTS_PAYLOAD_TYPE) -> ChallengeType:
"""Create a challenge."""
return cast(ChallengeSentType,
return cast(ChallengeType,
self.api_post("challenge", username, payload=payload, raise_for_status=False))

def cancel(self, challenge_id: str) -> None:
Expand Down
2 changes: 1 addition & 1 deletion lib/matchmaking.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def create_challenge(self, username: str, base_time: int, increment: int, days:
self.update_daily_challenge_record()
self.last_challenge_created_delay.reset()
response = self.li.challenge(username, params)
challenge_id: str = response.get("challenge", {}).get("id", "")
challenge_id: str = response.get("id", "")
if not challenge_id:
logger.error(response)
self.add_to_block_list(username)
Expand Down
6 changes: 0 additions & 6 deletions lib/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,6 @@ class ChallengeType(TypedDict, total=False):
initialFen: str


class ChallengeSentType(TypedDict, total=False):
"""Type hint for challenge sent."""

challenge: ChallengeType


class EventType(TypedDict, total=False):
"""Type hint for event."""

Expand Down
4 changes: 2 additions & 2 deletions test_bot/lichess.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from queue import Queue
from typing import Union, Optional, Generator
from lib.timer import to_msec
from lib.types import (UserProfileType, ChallengeSentType, REQUESTS_PAYLOAD_TYPE, GameType, OnlineType, PublicDataType,
from lib.types import (UserProfileType, ChallengeType, REQUESTS_PAYLOAD_TYPE, GameType, OnlineType, PublicDataType,
BackoffDetails)


Expand Down Expand Up @@ -228,7 +228,7 @@ def get_online_bots(self) -> list[UserProfileType]:
"""Return that the only bot online is us."""
return [{"username": "b", "online": True}]

def challenge(self, username: str, payload: REQUESTS_PAYLOAD_TYPE) -> ChallengeSentType:
def challenge(self, username: str, payload: REQUESTS_PAYLOAD_TYPE) -> ChallengeType:
"""Isn't used in tests."""
return {}

Expand Down

0 comments on commit 38ab296

Please sign in to comment.