Skip to content

Commit

Permalink
add PuzzleRace TypedDict
Browse files Browse the repository at this point in the history
  • Loading branch information
Anupya committed Oct 30, 2023
1 parent 7866727 commit d323d23
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
19 changes: 10 additions & 9 deletions berserk/clients/puzzles.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from __future__ import annotations

from typing import Iterator, Any, Dict
from typing import Iterator, Any, Dict, cast

from .. import models
from ..formats import NDJSON
from .base import BaseClient
from ..types import PuzzleRace


class Puzzles(BaseClient):
Expand All @@ -18,28 +19,28 @@ def get_daily(self) -> Dict[str, Any]:
path = "/api/puzzle/daily"
return self._r.get(path)

def get(self, puzzle_id: str) -> Dict[str, Any]:
def get(self, id: str) -> Dict[str, Any]:
"""Get a puzzle by its id.
:param puzzle_id: the id of the puzzle to retrieve
:param id: the id of the puzzle to retrieve
:return: the puzzle
"""
path = f"/api/puzzle/{puzzle_id}"
path = f"/api/puzzle/{id}"
return self._r.get(path)

def get_puzzle_activity(
self, max_entries: int | None = None, before: int | None = None
self, max: int | None = None, before: int | None = None
) -> Iterator[Dict[str, Any]]:
"""Stream puzzle activity history of the authenticated user, starting with the
most recent activity.
:param max_entries: maximum number of entries to stream. defaults to all activity
:param max: maximum number of entries to stream. defaults to all activity
:param before: timestamp in milliseconds. only stream activity before this time.
defaults to now. use together with max for pagination
:return: iterator over puzzle activity history
"""
path = "/api/puzzle/activity"
params = {"max": max_entries, "before": before}
params = {"max": max, "before": before}
yield from self._r.get(
path,
params=params,
Expand Down Expand Up @@ -69,11 +70,11 @@ def get_storm_dashboard(self, username: str, days: int = 30) -> Dict[str, Any]:
params = {"days": days}
return self._r.get(path, params=params)

def create_race(self) -> Dict[str, str]:
def create_race(self) -> PuzzleRace:
"""Create a new private puzzle race. The Lichess user who creates the race must join the race page,
and manually start the race when enough players have joined.
:return: puzzle race ID and URL
"""
path = "/api/racer"
return self._r.post(path)
return cast(PuzzleRace, self._r.post(path))
3 changes: 2 additions & 1 deletion berserk/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from .account import AccountInformation, Perf, Preferences, Profile, StreamerInfo
from .bulk_pairings import BulkPairing, BulkPairingGame
from .common import ClockConfig, LightUser, OnlineLightUser
from .common import ClockConfig, LightUser, OnlineLightUser, PuzzleRace
from .opening_explorer import (
OpeningExplorerRating,
OpeningExplorerVariant,
Expand All @@ -25,6 +25,7 @@
"Perf",
"Preferences",
"Profile",
"PuzzleRace",
"Speed",
"StreamerInfo",
"Team",
Expand Down
7 changes: 7 additions & 0 deletions berserk/types/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ class ClockConfig(TypedDict):
increment: int


class PuzzleRace(TypedDict):
# Puzzle race ID
id: str
# Puzzle race URL
url: str


Color: TypeAlias = Literal["white", "black"]

GameType: TypeAlias = Literal[
Expand Down

0 comments on commit d323d23

Please sign in to comment.