splits_io_api/
game.rs

1//! The game module handles retrieving Games. A Game is a collection of information about a game and
2//! may contain Categories.
3//!
4//! [API Documentation](https://github.com/glacials/splits-io/blob/master/docs/api.md#game)
5
6use crate::{
7    get_json,
8    wrapper::{ContainsCategories, ContainsGame, ContainsGames, ContainsRunners, ContainsRuns},
9    Category, Client, Error, Game, Run, Runner,
10};
11use reqwest::Url;
12
13impl Game {
14    /// Searches for a Game based on the name of the game.
15    pub async fn search(client: &Client, name: &str) -> Result<Vec<Game>, Error> {
16        self::search(client, name).await
17    }
18
19    /// Gets a Game based on the shortened title of the game.
20    pub async fn get(client: &Client, shortname: &str) -> Result<Game, Error> {
21        self::get(client, shortname).await
22    }
23
24    /// Gets the Categories that belong to the Game based on the shortened title of the game.
25    pub async fn categories(&self, client: &Client) -> Result<Vec<Category>, Error> {
26        get_categories(
27            client,
28            self.shortname
29                .as_deref()
30                .ok_or(Error::UnidentifiableResource)?,
31        )
32        .await
33    }
34
35    /// Gets the Runs that belong to the Game based on the shortened title of the game.
36    pub async fn runs(&self, client: &Client) -> Result<Vec<Run>, Error> {
37        get_runs(
38            client,
39            self.shortname
40                .as_deref()
41                .ok_or(Error::UnidentifiableResource)?,
42        )
43        .await
44    }
45
46    /// Gets the Runners that belong to the Game based on the shortened title of the game.
47    pub async fn runners(&self, client: &Client) -> Result<Vec<Runner>, Error> {
48        get_runners(
49            client,
50            self.shortname
51                .as_deref()
52                .ok_or(Error::UnidentifiableResource)?,
53        )
54        .await
55    }
56}
57
58/// Searches for a Game based on the name of the game.
59pub async fn search(client: &Client, name: &str) -> Result<Vec<Game>, Error> {
60    let mut url = Url::parse("https://splits.io/api/v4/games").unwrap();
61    url.query_pairs_mut().append_pair("search", name);
62
63    let ContainsGames { games } = get_json(client, client.client.get(url)).await?;
64
65    Ok(games)
66}
67
68/// Gets a Game based on the shortened title of the game.
69pub async fn get(client: &Client, shortname: &str) -> Result<Game, Error> {
70    let mut url = Url::parse("https://splits.io/api/v4/games").unwrap();
71    url.path_segments_mut().unwrap().push(shortname);
72
73    let ContainsGame { game } = get_json(client, client.client.get(url)).await?;
74
75    Ok(game)
76}
77
78/// Gets the Categories that belong to a Game based on the shortened title of the game.
79pub async fn get_categories(client: &Client, shortname: &str) -> Result<Vec<Category>, Error> {
80    let mut url = Url::parse("https://splits.io/api/v4/games").unwrap();
81    url.path_segments_mut()
82        .unwrap()
83        .extend(&[shortname, "categories"]);
84
85    let ContainsCategories { categories } = get_json(client, client.client.get(url)).await?;
86
87    Ok(categories)
88}
89
90/// Gets the Runs that belong to a Game based on the shortened title of the game.
91pub async fn get_runs(client: &Client, shortname: &str) -> Result<Vec<Run>, Error> {
92    let mut url = Url::parse("https://splits.io/api/v4/games").unwrap();
93    url.path_segments_mut()
94        .unwrap()
95        .extend(&[shortname, "runs"]);
96
97    let ContainsRuns { runs } = get_json(client, client.client.get(url)).await?;
98
99    Ok(runs)
100}
101
102/// Gets the Runners that belong to a Game based on the shortened title of the game.
103pub async fn get_runners(client: &Client, shortname: &str) -> Result<Vec<Runner>, Error> {
104    let mut url = Url::parse("https://splits.io/api/v4/games").unwrap();
105    url.path_segments_mut()
106        .unwrap()
107        .extend(&[shortname, "runners"]);
108
109    let ContainsRunners { runners } = get_json(client, client.client.get(url)).await?;
110
111    Ok(runners)
112}