This commit is contained in:
Junko 2023-11-22 13:56:08 +01:00
parent 7617757861
commit dc472a929b
2 changed files with 114 additions and 31 deletions

View File

@ -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<Value> {
let url = format!("{}/systems/{}/members", PK_URL, sysid);
let res = http_get_request(url,key);
let datas: Vec<Value> = 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<String, Vec<Member>> {
let mut fronters: HashMap<String, Vec<Member>> = 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<String, Box<dyn std:
.await?;
Ok(res)
}
#[tokio::main]
async fn http_post_request(url: String, key: &str, body: &HashMap<&str, Vec<String>>) -> Result<(), Box<dyn std::error::Error>> {
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<String, Box<dyn std::error::Error>> {
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

View File

@ -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<Value> {
let url = format!("{}/systems/{}/members", PK_URL, sysid);
@ -202,27 +212,46 @@ fn pk_get_members(key: &str, sysid: &str) -> Vec<Value> {
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<Member> {
let url = format!("{}/fronters", SP_URL);
fn pk_get_fronters(key: &str, sys: &System) -> Vec<Member> {
let url = format!("{}/systems/{}/fronters", PK_URL, sys.pk_userid);
let res = http_get_request(url,key);
let datas: Vec<Value> = 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<Member> = 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<Member>, fronters: &HashMap<String, Vec<Member>>) {
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<String>> = 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, String>) -> 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<String> {
fn sp_get_fronters(key: &str, sys: &System) -> Vec<Member> {
let url = format!("{}/fronters", SP_URL);
let res = http_get_request(url,key);
let datas: Vec<Value> = serde_json::from_str(&res.unwrap()).unwrap();
let mut members: Vec<String> = Vec::new();
let mut members: Vec<Member> = 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<String, Vec<Member>> {
let mut fronters: HashMap<String, Vec<Member>> = 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<String, Vec
return fronters;
}
// Get fronters:1 ends here
// Get system json:1 ends here
// [[file:../README.org::*Get request][Get request:1]]
// [[file:../README.org::*Http Request handler][Http Request handler:1]]
#[tokio::main]
async fn http_get_request(url: String, key: &str) -> Result<String, Box<dyn std::error::Error>> {
let client = reqwest::Client::new();
@ -377,9 +402,8 @@ async fn http_get_request(url: String, key: &str) -> Result<String, Box<dyn std:
.await?;
Ok(res)
}
// Get request:1 ends here
// [[file:../README.org::*Post request][Post request:1]]
#[tokio::main]
async fn http_post_request(url: String, key: &str, body: &HashMap<&str, Vec<String>>) -> Result<(), Box<dyn std::error::Error>> {
let client = reqwest::Client::new();
@ -392,9 +416,7 @@ async fn http_post_request(url: String, key: &str, body: &HashMap<&str, Vec<Stri
.await?;
Ok(())
}
// Post request:1 ends here
// [[file:../README.org::*Patch request][Patch request:1]]
/*
#[tokio::main]
async fn http_patch_request(url: String, key: &str) -> Result<String, Box<dyn std::error::Error>> {
@ -411,4 +433,4 @@ async fn http_patch_request(url: String, key: &str) -> Result<String, Box<dyn st
Ok(res)
}
*/
// Patch request:1 ends here
// Http Request handler:1 ends here