Skip to main content

licoricedev/api/
misc.rs

1use crate::client::{Lichess, LichessResult};
2use crate::models::{game::Eval, tournament::Simuls};
3use serde_json::{from_value, Value};
4
5impl Lichess {
6    pub async fn get_current_simuls(&self) -> LichessResult<Simuls> {
7        let url = format!("{}/api/simul", self.base);
8        let builder = self.client.get(&url);
9        self.to_model_full(builder).await
10    }
11
12    pub async fn study_chapter_pgn(
13        &self,
14        study_id: &str,
15        chapter_id: &str,
16        query_params: Option<&[(&str, &str)]>,
17    ) -> LichessResult<String> {
18        let url = format!("{}/study/{}/{}.pgn", self.base, study_id, chapter_id);
19        let mut builder = self.client.get(&url);
20        if let Some(params) = query_params {
21            builder = builder.query(&params)
22        }
23        self.to_raw_str(builder).await
24    }
25
26    pub async fn study_full_pgn(
27        &self,
28        study_id: &str,
29        query_params: Option<&[(&str, &str)]>,
30    ) -> LichessResult<String> {
31        let url = format!("{}/study/{}.pgn", self.base, study_id);
32        let mut builder = self.client.get(&url);
33        if let Some(params) = query_params {
34            builder = builder.query(&params)
35        }
36        self.to_raw_str(builder).await
37    }
38
39    pub async fn mesage(&self, recipient: &str, message: &str) -> LichessResult<()> {
40        let url = format!("{}/inbox/{}", self.base, recipient);
41        let builder = self.client.post(&url).form(&[("text", message)]);
42        let ok_json = self.to_model_full::<Value>(builder);
43        assert!(from_value::<bool>(ok_json.await?["ok"].take())?);
44        Ok(())
45    }
46
47    pub async fn get_cloud_eval(
48        &self,
49        fen: &str,
50        multi_pv: u8,
51        variant: Option<&str>,
52    ) -> LichessResult<Eval> {
53        let url = format!("{}/api/cloud-eval", self.base);
54        let builder = self.client.get(&url).query(&[
55            ("fen", fen),
56            ("multiPv", &multi_pv.to_string()),
57            ("variant", variant.unwrap_or("standard")),
58        ]);
59        self.to_model_full(builder).await
60    }
61}