Skip to main content

lichess_api/api/
games.rs

1//! Fetching, exporting, streaming, and importing games.
2//!
3//! Games can be exported as [`GameJson`] (single game or user history) or as
4//! raw PGN text, and requests for many games at once return a stream of
5//! results rather than a single value, since the response can be very large.
6//! [`stream_games_of_users`](LichessApi::stream_games_of_users) and
7//! [`stream_games_by_ids`](LichessApi::stream_games_by_ids) instead stream
8//! [`GameStream`] events for games as they start and finish, in real time.
9//!
10//! Most exports work without a token, but return more detail when
11//! authenticated, and downloading your own games is rate limited more
12//! generously than anonymous or third-party requests. Importing a game and
13//! bookmarking a game both require an authenticated user.
14
15use futures::stream::StreamExt;
16
17use crate::client::LichessApi;
18use crate::error::Result;
19use crate::model::games::*;
20
21impl LichessApi<reqwest::Client> {
22    pub async fn export_one_game(
23        &self,
24        request: impl Into<export::one::GetRequest>,
25    ) -> Result<GameJson> {
26        self.get_single_model(request.into()).await
27    }
28
29    pub async fn export_ongoing_game(
30        &self,
31        request: impl Into<export::ongoing::GetRequest>,
32    ) -> Result<GameJson> {
33        self.get_single_model(request.into()).await
34    }
35
36    pub async fn export_games_of_user(
37        &self,
38        request: impl Into<export::by_user::GetRequest>,
39    ) -> Result<impl StreamExt<Item = Result<GameJson>>> {
40        self.get_streamed_models(request.into()).await
41    }
42
43    pub async fn export_games_by_ids(
44        &self,
45        request: impl Into<export::by_ids::PostRequest>,
46    ) -> Result<impl StreamExt<Item = Result<GameJson>>> {
47        self.get_streamed_models(request.into()).await
48    }
49
50    pub async fn stream_games_of_users(
51        &self,
52        request: impl Into<stream::by_users::PostRequest>,
53    ) -> Result<impl StreamExt<Item = Result<GameStream>>> {
54        self.get_streamed_models(request.into()).await
55    }
56
57    pub async fn stream_games_by_ids(
58        &self,
59        request: impl Into<stream::by_ids::PostRequest>,
60    ) -> Result<impl StreamExt<Item = Result<GameStream>>> {
61        self.get_streamed_models(request.into()).await
62    }
63
64    pub async fn add_game_ids_to_stream(
65        &self,
66        request: impl Into<stream::add_ids::PostRequest>,
67    ) -> Result<bool> {
68        self.get_ok(request.into()).await
69    }
70
71    pub async fn get_my_ongoing_games(
72        &self,
73        request: impl Into<ongoing::GetRequest>,
74    ) -> Result<ongoing::Games> {
75        self.get_single_model(request.into()).await
76    }
77
78    pub async fn stream_game_moves(
79        &self,
80        request: impl Into<stream::moves::GetRequest>,
81    ) -> Result<impl StreamExt<Item = Result<stream::moves::Move>>> {
82        self.get_streamed_models(request.into()).await
83    }
84
85    pub async fn import_game(
86        &self,
87        request: impl Into<import::PostRequest>,
88    ) -> Result<import::ImportData> {
89        self.get_single_model(request.into()).await
90    }
91
92    pub async fn export_bookmarked_games(
93        &self,
94        request: impl Into<export::bookmarks::GetRequest>,
95    ) -> Result<impl StreamExt<Item = Result<GameJson>>> {
96        self.get_streamed_models(request.into()).await
97    }
98
99    pub async fn export_imported_games(&self) -> Result<impl StreamExt<Item = Result<String>>> {
100        self.get_pgn(export::imports::GetRequest::new()).await
101    }
102
103    pub async fn get_game_chat(
104        &self,
105        request: impl Into<chat::GetRequest>,
106    ) -> Result<impl StreamExt<Item = Result<chat::ChatLine>>> {
107        self.get_streamed_models(request.into()).await
108    }
109
110    pub async fn bookmark_game(&self, request: impl Into<bookmark::PostRequest>) -> Result<()> {
111        self.get_empty(request.into()).await
112    }
113}