use reqwest::Method;
use crate::client::LichessClient;
use crate::config::Host;
use crate::error::Result;
use crate::http;
mod export;
mod model;
mod stream;
pub use export::{
CurrentGameRequest, ExportBookmarksRequest, ExportByIdsRequest, GameExportRequest, GameSort,
UserGamesRequest,
};
pub use model::{
LichessGame, LichessGameArenaTour, LichessGameChatMessage, LichessGameClock,
LichessGameDivision, LichessGameMoveAnalysis, LichessGameMoveUpdate, LichessGameOpening,
LichessGamePlayer, LichessGamePlayers, LichessGameStatusName, LichessGameSwissTour,
LichessImportedGame, LichessMoveJudgment, LichessNowPlaying, LichessNowPlayingGame,
LichessNowPlayingOpponent, LichessPlayerAnalysis,
};
#[derive(Debug)]
pub struct GamesApi<'a> {
client: &'a LichessClient,
}
impl<'a> GamesApi<'a> {
pub(crate) fn new(client: &'a LichessClient) -> Self {
Self { client }
}
#[must_use]
pub fn export(&self, game_id: &'a str) -> GameExportRequest<'a> {
GameExportRequest::new(self.client, game_id)
}
#[must_use]
pub fn export_user(&self, username: &'a str) -> UserGamesRequest<'a> {
UserGamesRequest::new(self.client, username)
}
#[must_use]
pub fn current_game(&self, username: &'a str) -> CurrentGameRequest<'a> {
CurrentGameRequest::new(self.client, username)
}
#[must_use]
pub fn export_by_ids(&self, ids: &[&str]) -> ExportByIdsRequest<'a> {
ExportByIdsRequest::new(self.client, ids)
}
#[must_use]
pub fn export_bookmarks(&self) -> ExportBookmarksRequest<'a> {
ExportBookmarksRequest::new(self.client)
}
pub async fn import_game(&self, pgn: &str) -> Result<LichessImportedGame> {
let request = self
.client
.request(Method::POST, Host::Default, "/api/import")
.form(&[("pgn", pgn)]);
http::json(request, "LichessImportedGame").await
}
pub async fn now_playing(&self, nb: Option<u32>) -> Result<LichessNowPlaying> {
let request = self
.client
.request(Method::GET, Host::Default, "/api/account/playing")
.query(&[("nb", nb)]);
http::json(request, "LichessNowPlaying").await
}
pub async fn chat(&self, game_id: &str) -> Result<Vec<LichessGameChatMessage>> {
let path = format!("/api/game/{}/chat", http::segment(game_id));
let request = self.client.request(Method::GET, Host::Default, &path);
http::json(request, "Vec<LichessGameChatMessage>").await
}
}
impl LichessClient {
#[must_use]
pub fn games(&self) -> GamesApi<'_> {
GamesApi::new(self)
}
}