diff --git a/src/lib.rs b/src/lib.rs index 30a2681..378b145 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,12 +13,11 @@ use crate::parser::{ }; pub use error::*; use reqwest::redirect::Policy; -use reqwest::{Client, StatusCode}; +use reqwest::{Client, Response, StatusCode}; pub use steamid_ng::SteamID; pub type Result = std::result::Result; -#[derive(Default)] pub struct UgcClient { client: Client, player_parser: PlayerParser, @@ -33,6 +32,12 @@ pub struct UgcClient { map_history_parser: MapHistoryParser, } +impl Default for UgcClient { + fn default() -> Self { + Self::new() + } +} + /// "API client" for ugc by scraping the website impl UgcClient { pub fn new() -> Self { @@ -53,35 +58,32 @@ impl UgcClient { /// Retrieve player information pub async fn player(&self, steam_id: SteamID) -> Result { - let res = self + let body = self .client .get(format!( "https://www.ugcleague.com/players_page.cfm?player_id={}", u64::from(steam_id) )) .send() + .await? + .check_not_found()? + .text() .await?; - if res.status() == StatusCode::FOUND { - return Err(ScrapeError::NotFound); - } - let body = res.text().await?; self.player_parser.parse(&body) } /// Retrieve team membership history for a player pub async fn player_team_history(&self, steam_id: SteamID) -> Result> { - let res = self + let body = self .client .get(format!( "https://www.ugcleague.com/players_page_details.cfm?player_id={}", u64::from(steam_id) )) .send() + .await? + .check_not_found()? .await?; - if res.status() == StatusCode::FOUND { - return Err(ScrapeError::NotFound); - } - let body = res.text().await?; self.player_detail_parser.parse(&body) } @@ -161,6 +163,7 @@ impl UgcClient { )) .send() .await? + .check_not_found()? .text() .await?; self.match_page_parser.parse(&body) @@ -184,3 +187,17 @@ impl UgcClient { self.map_history_parser.parse(&body) } } + +trait ResponseExt: Sized { + fn check_not_found(self) -> Result; +} + +impl ResponseExt for Response { + fn check_not_found(self) -> Result { + if self.status() == StatusCode::FOUND { + Err(ScrapeError::NotFound) + } else { + Ok(self) + } + } +}