Skip to main content

lichess_api/api/
challenges.rs

1//! Challenge other players, bots, or the Lichess AI to a game.
2//!
3//! Covers listing your incoming and outgoing challenges, creating, accepting,
4//! declining, and canceling a challenge, creating an open challenge anyone can
5//! join, and starting a game against the Lichess AI. Most operations require a
6//! bearer token with a `challenge:*` scope; open challenges and public reads
7//! are the exceptions. [`LichessApi::admin_challenge_tokens`] is restricted to
8//! Lichess administrators.
9//!
10//! Request/response types live in [`crate::model::challenges`].
11
12use crate::client::LichessApi;
13use crate::error::Result;
14use crate::model::challenges::*;
15use crate::model::games::stream::moves::Move;
16
17impl LichessApi<reqwest::Client> {
18    pub async fn list_challenges(&self) -> Result<list::Challenges> {
19        self.get_single_model(list::GetRequest::new()).await
20    }
21
22    pub async fn create_challenge(
23        &self,
24        request: impl Into<create::PostRequest>,
25    ) -> Result<ChallengeJson> {
26        self.get_single_model(request.into()).await
27    }
28
29    pub async fn accept_challenge(&self, request: impl Into<accept::PostRequest>) -> Result<bool> {
30        self.get_ok(request.into()).await
31    }
32
33    pub async fn decline_challenge(
34        &self,
35        request: impl Into<decline::PostRequest>,
36    ) -> Result<bool> {
37        self.get_ok(request.into()).await
38    }
39
40    pub async fn cancel_challenge(&self, request: impl Into<cancel::PostRequest>) -> Result<bool> {
41        self.get_ok(request.into()).await
42    }
43
44    pub async fn challenge_ai(&self, request: impl Into<ai::PostRequest>) -> Result<Move> {
45        self.get_single_model(request.into()).await
46    }
47
48    pub async fn create_open_challenge(
49        &self,
50        request: impl Into<open::PostRequest>,
51    ) -> Result<ChallengeOpenJson> {
52        self.get_single_model(request.into()).await
53    }
54
55    pub async fn start_clocks(
56        &self,
57        request: impl Into<start_clocks::PostRequest>,
58    ) -> Result<bool> {
59        self.get_ok(request.into()).await
60    }
61
62    pub async fn add_time_to_opponent_clock(
63        &self,
64        request: impl Into<add_time::PostRequest>,
65    ) -> Result<bool> {
66        self.get_ok(request.into()).await
67    }
68
69    pub async fn show_challenge(
70        &self,
71        request: impl Into<show::GetRequest>,
72    ) -> Result<ChallengeJson> {
73        self.get_single_model(request.into()).await
74    }
75
76    pub async fn admin_challenge_tokens(
77        &self,
78        request: impl Into<admin_challenge::PostRequest>,
79    ) -> Result<AdminChallengeTokenResults> {
80        self.get_single_model(request.into()).await
81    }
82}