From 12e7101e3130697e62cd03216fde2a0ceb85ad21 Mon Sep 17 00:00:00 2001 From: Sergio Ribera <56278796+SergioRibera@users.noreply.github.com> Date: Mon, 19 Aug 2024 23:04:48 -0400 Subject: [PATCH] feat: some fixtures --- src/client.rs | 30 +++++++++++++++--------------- src/error.rs | 2 ++ 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/client.rs b/src/client.rs index fb63f97..b01a74e 100644 --- a/src/client.rs +++ b/src/client.rs @@ -41,23 +41,18 @@ pub struct Client { } impl Client { - async fn get_lang_version(&self) -> Result, reqwest::Error> { + async fn get_lang_version(&self) -> Result, Error> { let result = self.client.get(RUNTIMES_URL).send().await?; let json = result.json::>().await?; Ok(json .iter() - .find(|lang| { - (lang.language == self.language.to_owned()) - | (lang.aliases.contains(&self.language)) - }) + .find(|lang| (lang.language == self.language) | (lang.aliases.contains(&self.language))) .and_then(|l| Some(l.version.clone()))) } pub async fn execute(self) -> Result { - let version = match self.get_lang_version().await { - Err(err) => return Err(Error::Unknown(err.to_string())), - Ok(None) => return Err(Error::InvalidLanguage), - Ok(Some(v)) => v, + let Some(version) = self.get_lang_version().await? else { + return Err(Error::InvalidLanguage); }; let mut files = vec![FileData { name: None, @@ -82,11 +77,7 @@ impl Client { files, }) .send() - .await; - let data = match data { - Err(e) => return Err(Error::Unknown(e.to_string())), - Ok(res) => res, - }; + .await?; match data.json::().await { Ok(ApiResponse::Good(response)) => Ok(response), @@ -100,6 +91,7 @@ pub struct ClientBuilder { language: Option, main_file: Option, add_files: Vec, + agent: String, } impl ClientBuilder { @@ -108,6 +100,7 @@ impl ClientBuilder { language: None, main_file: None, add_files: vec![], + agent: "Automated Piston Agent".to_owned(), } } @@ -132,12 +125,19 @@ impl ClientBuilder { } } + pub fn user_agent(self, agent: T) -> ClientBuilder { + ClientBuilder { + agent: agent.to_string(), + ..self + } + } + pub fn build(self) -> Result { let language = self.language.ok_or(Error::MissingLang)?; let main_file = self.main_file.ok_or(Error::MissingMain)?; let http_client = reqwest::ClientBuilder::new() - .user_agent("fewwis-bot/@romancitodev") + .user_agent(&self.agent) .build() .unwrap(); Ok(Client { diff --git a/src/error.rs b/src/error.rs index a4e4a80..7586916 100644 --- a/src/error.rs +++ b/src/error.rs @@ -14,4 +14,6 @@ pub enum Error { BadRequest, #[error("Unkown error catched: {0}.")] Unknown(String), + #[error("Request error: {0}")] + Request(#[from] reqwest::Error), }