lol_game_client_api/
api.rs1use crate::api;
5use crate::model::{Abilities, ActivePlayer, AllGameData, Events, FullRunes, GameData, Player};
6use reqwest::{Certificate, IntoUrl};
7use serde::Deserialize;
8use thiserror::Error;
9
10pub struct GameClient {
11 client: reqwest::Client,
12}
13
14#[cold]
17pub fn get_riot_root_certificate() -> Certificate {
18 Certificate::from_pem(include_bytes!("riotgames.cer")).unwrap()
19}
20
21#[derive(Debug, Error)]
22pub enum QueryError {
23 #[error("Failed to query the API. Is the game running ? '{}'", _0)]
24 Reqwest(#[from] reqwest::Error), }
26
27impl Default for GameClient {
28 fn default() -> Self {
29 Self::new()
30 }
31}
32
33impl GameClient {
34 pub fn new() -> Self {
37 GameClient::_from_certificate(get_riot_root_certificate()).unwrap()
38 }
39
40 pub fn _from_certificate(certificate: Certificate) -> Result<Self, QueryError> {
43 Ok(GameClient {
44 client: reqwest::ClientBuilder::new()
45 .add_root_certificate(certificate)
46 .build()?,
47 })
48 }
49
50 async fn get_data<T: for<'de> Deserialize<'de>, U: IntoUrl>(
52 &self,
53 endpoint: U,
54 ) -> Result<T, QueryError> {
55 let data = self.client.get(endpoint).send().await?.json::<T>().await?;
56 Ok(data)
57 }
58}
59
60impl GameClient {
62 pub async fn all_game_data(&self) -> Result<AllGameData, QueryError> {
64 self.get_data(api!("allgamedata")).await
65 }
66
67 pub async fn active_player(&self) -> Result<ActivePlayer, QueryError> {
69 self.get_data(api!("activeplayer")).await
70 }
71
72 pub async fn active_player_name(&self) -> Result<String, QueryError> {
74 self.get_data(api!("activeplayername")).await
75 }
76
77 pub async fn active_player_abilities(&self) -> Result<Abilities, QueryError> {
79 self.get_data(api!("activeplayerabilities")).await
80 }
81
82 pub async fn active_player_runes(&self) -> Result<FullRunes, QueryError> {
84 self.get_data(api!("activeplayerrunes")).await
85 }
86
87 pub async fn player_list(&self) -> Result<Vec<Player>, QueryError> {
89 self.get_data(api!("playerlist")).await
90 }
91
92 pub async fn event_data(&self) -> Result<Events, QueryError> {
96 self.get_data(api!("eventdata")).await
97 }
98
99 pub async fn game_stats(&self) -> Result<GameData, QueryError> {
101 self.get_data(api!("gamestats")).await
102 }
103}
104
105#[macro_export]
106macro_rules! api {
107 ($endpoint:expr) => {
108 concat!("https://127.0.0.1:2999/liveclientdata/", $endpoint)
109 };
110}