1use 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 pub async fn search(client: &Client, name: &str) -> Result<Vec<Game>, Error> {
16 self::search(client, name).await
17 }
18
19 pub async fn get(client: &Client, shortname: &str) -> Result<Game, Error> {
21 self::get(client, shortname).await
22 }
23
24 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 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 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
58pub 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
68pub 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
78pub 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
90pub 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
102pub 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}