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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//! The game module handles retrieving Games. A Game is a collection of information about a game and
//! may contain Categories.
//!
//! [API Documentation](https://github.com/glacials/splits-io/blob/master/docs/api.md#game)

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

impl Game {
    /// Searches for a Game based on the name of the game.
    pub async fn search(client: &Client, name: &str) -> Result<Vec<Game>, Error> {
        self::search(client, name).await
    }

    /// Gets a Game based on the shortened title of the game.
    pub async fn get(client: &Client, shortname: &str) -> Result<Game, Error> {
        self::get(client, shortname).await
    }

    /// Gets the Categories that belong to the Game based on the shortened title of the game.
    pub async fn categories(&self, client: &Client) -> Result<Vec<Category>, Error> {
        get_categories(
            client,
            &self.shortname.as_ref().context(UnidentifiableResource)?,
        )
        .await
    }

    /// Gets the Runs that belong to the Game based on the shortened title of the game.
    pub async fn runs(&self, client: &Client) -> Result<Vec<Run>, Error> {
        get_runs(
            client,
            &self.shortname.as_ref().context(UnidentifiableResource)?,
        )
        .await
    }

    /// Gets the Runners that belong to the Game based on the shortened title of the game.
    pub async fn runners(&self, client: &Client) -> Result<Vec<Runner>, Error> {
        get_runners(
            client,
            &self.shortname.as_ref().context(UnidentifiableResource)?,
        )
        .await
    }
}

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

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

    Ok(games)
}

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

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

    Ok(game)
}

/// Gets the Categories that belong to a Game based on the shortened title of the game.
pub async fn get_categories(client: &Client, shortname: &str) -> Result<Vec<Category>, Error> {
    let mut url = Url::parse("https://splits.io/api/v4/games").unwrap();
    url.path_segments_mut()
        .unwrap()
        .extend(&[shortname, "categories"]);

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

    Ok(categories)
}

/// Gets the Runs that belong to a Game based on the shortened title of the game.
pub async fn get_runs(client: &Client, shortname: &str) -> Result<Vec<Run>, Error> {
    let mut url = Url::parse("https://splits.io/api/v4/games").unwrap();
    url.path_segments_mut()
        .unwrap()
        .extend(&[shortname, "runs"]);

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

    Ok(runs)
}

/// Gets the Runners that belong to a Game based on the shortened title of the game.
pub async fn get_runners(client: &Client, shortname: &str) -> Result<Vec<Runner>, Error> {
    let mut url = Url::parse("https://splits.io/api/v4/games").unwrap();
    url.path_segments_mut()
        .unwrap()
        .extend(&[shortname, "runners"]);

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

    Ok(runners)
}