Skip to main content

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