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 GetMatchesParams {
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 GetMatchesParams {
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_matches(&self, params: Option<&GetMatchesParams>) -> Result<Box<[MatchInfo]>> {
56		make_request(USER_URL, [&self.to_string()], params).await
57	}
58}
59
60#[cfg(feature = "blocking")]
61impl UserIdentifier<'_> {
62	/// Synchronously GET the user's matches by identifier using given `params`
63	pub fn get_matches_blocking(
64		&self,
65		params: Option<&GetMatchesParams>,
66	) -> Result<Box<[MatchInfo]>> {
67		make_request_blocking(USER_URL, [&self.to_string()], params)
68	}
69}
70
71const RECENT_URL: &str = "https://api.mcsrranked.com/matches/";
72
73/// Parameters for [`MatchInfo::get_recent`]
74///
75/// /// Note: this struct supports the builder pattern
76///
77/// # Examples
78/// ```
79/// use mcsr_ranked_api::game::requests::GetRecentMatchesParams;
80/// use mcsr_ranked_api::game::MatchType;
81/// let params = GetRecentMatchesParams::default()
82///     .season(0)
83///     .include_decay(true)
84///     .kind(MatchType::Private);
85/// ```
86#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
87#[serde(rename_all = "lowercase")]
88pub struct GetRecentMatchesParams<'a> {
89	#[serde(flatten)]
90	pub pagination: Pagination,
91	#[serde(rename = "type")]
92	pub kind: Option<MatchType>,
93	pub tag: Option<&'a str>,
94	pub season: Option<Season>,
95	#[serde(rename = "includedecay")]
96	#[serde(skip_serializing_if = "Not::not")]
97	pub include_decay: bool,
98}
99impl From<Pagination> for GetRecentMatchesParams<'_> {
100	fn from(pagination: Pagination) -> Self {
101		Self {
102			pagination,
103			..Default::default()
104		}
105	}
106}
107impl<'a> GetRecentMatchesParams<'a> {
108	/// Set the `pagination` field
109	///
110	/// Note: it is better to use `From<Pagination>`
111	pub fn pagination(mut self, pagination: Pagination) -> Self {
112		self.pagination = pagination;
113		self
114	}
115	/// Set the `kind` field
116	pub fn kind(mut self, kind: MatchType) -> Self {
117		self.kind = Some(kind);
118		self
119	}
120	/// Set the `tag` field
121	pub fn tag(mut self, tag: &'a str) -> Self {
122		self.tag = Some(tag);
123		self
124	}
125	/// Set the `season` field
126	pub fn season(mut self, season: Season) -> Self {
127		self.season = Some(season);
128		self
129	}
130	/// Set the `include_decay` field
131	pub fn include_decay(mut self, include_decay: bool) -> Self {
132		self.include_decay = include_decay;
133		self
134	}
135}
136
137impl MatchInfo {
138	/// GET recent matches given `params`
139	pub async fn get_recent(params: Option<&GetRecentMatchesParams<'_>>) -> Result<Box<[Self]>> {
140		make_request(RECENT_URL, &[] as &[&str], params).await
141	}
142}
143
144#[cfg(feature = "blocking")]
145impl MatchInfo {
146	/// Synchronously GET recent matches given `params`
147	pub fn get_recent_blocking(params: Option<&GetRecentMatchesParams<'_>>) -> Result<Box<[Self]>> {
148		make_request_blocking(RECENT_URL, &[] as &[&str], params)
149	}
150}