ggapi/
lib.rs

1
2//! Communicate with start.gg's API in a fast, type-safe manner with little hassle.
3
4use gql_client::{
5    Client,
6    ClientConfig,
7};
8
9use std::collections::HashMap;
10use serde::Serialize;
11
12pub mod enums;
13pub use enums::*;
14
15pub mod structs;
16pub use structs::*;
17
18/// Variables for a query.
19#[derive(Serialize)]
20pub struct Vars {
21    pub id: GGID,
22    pub slug: String,
23    pub page: u32,
24    pub per_page: u32,
25}
26
27/// Execute a query.
28///
29/// When given a token, query, and a set of variables, this function will execute a query and return a deserialized object.
30pub async fn execute_query(
31    token: &str,
32    query: &str,
33    vars: Vars,
34) -> GGResponse {
35
36    let mut headers = HashMap::new();
37    headers.insert("authorization".to_string(), format!("Bearer {}", token));
38
39    let config = ClientConfig {
40        endpoint: "https://api.start.gg/gql/alpha".to_string(),
41        timeout: Some(60),
42        headers: Some(headers),
43        proxy: None,
44    };
45
46    let client = Client::new_with_config(config);
47    let data = client.query_with_vars::<GGResponse, Vars>(query, vars).await;
48    let response: GGResponse;
49    match data {
50        Ok(data) => response = data.unwrap(),
51        Err(e) => {
52            response = GGResponse::Error(String::from(e.message()));
53        },
54    }
55    return response;
56}
57
58/// Get some basic tournament information.
59///
60/// Returns the tournament id, name, slug, short slug, as well as a list of events, phases within those events, and all of the phase groups in each phase.
61pub async fn get_tournament_info(
62    slug: &str,
63    token: &str,
64) -> GGResponse {
65
66    let query = r#"
67    query GetTournamentInfo($slug: String!) {
68        tournament(slug: $slug) {
69            id
70            name
71            slug
72            shortSlug
73            startAt
74            events {
75                id
76                name
77                phases {
78                    id
79                    name
80                    phaseGroups(query: { page: 1, perPage: 100 }) {
81                        nodes {
82                            id
83                            displayIdentifier
84                        }
85                    }
86                }
87                slug
88            }
89        }
90    }
91    "#;
92
93    let vars = Vars { id: GGID::Int(0), slug: slug.to_string(), page: 1, per_page: 100 };
94
95    return execute_query(token, query, vars).await;
96}
97
98/// Get events from a tournament.
99///
100/// Returns a list of events within a tournament.
101pub async fn get_events_from_tournament(
102    id: GGID,
103    token: &str,
104) -> GGResponse {
105
106    let query = r#"
107    query GetEvents($id: ID!) {
108        tournament(id: $id) {
109            id
110            events {
111                id
112                name
113                slug
114            }
115        }
116    }
117    "#;
118
119    let vars = Vars { id: id, slug: "".to_string(), page: 1, per_page: 100 };
120
121    return execute_query(token, query, vars).await;
122}
123
124/// Get phases from an event.
125///
126/// Returns a list of phases within a event.
127pub async fn get_phases_from_event(
128    id: GGID,
129    token: &str,
130) -> GGResponse {
131
132    let query = r#"
133    query GetPhases($id: ID!) {
134        event(id: $id) {
135            phases {
136                id
137                name
138            }
139        }
140    }
141    "#;
142
143    let vars = Vars { id: id, slug: "".to_string(), page: 1, per_page: 100 };
144
145    return execute_query(token, query, vars).await;
146}
147
148/// Get phase groups from a phase.
149///
150/// Returns a list of phase groups within a phase.
151pub async fn get_phase_groups_from_phase(
152    id: GGID,
153    token: &str,
154) -> GGResponse {
155
156    let query = r#"
157    query GetPhaseGroups($id: ID!) {
158        phase(id: $id) {
159            phaseGroups(query: { page: 1, perPage: 100 }) {
160                nodes {
161                    id
162                    displayIdentifier
163                }
164            }
165        }
166    }
167    "#;
168
169    let vars = Vars { id: id, slug: "".to_string(), page: 1, per_page: 100 };
170
171    return execute_query(token, query, vars).await;
172}
173
174/// Get all of the sets in a given phase group.
175///
176/// Returns a list of sets including the set name and entrants.
177pub async fn get_sets_from_phase_group(
178    id: GGID,
179    token: &str,
180) -> GGResponse {
181
182    let query = r#"
183    query PhaseGroupSets($id: ID!){
184        phaseGroup(id: $id){
185            sets(page: 1, perPage: 100, sortType: STANDARD) {
186                nodes {
187                    id
188                    fullRoundText
189                    identifier
190                    slots {
191                        entrant {
192                            id
193                            name
194                        }
195                    }
196                }
197            }
198        }
199    }
200    "#;
201
202    let vars = Vars { id: id, slug: "".to_string(), page: 1, per_page: 100 };
203
204    return execute_query(token, query, vars).await;
205}
206
207/// Get specific set and various information about it.
208///
209/// Returns a set including the set name, entrants, and all of the participant information.
210pub async fn get_entrants_from_set(
211    id: GGID,
212    token: &str,
213) -> GGResponse {
214
215    let query = r#"
216    query SetEntrants($id: ID!){
217        set(id: $id){
218            id
219            event {
220                name
221            }
222            fullRoundText
223            identifier
224            slots {
225                standing {
226                    stats {
227                        score {
228                            label
229                            value
230                        }
231                    }
232                }
233                entrant {
234                    id
235                    name
236                    participants {
237                        id
238                        gamerTag
239                        user {
240                            discriminator
241                            name
242                        }
243                    }
244                }
245            }
246        }
247    }
248    "#;
249
250    let vars = Vars { id: id, slug: "".to_string(), page: 1, per_page: 100 };
251
252    return execute_query(token, query, vars).await;
253}
254
255/// Get information about a specific entrant.
256///
257/// Returns various important information regarding an entrant, including the name, tag and discriminator of each participant.
258pub async fn get_entrant_info(
259    id: GGID,
260    token: &str,
261) -> GGResponse {
262
263    let query = r#"
264    query EntrantInfo($id: ID!) {
265        entrant(id: $id) {
266            id
267            name
268            participants {
269                id
270                gamerTag
271                user {
272                    discriminator
273                    name
274                }
275            }
276        }
277    }
278    "#;
279
280    let vars = Vars { id: id, slug: "".to_string(), page: 1, per_page: 100 };
281
282    return execute_query(token, query, vars).await;
283}
284
285#[cfg(test)]
286mod tests {
287    use super::*;
288}