get members

This commit is contained in:
Junko 2023-11-21 15:38:25 +01:00
parent e338f92361
commit 46e0eb210d
4 changed files with 104 additions and 4 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
/target
/config.json
/system.json

View File

@ -56,6 +56,7 @@ const SP_URL: &str = "https://api.apparyllis.com/v1";
#[derive(Debug)]
#[derive(Serialize)]
#[derive(Deserialize)]
#[derive(Clone)]
struct System {
pk_userid: String,
sp_userid: String,
@ -120,7 +121,7 @@ fn set_member(config_path: String, member: String) -> Result<(), &'static str> {
let system: System = get_system(&config_path);
let mut flag = false;
for mem in system.members {
for mem in &system.members {
if mem.name.to_lowercase() == member.to_lowercase() {
flag = true;
}
@ -128,7 +129,7 @@ fn set_member(config_path: String, member: String) -> Result<(), &'static str> {
if flag {
println!("Member {member} found");
get_fronters(&config["pk_key"].as_str().unwrap(), &system.pk_userid, &config["sp_key"].as_str().unwrap(), &system);
Ok(())
@ -218,6 +219,24 @@ fn pk_get_members(key: &str, sysid: &str) -> Vec<Value> {
return datas;
}
fn pk_get_fronters(key: &str, sysid: &str) -> Vec<String> {
let url = format!("{}/systems/{}/fronters", PK_URL, sysid);
let res = http_get_request(url,key);
let data: Value = serde_json::from_str(&res.unwrap()).unwrap();
let memberdata = &data["members"].as_array();
let mut members: Vec<String> = Vec::new();
for member in memberdata {
for m in member.into_iter() {
let mname = m["name"].as_str().unwrap();
members.push(String::from(mname));
}
}
return members;
}
#+end_src
**** Simplyplural
@ -256,6 +275,27 @@ fn get_sp_id(mem: &Member, ids: &HashMap<String, String>) -> String {
return member_id;
}
fn sp_get_fronters(key: &str, sys: &System) -> Vec<String> {
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();
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(push_name);
}
return members;
}
#+end_src
**** Utilities
@ -317,6 +357,15 @@ fn get_system(config_path: &str) -> System {
return sys;
}
fn get_fronters(pk_key: &str, pk_sysid: &str, sp_key: &str, sys: &System) -> HashMap<String, Vec<String>> {
let mut fronters: HashMap<String, Vec<String>> = HashMap::new();
fronters.insert(String::from("pk"), pk_get_fronters(pk_key, pk_sysid));
fronters.insert(String::from("sp"), sp_get_fronters(sp_key, sys));
println!("{:?}", fronters);
return fronters;
}
#+end_src
**** Http Request handler

View File

@ -23,6 +23,7 @@
***** Json loading [2/2]
****** Finished Add new function to get the config create empty in path if not exists
****** Finished `Get system` if Value::Null sync and load json
***** InProgress Get current front
** Kanban
| Backlog | LowPriority | HighPriority | InProgress | Finished |

View File

@ -21,6 +21,7 @@ const SP_URL: &str = "https://api.apparyllis.com/v1";
#[derive(Debug)]
#[derive(Serialize)]
#[derive(Deserialize)]
#[derive(Clone)]
struct System {
pk_userid: String,
sp_userid: String,
@ -82,7 +83,7 @@ fn set_member(config_path: String, member: String) -> Result<(), &'static str> {
let system: System = get_system(&config_path);
let mut flag = false;
for mem in system.members {
for mem in &system.members {
if mem.name.to_lowercase() == member.to_lowercase() {
flag = true;
}
@ -90,7 +91,7 @@ fn set_member(config_path: String, member: String) -> Result<(), &'static str> {
if flag {
println!("Member {member} found");
get_fronters(&config["pk_key"].as_str().unwrap(), &system.pk_userid, &config["sp_key"].as_str().unwrap(), &system);
Ok(())
@ -179,6 +180,24 @@ fn pk_get_members(key: &str, sysid: &str) -> Vec<Value> {
return datas;
}
fn pk_get_fronters(key: &str, sysid: &str) -> Vec<String> {
let url = format!("{}/systems/{}/fronters", PK_URL, sysid);
let res = http_get_request(url,key);
let data: Value = serde_json::from_str(&res.unwrap()).unwrap();
let memberdata = &data["members"].as_array();
let mut members: Vec<String> = Vec::new();
for member in memberdata {
for m in member.into_iter() {
let mname = m["name"].as_str().unwrap();
members.push(String::from(mname));
}
}
return members;
}
// Pluralkit:1 ends here
// [[file:../README.org::*Simplyplural][Simplyplural:1]]
@ -216,6 +235,27 @@ fn get_sp_id(mem: &Member, ids: &HashMap<String, String>) -> String {
return member_id;
}
fn sp_get_fronters(key: &str, sys: &System) -> Vec<String> {
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();
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(push_name);
}
return members;
}
// Simplyplural:1 ends here
// [[file:../README.org::*Utilities][Utilities:1]]
@ -276,6 +316,15 @@ fn get_system(config_path: &str) -> System {
return sys;
}
fn get_fronters(pk_key: &str, pk_sysid: &str, sp_key: &str, sys: &System) -> HashMap<String, Vec<String>> {
let mut fronters: HashMap<String, Vec<String>> = HashMap::new();
fronters.insert(String::from("pk"), pk_get_fronters(pk_key, pk_sysid));
fronters.insert(String::from("sp"), sp_get_fronters(sp_key, sys));
println!("{:?}", fronters);
return fronters;
}
// Utilities:1 ends here
// [[file:../README.org::*Http Request handler][Http Request handler:1]]