Skip to main content

lichess_api/api/
puzzles.rs

1//! Fetching puzzles, tracking puzzle progress, and puzzle racing.
2//!
3//! Covers the daily puzzle, fetching a puzzle by ID or a random next puzzle,
4//! batches of puzzles for offline play, puzzle activity and the puzzle
5//! dashboard, puzzles to replay for a theme, and the Puzzle Storm dashboard.
6//! Also covers creating and joining a puzzle race and fetching its results.
7//!
8//! The daily puzzle, puzzle-by-ID, and Storm dashboard lookups are public.
9//! Everything else needs a bearer token: reading activity, the dashboard,
10//! replays, and fetching puzzles requires a `puzzle:read` scope, solving a
11//! batch requires `puzzle:write`, and creating a race requires `racer:write`.
12//!
13//! Request/response types live in [`crate::model::puzzles`].
14
15use futures::stream::StreamExt;
16
17use crate::client::LichessApi;
18use crate::error::Result;
19use crate::model::puzzles::*;
20
21impl LichessApi<reqwest::Client> {
22    pub async fn get_daily_puzzle(&self) -> Result<daily::Puzzle> {
23        self.get_single_model(daily::GetRequest::new()).await
24    }
25
26    pub async fn get_puzzle(&self, request: impl Into<id::GetRequest>) -> Result<id::Puzzle> {
27        self.get_single_model(request.into()).await
28    }
29
30    pub async fn get_new_puzzle(
31        &self,
32        request: impl Into<next::GetRequest>,
33    ) -> Result<next::Puzzle> {
34        self.get_single_model(request.into()).await
35    }
36
37    pub async fn get_puzzle_activity(
38        &self,
39        request: impl Into<activity::GetRequest>,
40    ) -> Result<impl StreamExt<Item = Result<activity::Activity>>> {
41        self.get_streamed_models(request.into()).await
42    }
43
44    pub async fn get_puzzles_to_replay(
45        &self,
46        request: impl Into<replay::GetRequest>,
47    ) -> Result<replay::Replay> {
48        self.get_single_model(request.into()).await
49    }
50
51    pub async fn get_puzzle_dashboard(
52        &self,
53        request: impl Into<dashboard::GetRequest>,
54    ) -> Result<dashboard::Dashboard> {
55        self.get_single_model(request.into()).await
56    }
57
58    pub async fn get_puzzle_storm_dashboard(
59        &self,
60        request: impl Into<storm_dashboard::GetRequest>,
61    ) -> Result<storm_dashboard::Dashboard> {
62        self.get_single_model(request.into()).await
63    }
64
65    pub async fn make_puzzle_race(
66        &self,
67        request: impl Into<race::PostRequest>,
68    ) -> Result<race::Race> {
69        self.get_single_model(request.into()).await
70    }
71
72    pub async fn get_puzzle_batch(
73        &self,
74        request: impl Into<batch::GetRequest>,
75    ) -> Result<batch::Select> {
76        self.get_single_model(request.into()).await
77    }
78
79    pub async fn solve_puzzle_batch(
80        &self,
81        request: impl Into<batch::PostRequest>,
82    ) -> Result<batch::SolveResponse> {
83        self.get_single_model(request.into()).await
84    }
85
86    pub async fn get_puzzle_race_results(
87        &self,
88        request: impl Into<racer::GetRequest>,
89    ) -> Result<racer::RaceResults> {
90        self.get_single_model(request.into()).await
91    }
92}