From 975c0b735be0808883116616d934ee257a24fb08 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Thu, 25 Apr 2024 17:41:52 +0200 Subject: cli: Add command to edit clients. Not entirely happy with this one. Specially the patch method of the Connection struct. While the other method take (or return) an object that can be serialized to json, the patch method takes a string slice directly, and passing it to the API. --- cli/src/api/client.rs | 6 ++++++ cli/src/api/connection.rs | 8 ++++++++ cli/src/main.rs | 5 +++++ 3 files changed, 19 insertions(+) diff --git a/cli/src/api/client.rs b/cli/src/api/client.rs index c77063b..df0ca96 100644 --- a/cli/src/api/client.rs +++ b/cli/src/api/client.rs @@ -28,6 +28,12 @@ impl Client { Ok(conn.get("clients")?) } + pub fn update(conn: Connection, client_id: u32, data: &str) -> Result<(), Box> { + let resp = conn.patch(&format!("clients?id=eq.{}", client_id), data)?; + println!("{}", resp); + Ok(()) + } + pub fn save(&self, conn: Connection) -> Result<(), Box> { // By default the `id` of a newly created client will be 0, // which will cause the API to add the client with id=0. This works once diff --git a/cli/src/api/connection.rs b/cli/src/api/connection.rs index f1d7f1c..5505afb 100644 --- a/cli/src/api/connection.rs +++ b/cli/src/api/connection.rs @@ -33,4 +33,12 @@ impl Connection { .send_json(&data)? .into_string()?) } + + pub fn patch(&self, resource: &str, data: &str) -> Result> { + Ok(ureq::patch(&format!("{}/{}", &self.url, resource)) + .set("Authorization", &format!("Bearer {}", self.jwt_token)) + .set("Content-Type", "application/json") + .send_string(&data)? + .into_string()?) + } } diff --git a/cli/src/main.rs b/cli/src/main.rs index ee10d17..794db9f 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -26,6 +26,11 @@ fn main() -> Result<(), Box> { add_client(conn, &args.next() .expect("expected new client json data"))?; }, + "--edit-client" => { + let client_id = args.next().expect("missing client id").parse()?; + let json = args.next().expect("missing json data"); + Client::update(conn, client_id, &json)?; + }, &_ => { println!("Unknown command: {}", cmd); } -- cgit v1.2.3