splits_io_api/
runner.rs

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