From dc472a929bd4b4c6e6c9b8fe289cb212de555778 Mon Sep 17 00:00:00 2001 From: Junko Date: Wed, 22 Nov 2023 13:56:08 +0100 Subject: [PATCH] get --- README.org | 63 +++++++++++++++++++++++++++++++++++++++- src/main.rs | 82 +++++++++++++++++++++++++++++++++-------------------- 2 files changed, 114 insertions(+), 31 deletions(-) diff --git a/README.org b/README.org index 8b43b6f..244ab5c 100644 --- a/README.org +++ b/README.org @@ -100,7 +100,7 @@ fn main() { //set_empty(config_path); } }, - //"get" => get(config_path), + "get" => get(config_path), "memberlist" => memberlist(config_path), &_ => println!("Invalid command"), } @@ -207,6 +207,19 @@ fn set_member(config_path: String, tf_members: &[String]) -> Result<(), &'static } Ok(()) } + +fn memberlist(config_path: String) { + let sys = get_system(&config_path); + + for mem in sys.members { + if mem.name != mem.alias { + println!("{} / {}", mem.name, mem.alias); + } else { + println!("{}", mem.name); + } + } + +} #+end_src ***** Memberlist @@ -234,6 +247,15 @@ fn pk_get_system(key: &str) -> Value { let res = http_get_request(url,key); return serde_json::from_str(&res.unwrap()).unwrap(); } + +fn pk_get_members(key: &str, sysid: &str) -> Vec { + let url = format!("{}/systems/{}/members", PK_URL, sysid); + + let res = http_get_request(url,key); + let datas: Vec = serde_json::from_str(&res.unwrap()).unwrap(); + + return datas; +} #+end_src ***** Get members @@ -405,6 +427,14 @@ fn get_system(config_path: &str) -> System { return sys; } + +fn get_fronters(pk_key: &str, sp_key: &str, sys: &System) -> HashMap> { + let mut fronters: HashMap> = HashMap::new(); + fronters.insert(String::from("pk"), pk_get_fronters(pk_key, sys)); + fronters.insert(String::from("sp"), sp_get_fronters(sp_key, sys)); + + return fronters; +} #+end_src ***** Get fronters @@ -434,6 +464,37 @@ async fn http_get_request(url: String, key: &str) -> Result>) -> Result<(), Box> { + let client = reqwest::Client::new(); + let _ = client + .post(url) + .json(body) + .header(USER_AGENT, "Pluralsync") + .header(AUTHORIZATION, key) + .send() + .await?; + Ok(()) +} + +/* +#[tokio::main] +async fn http_patch_request(url: String, key: &str) -> Result> { + let client = reqwest::Client::new(); + let res = client + .patch(url) + .body("wiwiwiw") + .header(USER_AGENT, "Pluralsync") + .header(AUTHORIZATION, key) + .send() + .await? + .text() + .await?; + Ok(res) +} +*/ #+end_src ***** Post request diff --git a/src/main.rs b/src/main.rs index d13ce1b..15040ab 100644 --- a/src/main.rs +++ b/src/main.rs @@ -64,7 +64,7 @@ fn main() { //set_empty(config_path); } }, - //"get" => get(config_path), + "get" => get(config_path), "memberlist" => memberlist(config_path), &_ => println!("Invalid command"), } @@ -167,9 +167,7 @@ fn set_member(config_path: String, tf_members: &[String]) -> Result<(), &'static } Ok(()) } -// Set member:1 ends here -// [[file:../README.org::*Memberlist][Memberlist:1]] fn memberlist(config_path: String) { let sys = get_system(&config_path); @@ -182,7 +180,21 @@ fn memberlist(config_path: String) { } } -// Memberlist:1 ends here + +fn get(config_path: String) { + let config = get_config(&config_path); + let sys = get_system(&config_path); + + let f = get_fronters(&config["pk_key"].as_str().unwrap(), &config["sp_key"].as_str().unwrap(), &sys); + let mut names = Vec::new(); + for m in &f["pk"] { + names.push(String::from(&m.name)); + } + let fronters = names.join(" | "); + println!("Currently fronting: {}", fronters); + let _ = fs::write(format!("{}/.front", config_path), fronters); +} +// Set member:1 ends here // [[file:../README.org::*Get system][Get system:1]] fn pk_get_system(key: &str) -> Value { @@ -191,9 +203,7 @@ fn pk_get_system(key: &str) -> Value { let res = http_get_request(url,key); return serde_json::from_str(&res.unwrap()).unwrap(); } -// Get system:1 ends here -// [[file:../README.org::*Get members][Get members:1]] fn pk_get_members(key: &str, sysid: &str) -> Vec { let url = format!("{}/systems/{}/members", PK_URL, sysid); @@ -202,27 +212,46 @@ fn pk_get_members(key: &str, sysid: &str) -> Vec { return datas; } -// Get members:1 ends here +// Get system:1 ends here // [[file:../README.org::*Get fronters][Get fronters:1]] -fn sp_get_fronters(key: &str, sys: &System) -> Vec { - let url = format!("{}/fronters", SP_URL); +fn pk_get_fronters(key: &str, sys: &System) -> Vec { + let url = format!("{}/systems/{}/fronters", PK_URL, sys.pk_userid); let res = http_get_request(url,key); - let datas: Vec = serde_json::from_str(&res.unwrap()).unwrap(); + let data: Value = serde_json::from_str(&res.unwrap()).unwrap(); + let memberdata = &data["members"].as_array(); let mut members: Vec = Vec::new(); - for data in datas { - let sp_id = &data["content"]["member"].as_str().unwrap(); - for member in &sys.members { - if &member.sp_id == sp_id { - members.push(member.clone()); + for member in memberdata { + for m in member.into_iter() { + for dbmem in &sys.members { + if m["name"].as_str().unwrap() == dbmem.name { + members.push(dbmem.clone()); + } } } - } + return members; } + +fn pk_set_fronters(key: &str, sys: &System, to_front: Vec, fronters: &HashMap>) { + let url = format!("{}/systems/{}/switches", PK_URL, sys.pk_userid); + + if to_front != fronters["pk"] { + let mut frontcodes = Vec::new(); + for tf in to_front { + frontcodes.push(tf.pk_id); + } + let mut body: HashMap<&str, Vec> = HashMap::new(); + body.insert("members", frontcodes); + let _ = http_post_request(url, key, &body); + } else { + println!("Members already fonting"); + } + +} // Get fronters:1 ends here // [[file:../README.org::*Get user ID][Get user ID:1]] @@ -267,22 +296,20 @@ fn get_sp_id(mem: &Member, ids: &HashMap) -> String { // Get ID from member:1 ends here // [[file:../README.org::*Get fronters][Get fronters:1]] -fn sp_get_fronters(key: &str, sys: &System) -> Vec { +fn sp_get_fronters(key: &str, sys: &System) -> Vec { let url = format!("{}/fronters", SP_URL); let res = http_get_request(url,key); let datas: Vec = serde_json::from_str(&res.unwrap()).unwrap(); - let mut members: Vec = Vec::new(); + let mut members: Vec = Vec::new(); for data in datas { let sp_id = &data["content"]["member"].as_str().unwrap(); - let mut push_name = String::new(); for member in &sys.members { if &member.sp_id == sp_id { - push_name = String::from(&member.name); + members.push(member.clone()); } } - members.push(push_name); } return members; @@ -351,9 +378,7 @@ fn get_system(config_path: &str) -> System { return sys; } -// Get system json:1 ends here -// [[file:../README.org::*Get fronters][Get fronters:1]] fn get_fronters(pk_key: &str, sp_key: &str, sys: &System) -> HashMap> { let mut fronters: HashMap> = HashMap::new(); fronters.insert(String::from("pk"), pk_get_fronters(pk_key, sys)); @@ -361,9 +386,9 @@ fn get_fronters(pk_key: &str, sp_key: &str, sys: &System) -> HashMap Result> { let client = reqwest::Client::new(); @@ -377,9 +402,8 @@ async fn http_get_request(url: String, key: &str) -> Result>) -> Result<(), Box> { let client = reqwest::Client::new(); @@ -392,9 +416,7 @@ async fn http_post_request(url: String, key: &str, body: &HashMap<&str, Vec Result> { @@ -411,4 +433,4 @@ async fn http_patch_request(url: String, key: &str) -> Result