From 7bad3c45442438ebfa65122b83e56c4321db2215 Mon Sep 17 00:00:00 2001 From: Anshul Gupta Date: Wed, 18 Sep 2024 15:43:43 -0700 Subject: feat: add more player endpoints --- src/endpoint/all/players.rs | 140 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) diff --git a/src/endpoint/all/players.rs b/src/endpoint/all/players.rs index 9f177fe..8541237 100644 --- a/src/endpoint/all/players.rs +++ b/src/endpoint/all/players.rs @@ -1,2 +1,142 @@ +use std::future::Future; + +use reqwest::{Request, Response}; +use url::Url; + +use crate::{ + endpoint::{ + sealed::Sealed, CollectionOptions, EndpointError, ListResponse, PaginatedEndpoint, BASE_URL, + }, + model::{league::League, Identifier}, +}; + crate::endpoint::list_endpoint!(ListPlayers("/players") => crate::model::player::Player); crate::endpoint::get_endpoint!(GetPlayer("/players") => crate::model::player::Player); + +#[derive(Debug, Clone, PartialEq, Eq, bon::Builder)] +pub struct ListPlayerLeagues<'a> { + #[builder(into)] + id: Identifier<'a>, + #[builder(default)] + options: CollectionOptions, +} + +impl Sealed for ListPlayerLeagues<'_> { + type Response = ListResponse; + + fn to_request(self) -> Result { + let mut url = Url::parse(&format!("{}/players/{}/leagues", BASE_URL, self.id))?; + self.options.add_params(&mut url); + Ok(Request::new(reqwest::Method::GET, url)) + } + + fn from_response( + response: Response, + ) -> impl Future> + Send { + ListResponse::from_response(response) + } +} + +impl PaginatedEndpoint for ListPlayerLeagues<'_> { + type Item = League; + + fn with_options(self, options: CollectionOptions) -> Self { + Self { options, ..self } + } +} + +#[derive(Debug, Clone, PartialEq, Eq, bon::Builder)] +pub struct ListPlayerSeries<'a> { + #[builder(into)] + id: Identifier<'a>, + #[builder(default)] + options: CollectionOptions, +} + +impl Sealed for ListPlayerSeries<'_> { + type Response = ListResponse; + + fn to_request(self) -> Result { + let mut url = Url::parse(&format!("{}/players/{}/series", BASE_URL, self.id))?; + self.options.add_params(&mut url); + Ok(Request::new(reqwest::Method::GET, url)) + } + + fn from_response( + response: Response, + ) -> impl Future> + Send { + ListResponse::from_response(response) + } +} + +impl PaginatedEndpoint for ListPlayerSeries<'_> { + type Item = League; + + fn with_options(self, options: CollectionOptions) -> Self { + Self { options, ..self } + } +} + +#[derive(Debug, Clone, PartialEq, Eq, bon::Builder)] +pub struct ListPlayerTournaments<'a> { + #[builder(into)] + id: Identifier<'a>, + #[builder(default)] + options: CollectionOptions, +} + +impl Sealed for ListPlayerTournaments<'_> { + type Response = ListResponse; + + fn to_request(self) -> Result { + let mut url = Url::parse(&format!("{}/players/{}/tournaments", BASE_URL, self.id))?; + self.options.add_params(&mut url); + Ok(Request::new(reqwest::Method::GET, url)) + } + + fn from_response( + response: Response, + ) -> impl Future> + Send { + ListResponse::from_response(response) + } +} + +impl PaginatedEndpoint for ListPlayerTournaments<'_> { + type Item = League; + + fn with_options(self, options: CollectionOptions) -> Self { + Self { options, ..self } + } +} + +#[derive(Debug, Clone, PartialEq, Eq, bon::Builder)] +pub struct ListPlayerMatches<'a> { + #[builder(into)] + id: Identifier<'a>, + #[builder(default)] + options: CollectionOptions, +} + +impl Sealed for ListPlayerMatches<'_> { + type Response = ListResponse; + + fn to_request(self) -> Result { + let mut url = Url::parse(&format!("{}/players/{}/matches", BASE_URL, self.id))?; + self.options.add_params(&mut url); + Ok(Request::new(reqwest::Method::GET, url)) + } + + fn from_response( + response: Response, + ) -> impl Future> + Send { + ListResponse::from_response(response) + } +} + +impl PaginatedEndpoint for ListPlayerMatches<'_> { + type Item = League; + + fn with_options(self, options: CollectionOptions) -> Self { + Self { options, ..self } + } +} -- cgit v1.2.3