fmt: cargo fmt and clippy

This commit is contained in:
silver 2024-11-23 02:13:29 +00:00
parent e2024eaa34
commit 92a303401b
Signed by: silver
GPG key ID: 36F93D61BAD3FD7D

View file

@ -4,157 +4,151 @@ use serde::{Deserialize, Serialize};
// success will always be 1/ // success will always be 1/
#[derive(Deserialize, Serialize, Debug)] #[derive(Deserialize, Serialize, Debug)]
struct WolvesResult<T> { struct WolvesResult<T> {
success: i8, success: i8,
result: Vec<T>, result: Vec<T>,
} }
#[derive(Deserialize, Serialize, Debug)] #[derive(Deserialize, Serialize, Debug)]
struct WolvesResultSingle<T> { struct WolvesResultSingle<T> {
success: i8, success: i8,
result: T, result: T,
} }
// get_members // get_members
#[derive(Deserialize, Serialize, Debug)] #[derive(Deserialize, Serialize, Debug)]
struct WolvesUser { pub struct WolvesUser {
// TODO: Might be worth trying to get this replaced with the club/soc ID? // TODO: Might be worth trying to get this replaced with the club/soc ID?
committee: String, committee: String,
member_id: String, member_id: String,
first_name: String, first_name: String,
last_name: String, last_name: String,
contact_email: String, contact_email: String,
opt_in_email: String, opt_in_email: String,
student_id: Option<String>, student_id: Option<String>,
note: Option<String>, note: Option<String>,
expiry: String, expiry: String,
requested: String, requested: String,
approved: String, approved: String,
sitename: String, sitename: String,
domain: String, domain: String,
} }
// get_cns // get_cns
#[derive(Deserialize, Serialize, Debug)] #[derive(Deserialize, Serialize, Debug)]
struct WolvesCNS { pub struct WolvesCNS {
id: String, id: String,
name: String, name: String,
// Link to their page such as https://ulwolves.ie/society/computer // Link to their page such as https://ulwolves.ie/society/computer
link: String, link: String,
// array of Committee members member_id's // array of Committee members member_id's
committee: Vec<String>, committee: Vec<String>,
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)] #[serde(untagged)]
pub enum WolvesUserExists { pub enum WolvesUserExists {
B(bool), B(bool),
S(String), S(String),
} }
pub struct Client<'a> { pub struct Client {
base_wolves: &'a str, base_wolves: String,
base_key: Option<&'a str>, base_key: Option<String>,
} }
impl Client { impl Client {
pub fn new(base_wolves: &str, base_key: Option<&str>) -> Self { pub fn new(base_wolves: &str, base_key: Option<&str>) -> Self {
Self{ Self {
base_wolves, base_wolves: base_wolves.to_string(),
base_key base_key: base_key.map(|x| x.to_string()),
} }
}
// This should handle any method where wolves returns an array
async fn get_bulk<T: serde::de::DeserializeOwned>(&self, wolves_endpoint: &str, api_key: &str) -> Vec<T> {
if self.base_wolves.is_empty() {
return vec![];
} }
// This should handle any method where wolves returns an array let url = format!("{}/{}", &self.base_wolves, wolves_endpoint);
async fn get_bulk<T>(self, wolves_endpoint: &str, api_key: &str) -> Vec<T> {
if self.base_wolves.is_empty() { // 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::<WolvesResult<T>>().await
{
if success != 1 {
return vec![]; return vec![];
}
return result;
} }
}
Err(e) => {
dbg!(e);
}
}
vec![]
}
let url = format!("{}/{}", self.base_wolves, wolves_endpoint); // 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<WolvesUser> {
self.get_bulk::<WolvesUser>("get_members", api_key).await
}
// get wolves data // command to get club/soc and committee members
match reqwest::Client::new() // uses the instance key if it exists
.post(&url) pub async fn get_committees(&self) -> Vec<WolvesCNS> {
.header("X-AM-Identity", api_key) if let Some(api_key) = &self.base_key {
.send() self.get_bulk::<WolvesCNS>("get_cns", api_key).await
.await } else {
{ vec![]
Ok(x) => { }
if let Ok(WolvesResult {success, result }) = x.json::<WolvesResult<T>>().await { }
if success != 1 {
return vec![];
}
return result; // 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<i64> {
Err(e) => { // if the key isnt set then we cant do anything.
dbg!(e); 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::<WolvesResultSingle<WolvesUserExists>>().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::<i64>() {
return Some(id);
} }
}
} }
vec![] }
} Err(e) => {
dbg!(e);
// 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<WolvesUser>{
self.get_bulk::<WolvesUser>("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<WolvesCNS> {
if let Some(api_key) = self.base_key {
self.get_bulk::<WolvesCNS>("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<i64> {
// 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::<WolvesResultSingle<WolvesUserExists>>().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::<i64>() {
return Some(id);
}
}
}
}
Err(e) => {
dbg!(e);
}
}
None
} }
None
}
} }
// pub fn add(left: u64, right: u64) -> u64 { // pub fn add(left: u64, right: u64) -> u64 {
// left + right // left + right
// } // }