use serde::Serialize;
use std::ops::Not;
#[cfg(feature = "blocking")]
use crate::helpers::make_request_blocking;
use crate::{
Result,
helpers::make_request,
pagination::Pagination,
types::{MatchId, Season},
user::identifier::UserIdentifier,
};
use super::{AdvancedMatchInfo, MatchInfo, MatchType};
const BASE_URL: &str = "https://api.mcsrranked.com/matches/{}";
impl AdvancedMatchInfo {
pub async fn get_by_id(id: MatchId) -> Result<Self> {
make_request(BASE_URL, [&id.to_string()], None::<&()>).await
}
}
#[cfg(feature = "blocking")]
impl AdvancedMatchInfo {
pub fn get_by_id_blocking(id: MatchId) -> Result<Self> {
make_request_blocking(BASE_URL, [&id.to_string()], None::<&()>)
}
}
const USER_URL: &str = "https://api.mcsrranked.com/users/{}/matches";
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
#[serde(rename_all = "lowercase")]
pub struct GetMatchesParams {
#[serde(flatten)]
pub pagination: Pagination,
#[serde(rename = "type")]
pub kind: Option<MatchType>,
pub season: Option<Season>,
pub exclude_decay: bool,
}
impl From<Pagination> for GetMatchesParams {
fn from(pagination: Pagination) -> Self {
Self {
pagination,
..Default::default()
}
}
}
impl<'a> UserIdentifier<'a> {
pub async fn get_matches(
&self,
params: impl Into<Option<&'a GetMatchesParams>>,
) -> Result<Box<[MatchInfo]>> {
make_request(USER_URL, [&self.to_string()], params.into()).await
}
}
#[cfg(feature = "blocking")]
impl<'a> UserIdentifier<'a> {
pub fn get_matches_blocking(
&self,
params: impl Into<Option<&'a GetMatchesParams>>,
) -> Result<Box<[MatchInfo]>> {
make_request_blocking(USER_URL, [&self.to_string()], params.into())
}
}
const RECENT_URL: &str = "https://api.mcsrranked.com/matches/";
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
#[serde(rename_all = "lowercase")]
pub struct GetRecentMatchesParams<'a> {
#[serde(flatten)]
pub pagination: Pagination,
#[serde(rename = "type")]
pub kind: Option<MatchType>,
pub tag: Option<&'a str>,
pub season: Option<Season>,
#[serde(rename = "includedecay")]
#[serde(skip_serializing_if = "Not::not")]
pub include_decay: bool,
}
impl From<Pagination> for GetRecentMatchesParams<'_> {
fn from(pagination: Pagination) -> Self {
Self {
pagination,
..Default::default()
}
}
}
impl<'a> GetRecentMatchesParams<'a> {
pub fn pagination(mut self, pagination: Pagination) -> Self {
self.pagination = pagination;
self
}
pub fn kind(mut self, kind: MatchType) -> Self {
self.kind = Some(kind);
self
}
pub fn tag(mut self, tag: &'a str) -> Self {
self.tag = Some(tag);
self
}
pub fn season(mut self, season: Season) -> Self {
self.season = Some(season);
self
}
pub fn include_decay(mut self, include_decay: bool) -> Self {
self.include_decay = include_decay;
self
}
}
impl MatchInfo {
pub async fn get_recent<'a>(
params: impl Into<Option<&'a GetRecentMatchesParams<'a>>>,
) -> Result<Box<[Self]>> {
make_request(RECENT_URL, &[] as &[&str], params.into()).await
}
}
#[cfg(feature = "blocking")]
impl MatchInfo {
pub fn get_recent_blocking<'a>(
params: impl Into<Option<&'a GetRecentMatchesParams<'a>>>,
) -> Result<Box<[Self]>> {
make_request_blocking(RECENT_URL, &[] as &[&str], params.into())
}
}