mcsr_ranked_api/game/
requests.rs

1use serde::Serialize;
2use std::ops::Not;
3
4#[cfg(feature = "blocking")]
5use crate::helpers::make_request_blocking;
6use crate::{
7	helpers::make_request,
8	pagination::Pagination,
9	types::{MatchId, Season},
10	user::identifier::UserIdentifier,
11	Result,
12};
13
14use super::{AdvancedMatchInfo, MatchInfo, MatchType};
15
16const BASE_URL: &str = "https://api.mcsrranked.com/matches/{}";
17
18impl AdvancedMatchInfo {
19	pub async fn get_by_id(id: MatchId) -> Result<Self> {
20		make_request(BASE_URL, [&id.to_string()], None::<&()>).await
21	}
22}
23
24#[cfg(feature = "blocking")]
25impl AdvancedMatchInfo {
26	pub fn get_by_id_blocking(id: MatchId) -> Result<Self> {
27		make_request_blocking(BASE_URL, [&id.to_string()], None::<&()>)
28	}
29}
30
31const USER_URL: &str = "https://api.mcsrranked.com/users/{}/matches";
32
33/// Parameters for [`UserIdentifier::get_user_matches`]
34#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
35#[serde(rename_all = "lowercase")]
36pub struct GetUserMatchesParams {
37	#[serde(flatten)]
38	pub pagination: Pagination,
39	#[serde(rename = "type")]
40	pub kind: Option<MatchType>,
41	pub season: Option<Season>,
42	pub exclude_decay: bool,
43}
44impl From<Pagination> for GetUserMatchesParams {
45	fn from(pagination: Pagination) -> Self {
46		Self {
47			pagination,
48			..Default::default()
49		}
50	}
51}
52
53impl UserIdentifier<'_> {
54	/// GET the user's matches by identifier using given `params`
55	pub async fn get_user_matches(
56		&self,
57		params: Option<&GetUserMatchesParams>,
58	) -> Result<Box<[MatchInfo]>> {
59		make_request(USER_URL, [&self.to_string()], params).await
60	}
61}
62
63#[cfg(feature = "blocking")]
64impl UserIdentifier<'_> {
65	/// Synchronously GET the user's matches by identifier using given `params`
66	pub fn get_user_matches_blocking(
67		&self,
68		params: Option<&GetUserMatchesParams>,
69	) -> Result<Box<[MatchInfo]>> {
70		make_request_blocking(USER_URL, [&self.to_string()], params)
71	}
72}
73
74const RECENT_URL: &str = "https://api.mcsrranked.com/matches/";
75
76/// Parameters for [`MatchInfo::get_recent`]
77///
78/// /// Note: this struct supports the builder pattern
79///
80/// # Examples
81/// ```
82/// use mcsr_ranked_api::game::requests::GetRecentMatchesParams;
83/// use mcsr_ranked_api::game::MatchType;
84/// let params = GetRecentMatchesParams::default()
85///     .season(0)
86///     .include_decay(true)
87///     .kind(MatchType::Private);
88/// ```
89#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
90#[serde(rename_all = "lowercase")]
91pub struct GetRecentMatchesParams<'a> {
92	#[serde(flatten)]
93	pub pagination: Pagination,
94	#[serde(rename = "type")]
95	pub kind: Option<MatchType>,
96	pub tag: Option<&'a str>,
97	pub season: Option<Season>,
98	#[serde(rename = "includedecay")]
99	#[serde(skip_serializing_if = "Not::not")]
100	pub include_decay: bool,
101}
102impl From<Pagination> for GetRecentMatchesParams<'_> {
103	fn from(pagination: Pagination) -> Self {
104		Self {
105			pagination,
106			..Default::default()
107		}
108	}
109}
110impl<'a> GetRecentMatchesParams<'a> {
111	/// Set the `pagination` field
112	///
113	/// Note: it is better to use `From<Pagination>`
114	pub fn pagination(mut self, pagination: Pagination) -> Self {
115		self.pagination = pagination;
116		self
117	}
118	/// Set the `kind` field
119	pub fn kind(mut self, kind: MatchType) -> Self {
120		self.kind = Some(kind);
121		self
122	}
123	/// Set the `tag` field
124	pub fn tag(mut self, tag: &'a str) -> Self {
125		self.tag = Some(tag);
126		self
127	}
128	/// Set the `season` field
129	pub fn season(mut self, season: Season) -> Self {
130		self.season = Some(season);
131		self
132	}
133	/// Set the `include_decay` field
134	pub fn include_decay(mut self, include_decay: bool) -> Self {
135		self.include_decay = include_decay;
136		self
137	}
138}
139
140impl MatchInfo {
141	/// GET recent matches given `params`
142	pub async fn get_recent(params: Option<&GetRecentMatchesParams<'_>>) -> Result<Box<[Self]>> {
143		make_request(RECENT_URL, &[] as &[&str], params).await
144	}
145}
146
147#[cfg(feature = "blocking")]
148impl MatchInfo {
149	/// Synchronously GET recent matches given `params`
150	pub fn get_recent_blocking(params: Option<&GetRecentMatchesParams<'_>>) -> Result<Box<[Self]>> {
151		make_request_blocking(RECENT_URL, &[] as &[&str], params)
152	}
153}