fmt: cargo fmt and clippy
This commit is contained in:
parent
e2024eaa34
commit
92a303401b
1 changed files with 110 additions and 116 deletions
60
src/lib.rs
60
src/lib.rs
|
@ -16,7 +16,7 @@ struct WolvesResultSingle<T> {
|
|||
|
||||
// get_members
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
struct WolvesUser {
|
||||
pub struct WolvesUser {
|
||||
// TODO: Might be worth trying to get this replaced with the club/soc ID?
|
||||
committee: String,
|
||||
member_id: String,
|
||||
|
@ -33,10 +33,9 @@ struct WolvesUser {
|
|||
domain: String,
|
||||
}
|
||||
|
||||
|
||||
// get_cns
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
struct WolvesCNS {
|
||||
pub struct WolvesCNS {
|
||||
id: String,
|
||||
name: String,
|
||||
// Link to their page such as https://ulwolves.ie/society/computer
|
||||
|
@ -45,7 +44,6 @@ struct WolvesCNS {
|
|||
committee: Vec<String>,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
#[serde(untagged)]
|
||||
pub enum WolvesUserExists {
|
||||
|
@ -53,36 +51,35 @@ pub enum WolvesUserExists {
|
|||
S(String),
|
||||
}
|
||||
|
||||
pub struct Client<'a> {
|
||||
base_wolves: &'a str,
|
||||
base_key: Option<&'a str>,
|
||||
pub struct Client {
|
||||
base_wolves: String,
|
||||
base_key: Option<String>,
|
||||
}
|
||||
|
||||
impl Client {
|
||||
pub fn new(base_wolves: &str, base_key: Option<&str>) -> Self {
|
||||
Self{
|
||||
base_wolves,
|
||||
base_key
|
||||
Self {
|
||||
base_wolves: base_wolves.to_string(),
|
||||
base_key: base_key.map(|x| x.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
// This should handle any method where wolves returns an array
|
||||
async fn get_bulk<T>(self, wolves_endpoint: &str, api_key: &str) -> Vec<T> {
|
||||
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![];
|
||||
}
|
||||
|
||||
let url = format!("{}/{}", self.base_wolves, wolves_endpoint);
|
||||
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
|
||||
{
|
||||
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 let Ok(WolvesResult {
|
||||
success,
|
||||
result,
|
||||
}) = x.json::<WolvesResult<T>>().await
|
||||
{
|
||||
if success != 1 {
|
||||
return vec![];
|
||||
}
|
||||
|
@ -99,31 +96,32 @@ impl Client {
|
|||
|
||||
// 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)
|
||||
pub async fn get_members(&self, api_key: &str) -> Vec<WolvesUser> {
|
||||
self.get_bulk::<WolvesUser>("get_members", api_key).await
|
||||
}
|
||||
|
||||
// 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)
|
||||
pub async fn get_committees(&self) -> Vec<WolvesCNS> {
|
||||
if let Some(api_key) = &self.base_key {
|
||||
self.get_bulk::<WolvesCNS>("get_cns", api_key).await
|
||||
} 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 api_key = match &self.base_key {
|
||||
None => {
|
||||
return None;
|
||||
}
|
||||
Some(key) => key,
|
||||
};
|
||||
|
||||
let url = format!("{}/get_id_from_email", self.base_wolves);
|
||||
let url = format!("{}/get_id_from_email", &self.base_wolves);
|
||||
|
||||
match reqwest::Client::new()
|
||||
.post(&url)
|
||||
|
@ -149,12 +147,8 @@ impl Client {
|
|||
|
||||
None
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// pub fn add(left: u64, right: u64) -> u64 {
|
||||
// left + right
|
||||
// }
|
||||
|
|
Loading…
Reference in a new issue