Skip to main content

pubg_rs/
lib.rs

1use reqwest::Client;
2use structs::{ClanApiResponse, Player, PlayerByID, PlayersApiResponse, SeasonData, SeasonResponseApi};
3
4
5mod structs;
6
7pub struct PubgRs {
8    key: &'static str
9}
10
11impl PubgRs {
12
13    pub fn new(key: impl Into<&'static str>) -> Self {
14        Self { key: key.into() }
15    }
16
17
18    pub async fn get_player(&self, nickname: &str) -> Option<Player> {
19        let client = Client::new();
20    
21        let response = client
22            .get(format!("https://api.pubg.com/shards/steam/players?filter[playerNames]={}", nickname))
23            .header("Authorization", format!("Bearer {}", self.key))
24            .header("Accept", "application/vnd.api+json")
25            .send()
26            .await.unwrap()
27            .json::<PlayersApiResponse>().await;
28    
29    
30            match response {
31                Err(_) => None,
32                Ok(player) => {
33
34                    if player.data.len() >= 1 {
35                        Some(player.data[0].clone())
36                    }
37                    else {None}
38                }
39            }
40    
41    }
42
43
44    pub async fn get_player_by_id(&self, id: &str) -> Option<Player> {
45        let client = Client::new();
46    
47        let response = client
48            .get(format!("https://api.pubg.com/shards/steam/players/{}", id))
49            .header("Authorization", format!("Bearer {}", self.key))
50            .header("Accept", "application/vnd.api+json")
51            .send()
52            .await.unwrap()
53            .json::<PlayerByID>()
54            .await;
55    
56    
57            match response {
58                Err(_) => None,
59                Ok(player) => Some(player.data)
60            }
61    
62    }
63    
64    
65    pub async fn get_clan(&self, id: &str) -> Option<ClanApiResponse> {
66        let client = Client::new();
67    
68        let response = client
69            .get(format!("https://api.pubg.com/shards/steam/clans/{}", id))
70            .header("Authorization", format!("Bearer {}", self.key))
71            .header("Accept", "application/vnd.api+json")
72            .send()
73            .await.unwrap()
74            .json::<ClanApiResponse>()
75            .await;
76    
77    
78            match response {
79                Err(_) => None,
80                Ok(player) => Some(player)
81            }
82    
83    }
84
85    pub async fn get_season_info(&self, player_id: &str, season_id: &str) -> Option<SeasonData> {
86        let client = Client::new();
87    
88        let response = client
89            .get(format!("https://api.pubg.com/shards/steam/players/{}/seasons/{}/ranked", player_id, season_id))
90            .header("Authorization", format!("Bearer {}", self.key))
91            .header("Accept", "application/vnd.api+json")
92            .send()
93            .await.unwrap()
94            
95            .json::<SeasonResponseApi>()
96            .await;
97    
98    
99            match response {
100                Err(err) => {
101                    println!("Erro: {}", err);
102                    None},
103                Ok(player) => Some({
104                    player.data
105                })
106            }
107    
108    }
109    
110}
111
112