add assign + login

This commit is contained in:
JMARyA 2024-06-06 15:28:49 +02:00
parent c2944722dd
commit b046374280
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
4 changed files with 85 additions and 2 deletions

View file

@ -259,4 +259,46 @@ impl VikunjaAPI {
);
serde_json::from_str(&resp).unwrap()
}
pub fn login(&self, username: &str, password: &str, totp: Option<&str>) -> String {
let resp = self.post_request(
"/login",
&serde_json::json!({
"username": username,
"password": password,
"totp_passcode": totp
}),
);
let val: serde_json::Value = serde_json::from_str(&resp).unwrap();
val.as_object()
.unwrap()
.get("token")
.unwrap()
.as_str()
.unwrap()
.to_string()
}
pub fn search_user(&self, search: &str) -> Option<Vec<User>> {
let resp = self.get_request(&format!("/users?s={search}"));
serde_json::from_str(&resp).ok()
}
pub fn assign_to_task(&self, user: &str, task_id: isize) {
let user = self.search_user(user).unwrap();
self.put_request(
&format!("/tasks/{task_id}/assignees"),
&serde_json::json!({
"user_id": user.first().unwrap().id
}),
);
}
pub fn remove_assign_to_task(&self, user: &str, task_id: isize) {
let user = self.search_user(user).unwrap();
let user_id = user.first().unwrap().id;
self.delete_request(&format!("/tasks/{task_id}/assignees/{user_id}"));
}
}

View file

@ -32,6 +32,22 @@ pub fn get_args() -> clap::ArgMatches {
.default_value("Inbox"),
),
)
.subcommand(
command!()
.name("login")
.about("Get a JWT Token for authentication")
.arg(arg!(-u --username <username> "Username").required(true))
.arg(arg!(-p --password <password> "Password").required(true))
.arg(arg!(--totp <totp> "TOTP Code").required(false)),
)
.subcommand(
command!()
.name("assign")
.about("Assign a user to a task")
.arg(arg!(-u --undo "Remove label from task").required(false))
.arg(arg!([user] "User").required(true))
.arg(arg!([task_id] "Task ID").required(true)),
)
.subcommand(
command!()
.name("label")

View file

@ -16,6 +16,14 @@ fn main() {
let task_id: &String = task_info_arg.get_one("task_id").unwrap();
ui::task::print_task_info(task_id.parse().unwrap(), &api);
}
Some(("login", login_arg)) => {
let username: &String = login_arg.get_one("username").unwrap();
let password: &String = login_arg.get_one("password").unwrap();
let totp: Option<&String> = login_arg.get_one("totp");
let token = api.login(username, password, totp.map(|x| x.as_str()));
println!("\"token\" = \"{token}\"");
}
Some(("prj", prj_arg)) => match prj_arg.subcommand() {
Some(("ls", _)) => {
ui::project::list_projects(&api);
@ -25,6 +33,17 @@ fn main() {
ui::project::list_projects(&api);
}
},
Some(("assign", assign_arg)) => {
let user: &String = assign_arg.get_one("user").unwrap();
let task_id: &String = assign_arg.get_one("task_id").unwrap();
let undo = assign_arg.get_flag("undo");
if undo {
api.remove_assign_to_task(user, task_id.parse().unwrap());
} else {
api.assign_to_task(user, task_id.parse().unwrap());
}
}
Some(("labels", label_args)) => match label_args.subcommand() {
Some(("ls", _)) => {
ui::print_all_labels(&api);

View file

@ -135,7 +135,13 @@ pub fn print_task_info(task_id: isize, api: &VikunjaAPI) {
println!("---\n{}", task.description);
}
//pub assignees: Option<Vec<String>>,
//pub labels: Option<Vec<Label>>,
if let Some(assigned) = task.assignees {
print!("Assigned to: ");
for assignee in assigned {
print!("{} ", assignee.username);
}
println!();
}
// pub percent_done: f64,
}