1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//! The runner module handles retrieving Runners. A Runner is a user with at least one Run tied to
//! their Splits.io account.
//!
//! [API Documentation](https://github.com/glacials/splits-io/blob/master/docs/api.md#runner)

use crate::platform::Body;
use crate::{
    get_json,
    wrapper::{
        ContainsCategories, ContainsGames, ContainsPBs, ContainsRunner, ContainsRunners,
        ContainsRuns,
    },
    Category, Client, Error, Game, Run, Runner,
};
use http::Request;
use url::Url;

/// Searches for a Runner based on the name of the runner.
pub async fn search(client: &Client, name: &str) -> Result<Vec<Runner>, Error> {
    let mut url = Url::parse("https://splits.io/api/v4/runners").unwrap();
    url.query_pairs_mut().append_pair("search", name);

    let ContainsRunners { runners } = get_json(
        client,
        Request::get(url.as_str()).body(Body::empty()).unwrap(),
    )
    .await?;

    Ok(runners)
}

/// Gets the Runner that is associated with the current user.
pub async fn myself(client: &Client) -> Result<Runner, Error> {
    let ContainsRunner { runner } = get_json(
        client,
        Request::get("https://splits.io/api/v4/runner")
            .body(Body::empty())
            .unwrap(),
    )
    .await?;

    Ok(runner)
}

/// Gets a Runner based on the name of the runner.
pub async fn get(client: &Client, name: &str) -> Result<Runner, Error> {
    let mut url = Url::parse("https://splits.io/api/v4/runners").unwrap();
    url.path_segments_mut().unwrap().push(name);

    let ContainsRunner { runner } = get_json(
        client,
        Request::get(url.as_str()).body(Body::empty()).unwrap(),
    )
    .await?;

    Ok(runner)
}

/// Gets the Runs that are associated with a Runner.
pub async fn get_runs(client: &Client, name: &str) -> Result<Vec<Run>, Error> {
    let mut url = Url::parse("https://splits.io/api/v4/runners").unwrap();
    url.path_segments_mut().unwrap().extend(&[name, "runs"]);

    let ContainsRuns { runs } = get_json(
        client,
        Request::get(url.as_str()).body(Body::empty()).unwrap(),
    )
    .await?;

    Ok(runs)
}

/// Gets the personal best Runs that are associated with a Runner.
pub async fn get_pbs(client: &Client, name: &str) -> Result<Vec<Run>, Error> {
    let mut url = Url::parse("https://splits.io/api/v4/runners").unwrap();
    url.path_segments_mut().unwrap().extend(&[name, "pbs"]);

    let ContainsPBs { pbs } = get_json(
        client,
        Request::get(url.as_str()).body(Body::empty()).unwrap(),
    )
    .await?;

    Ok(pbs)
}

/// Gets the Games that are associated with a Runner.
pub async fn get_games(client: &Client, name: &str) -> Result<Vec<Game>, Error> {
    let mut url = Url::parse("https://splits.io/api/v4/runners").unwrap();
    url.path_segments_mut().unwrap().extend(&[name, "games"]);

    let ContainsGames { games } = get_json(
        client,
        Request::get(url.as_str()).body(Body::empty()).unwrap(),
    )
    .await?;

    Ok(games)
}

/// Gets the Categories that are associated with a Runner.
pub async fn get_categories(client: &Client, name: &str) -> Result<Vec<Category>, Error> {
    let mut url = Url::parse("https://splits.io/api/v4/runners").unwrap();
    url.path_segments_mut()
        .unwrap()
        .extend(&[name, "categories"]);

    let ContainsCategories { categories } = get_json(
        client,
        Request::get(url.as_str()).body(Body::empty()).unwrap(),
    )
    .await?;

    Ok(categories)
}