brawl_api/http/routes.rs
1//! Contains the `Route` enum, responsible for listing the available API endpoints and parsing
2//! the given values into a valid URL.
3
4use crate::b_api_concat;
5
6
7/// An enum representing the possible Brawl API routes.
8#[non_exhaustive]
9#[derive(Debug, Clone, Hash, PartialEq, Eq)]
10pub enum Route {
11 /// Route for the `/players/:tag` endpoint. (`tag` must begin with a `#` (`%23`) for correct
12 /// results.)
13 ///
14 /// This fetches a player's data.
15 Player(String),
16
17 /// Route for the `/players/:tag/battlelog` endpoint. (`tag` must begin with a `#` (`%23`) for
18 /// correct results.)
19 ///
20 /// This fetches the player's recently-played battles.
21 PlayerBattlelogs(String),
22
23 /// Route for the `/clubs/:tag` endpoint. (`tag` must begin with a `#` (`%23`) for correct
24 /// results.)
25 ///
26 /// This fetches a club's data.
27 Club(String),
28
29 /// Route for the `/clubs/:tag/members` endpoint.
30 /// (`tag` must begin with a `#` (`%23`) for correct results.)
31 ///
32 /// This fetches a club's members.
33 ClubMembers(String),
34
35 /// Route for the `/rankings/:country_code/players?limit=x` endpoint (shows the top `x` players
36 /// with most trophies in said country code).
37 ///
38 /// The limit can be up to 200. Specifying higher than that simply works the same way as
39 /// specifying 200, thus returning up to 200 entries.
40 PlayerRankings {
41 /// The two-letter country code whose leaderboard should be fetched (e.g. BR for Brazil,
42 /// ZW for Zimbabwe...), or `"global"` for the global leaderboard.
43 country_code: String,
44
45 /// The limit of rankings to get (i.e., to get the top `limit` players, sorted by trophies).
46 limit: u8,
47 },
48
49 /// Route for the `/rankings/:country_code/clubs?limit=x` endpoint.
50 ///
51 /// The limit can be up to 200. Specifying higher than that simply works the same way as
52 /// specifying 200, thus returning up to 200 entries.
53 ClubRankings {
54 /// The two-letter country code whose leaderboard should be fetched (e.g. BR for Brazil,
55 /// ZW for Zimbabwe...), or `"global"` for the global leaderboard.
56 country_code: String,
57
58 /// The limit of rankings to get (i.e., to get the top `limit` clubs, sorted by trophies).
59 limit: u8,
60 },
61
62 /// Route for the `/rankings/:country_code/brawlers/:brawler_id?limit=x` endpoint.
63 ///
64 /// The limit can be up to 200. Specifying higher than that simply works the same way as
65 /// specifying 200, thus returning up to 200 entries.
66 BrawlerRankings {
67 /// The two-letter country code whose leaderboard should be fetched (e.g. BR for Brazil,
68 /// ZW for Zimbabwe...), or `"global"` for the global leaderboard.
69 country_code: String,
70
71 /// The ID of the brawler whose rankings should be fetched. To obtain this,
72 /// use the `/brawlers/` endpoint.
73 brawler_id: usize,
74
75 /// The limit of rankings to get (i.e., to get the top `limit` players, sorted by trophies
76 /// on this specific brawler).
77 limit: u8,
78 },
79
80 /// Route for the `/brawlers/` endpoint, which returns data for all brawlers in the game.
81 Brawlers,
82
83 /// Route for the `/brawlers/:id` endpoint, which returns data for a specific brawler, given
84 /// that brawler's ID.
85 Brawler(usize),
86}
87
88impl Route {
89
90 /// Evaluates the `Route` instance into a full URL path string.
91 ///
92 /// # Examples
93 /// ```rs
94 /// use brawl_api::Route;
95 /// assert_eq!(Route::Player("tag"), "https://api.brawlstars.com/v1/players/tag")
96 /// assert_eq!(
97 /// Route::PlayerBattlelogs("tag"), "https://api.brawlstars.com/v1/players/tag/battlelogs"
98 /// )
99 /// assert_eq!(Route::Club("tag"), "https://api.brawlstars.com/v1/clubs/tag")
100 /// assert_eq!(Route::ClubMembers("tag"), "https://api.brawlstars.com/v1/clubs/tag/members")
101 /// ```
102 pub fn to_url_str(&self) -> String {
103 match self {
104 Route::Player(ref s) => format!("{}{}", b_api_concat!("players/"), s),
105
106 Route::PlayerBattlelogs(ref s) => format!(
107 "{}{}/battlelog", b_api_concat!("players/"), s
108 ),
109
110 Route::Club(ref s) => format!("{}{}", b_api_concat!("clubs/"), s),
111
112 Route::ClubMembers(ref s) => format!(
113 "{}{}/members", b_api_concat!("clubs/"), s
114 ),
115
116 Route::PlayerRankings {
117 ref country_code,
118 limit
119 } => format!(
120 "{}{}/players?limit={}", b_api_concat!("rankings/"), country_code, limit
121 ),
122
123 Route::ClubRankings {
124 ref country_code,
125 limit
126 } => format!(
127 "{}{}/clubs?limit={}", b_api_concat!("rankings/"), country_code, limit
128 ),
129
130 Route::BrawlerRankings {
131 ref country_code,
132 brawler_id,
133 limit
134 } => format!(
135 "{}{}/brawlers/{}?limit={}",
136 b_api_concat!("rankings/"), country_code, brawler_id, limit
137 ),
138
139 Route::Brawlers => String::from(b_api_concat!("brawlers/")),
140
141 Route::Brawler(id) => format!(
142 "{}/{}",
143 b_api_concat!("brawlers"),
144 id,
145 )
146 }
147 }
148}