From e2024eaa344f6844d2c6629aa96ba239c50dfda2 Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Sat, 23 Nov 2024 02:00:13 +0000 Subject: [PATCH] feat: move everything into a single file and compact it down --- src/endpoints/get_cns.rs | 52 --------- src/endpoints/get_id_from_email.rs | 42 ------- src/endpoints/get_members.rs | 55 --------- src/endpoints/mod.rs | 3 - src/lib.rs | 175 +++++++++++++++++++++++++++-- 5 files changed, 165 insertions(+), 162 deletions(-) delete mode 100644 src/endpoints/get_cns.rs delete mode 100644 src/endpoints/get_id_from_email.rs delete mode 100644 src/endpoints/get_members.rs delete mode 100644 src/endpoints/mod.rs diff --git a/src/endpoints/get_cns.rs b/src/endpoints/get_cns.rs deleted file mode 100644 index 3b87831..0000000 --- a/src/endpoints/get_cns.rs +++ /dev/null @@ -1,52 +0,0 @@ -use serde::{Deserialize, Serialize}; - -// This is what Wolves returns to us -#[derive(Deserialize, Serialize, Debug)] -struct WolvesResult { - success: i8, - result: Vec, -} - -// this is teh actual data we care about -#[derive(Deserialize, Serialize, Debug)] -struct WolvesResultCNS { - id: String, - name: String, - // Link to their page such as https://ulwolves.ie/society/computer - link: String, - // array of Committee members member_id's - committee: Vec, -} - -async fn get_committees(wolves_url_base: &str, api_key: &str) -> Vec { - if wolves_url_base.is_empty() { - return vec![]; - } - - // TODO: Change teh stored env value to teh base domain - // TODO: this address may change - let url = format!("{}/get_cns", wolves_url_base); - - // get wolves data - match reqwest::Client::new() - .post(&url) - .header("X-AM-Identity", api_key) - .send() - .await - { - Ok(x) => { - if let Ok(WolvesResult {success, result }) = x.json::().await { - if success != 1 { - return vec![]; - } - - return result; - } - } - Err(e) => { - dbg!(e); - } - } - - vec![] -} \ No newline at end of file diff --git a/src/endpoints/get_id_from_email.rs b/src/endpoints/get_id_from_email.rs deleted file mode 100644 index 1c0223d..0000000 --- a/src/endpoints/get_id_from_email.rs +++ /dev/null @@ -1,42 +0,0 @@ -use serde::{Deserialize, Serialize}; - -#[derive(Serialize, Deserialize, Debug)] -#[serde(untagged)] -pub enum WolvesResultUserResult { - B(bool), - S(String), -} - -#[derive(Deserialize, Serialize, Debug)] -struct WolvesResultUser { - success: i64, - result: WolvesResultUserResult, -} - -async fn get_user(wolves_url_base: &str, api_key: &str ,email: &str) -> Option { - let url = format!("{}/get_id_from_email", wolves_url_base); - - match reqwest::Client::new() - .post(&url) - .form(&[("email", email)]) - .header("X-AM-Identity", api_key) - .send() - .await - { - Ok(x) => { - if let Ok(y) = x.json::().await { - // this is the only time we will get a positive response, the None at the end catches everything else - if let WolvesResultUserResult::S(z) = y.result { - if let Ok(id) = z.parse::() { - return Some(id); - } - } - } - } - Err(e) => { - dbg!(e); - } - } - - None -} \ No newline at end of file diff --git a/src/endpoints/get_members.rs b/src/endpoints/get_members.rs deleted file mode 100644 index 9aa0060..0000000 --- a/src/endpoints/get_members.rs +++ /dev/null @@ -1,55 +0,0 @@ -use serde::{Deserialize, Serialize}; - -#[derive(Deserialize, Serialize, Debug)] -struct WolvesResult { - success: i8, - result: Vec, -} - -#[derive(Deserialize, Serialize, Debug)] -struct WolvesResultUser { - // TODO: Might be worth trying to get this replaced with the club/soc ID? - committee: String, - member_id: String, - first_name: String, - last_name: String, - contact_email: String, - opt_in_email: String, - student_id: Option, - note: Option, - expiry: String, - requested: String, - approved: String, - sitename: String, - domain: String, -} - -async fn get_wolves_sub(wolves_url_base: &str, api_key: &str) -> Vec { - if wolves_url_base.is_empty() { - return vec![]; - } - - let url = format!("{}/get_members", wolves_url_base); - match reqwest::Client::new() - .post(&url) - .header("X-AM-Identity", api_key) - .send() - .await - { - Ok(x) => { - if let Ok(WolvesResult {success,result}) = x.json::().await { - // this is the only time we will get a positive response, the None at the end catches everything else - if success != 1 { - return vec![]; - } - return result; - } - - } - Err(e) => { - dbg!(e); - } - } - - vec![] -} \ No newline at end of file diff --git a/src/endpoints/mod.rs b/src/endpoints/mod.rs deleted file mode 100644 index 0549b99..0000000 --- a/src/endpoints/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -mod get_members; -mod get_cns; -mod get_id_from_email; \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index b83e929..7ed13f8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,16 +1,171 @@ -mod endpoints; +use serde::{Deserialize, Serialize}; -pub fn add(left: u64, right: u64) -> u64 { - left + right +// This is what Wolves returns to us +// success will always be 1/ +#[derive(Deserialize, Serialize, Debug)] +struct WolvesResult { + success: i8, + result: Vec, } -#[cfg(test)] -mod tests { - use super::*; +#[derive(Deserialize, Serialize, Debug)] +struct WolvesResultSingle { + success: i8, + result: T, +} - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); +// get_members +#[derive(Deserialize, Serialize, Debug)] +struct WolvesUser { + // TODO: Might be worth trying to get this replaced with the club/soc ID? + committee: String, + member_id: String, + first_name: String, + last_name: String, + contact_email: String, + opt_in_email: String, + student_id: Option, + note: Option, + expiry: String, + requested: String, + approved: String, + sitename: String, + domain: String, +} + + +// get_cns +#[derive(Deserialize, Serialize, Debug)] +struct WolvesCNS { + id: String, + name: String, + // Link to their page such as https://ulwolves.ie/society/computer + link: String, + // array of Committee members member_id's + committee: Vec, +} + + +#[derive(Serialize, Deserialize, Debug)] +#[serde(untagged)] +pub enum WolvesUserExists { + B(bool), + S(String), +} + +pub struct Client<'a> { + base_wolves: &'a str, + base_key: Option<&'a str>, +} + +impl Client { + pub fn new(base_wolves: &str, base_key: Option<&str>) -> Self { + Self{ + base_wolves, + base_key + } } + + // This should handle any method where wolves returns an array + async fn get_bulk(self, wolves_endpoint: &str, api_key: &str) -> Vec { + if self.base_wolves.is_empty() { + return vec![]; + } + + let url = format!("{}/{}", self.base_wolves, wolves_endpoint); + + // get wolves data + match reqwest::Client::new() + .post(&url) + .header("X-AM-Identity", api_key) + .send() + .await + { + Ok(x) => { + if let Ok(WolvesResult {success, result }) = x.json::>().await { + if success != 1 { + return vec![]; + } + + return result; + } + } + Err(e) => { + dbg!(e); + } + } + vec![] + } + + // this is the command that gets a list of all members in a club/soc + // uses the api key for each club/soc + pub async fn get_members(self, api_key: &str) -> Vec{ + self.get_bulk::("get_members", api_key) + } + + // command to get club/soc and committee members + // uses the instance key if it exists + pub async fn get_committees(self) -> Vec { + if let Some(api_key) = self.base_key { + self.get_bulk::("get_cns", api_key) + } else { + vec![] + } + } + + + // method to get member_id from an email + // email can be either the login email or contact email + pub async fn get_member(self, email: &str) -> Option { + // if the key isnt set then we cant do anything. + let api_key = match self.base_key { + None => { return None; } + Some(key) => {key} + }; + + let url = format!("{}/get_id_from_email", self.base_wolves); + + match reqwest::Client::new() + .post(&url) + .form(&[("email", email)]) + .header("X-AM-Identity", api_key) + .send() + .await + { + Ok(x) => { + if let Ok(y) = x.json::>().await { + // this is the only time we will get a positive response, the None at the end catches everything else + if let WolvesUserExists::S(z) = y.result { + if let Ok(id) = z.parse::() { + return Some(id); + } + } + } + } + Err(e) => { + dbg!(e); + } + } + + None + } + } + + + + +// pub fn add(left: u64, right: u64) -> u64 { +// left + right +// } +// +// #[cfg(test)] +// mod tests { +// use super::*; +// +// #[test] +// fn it_works() { +// let result = add(2, 2); +// assert_eq!(result, 4); +// } +// }