From a4dbf815cc418514a268ca54464c2d1b9c413055 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Wed, 24 Apr 2024 11:27:42 +0200 Subject: cli: Add feature to save clients to the db. Also introduces a Connection object to handle the actual connection to the remote API. We also load the remote API url and jwt token from the process environment so these are no longer hardcoded into the source code. --- cli/Cargo.toml | 1 + cli/src/api.rs | 2 ++ cli/src/api/client.rs | 23 ++++++++++++++++------- cli/src/api/connection.rs | 36 ++++++++++++++++++++++++++++++++++++ cli/src/main.rs | 35 +++++++++++++++++++++++++++++++++-- 5 files changed, 88 insertions(+), 9 deletions(-) create mode 100644 cli/src/api/connection.rs diff --git a/cli/Cargo.toml b/cli/Cargo.toml index be0f14d..f84695f 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -12,4 +12,5 @@ edition = "2021" [dependencies] serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" ureq = { version = "2.9.6", features = ["json", "native-certs"] } diff --git a/cli/src/api.rs b/cli/src/api.rs index 594530a..da7eb5b 100644 --- a/cli/src/api.rs +++ b/cli/src/api.rs @@ -4,5 +4,7 @@ // SPDX-License-Identifier: AGPL-3.0-or-later pub mod client; +pub mod connection; pub use client::Client; +pub use connection::Connection; diff --git a/cli/src/api/client.rs b/cli/src/api/client.rs index eee59b0..9c46db3 100644 --- a/cli/src/api/client.rs +++ b/cli/src/api/client.rs @@ -3,25 +3,34 @@ // // SPDX-License-Identifier: AGPL-3.0-or-later -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use std::error::Error; +use super::Connection; -#[derive(Debug, Deserialize)] +#[derive(Debug, Default, Deserialize, Serialize)] pub struct Client { + #[serde(default)] pub id: u32, + pub name: String, pub contact: Option, pub address: Option, pub email: String, pub phone: Option, + + #[serde(default)] pub vat: bool, } impl Client { - pub fn all() -> Result, Box> { - Ok(ureq::get("http://faktura.ddev.site/api/clients") - .set("Accept", "application/json") - .call()? - .into_json()?) + pub fn all(conn: Connection) -> Result, Box> { + Ok(conn.get("clients")?) + } + + pub fn save(&self, conn: Connection) -> Result<(), Box> { + let resp = conn.post("clients", &self)?; + + println!("{}", resp); + Ok(()) } } diff --git a/cli/src/api/connection.rs b/cli/src/api/connection.rs new file mode 100644 index 0000000..f1d7f1c --- /dev/null +++ b/cli/src/api/connection.rs @@ -0,0 +1,36 @@ +// SPDX-FileCopyrightText: 2024 Eilertsens Kodeknekkeri +// SPDX-FileCopyrightText: 2024 Harald Eilertsen +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +use serde::{Deserialize, Serialize}; +use std::error::Error; + +pub struct Connection { + pub url: String, + pub jwt_token: String, +} + +impl Connection { + pub fn new>(url: T, jwt_token: T) -> Connection { + Connection { + url: url.into(), + jwt_token: jwt_token.into(), + } + } + + pub fn get Deserialize<'de>>(&self, resource: &str) -> Result> { + Ok(ureq::get(&format!("{}/{}", self.url, resource)) + .set("Accept", "application/json") + .call()? + .into_json()?) + } + + pub fn post(&self, resource: &str, data: &T) -> Result> { + Ok(ureq::post(&format!("{}/{}", self.url, resource)) + .set("Authorization", &format!("Bearer {}", self.jwt_token)) + .set("Content-Type", "application/json") + .send_json(&data)? + .into_string()?) + } +} diff --git a/cli/src/main.rs b/cli/src/main.rs index 4c34e18..ee10d17 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -7,11 +7,36 @@ mod api; -use api::Client; +use api::{Client, Connection}; fn main() -> Result<(), Box> { - let clients = Client::all()?; + let conn = Connection::new( + std::env::var("FAKTURA_API_URL")?, + std::env::var("FAKTURA_JWT_TOKEN")? + ); + + let mut args = std::env::args().skip(1); + if let Some(cmd) = args.next() { + match cmd.as_str() { + "--list-clients" => { + list_clients(conn)?; + }, + "--add-client" => { + add_client(conn, &args.next() + .expect("expected new client json data"))?; + }, + &_ => { + println!("Unknown command: {}", cmd); + } + } + } + + Ok(()) +} + +fn list_clients(conn: Connection) -> Result<(), Box> { + let clients = Client::all(conn)?; for c in clients { print!("{}: {} <{}>", c.id, c.name, c.email); @@ -32,3 +57,9 @@ fn main() -> Result<(), Box> { Ok(()) } + +fn add_client(conn: Connection, json: &str) -> Result<(), Box> { + let client: Client = serde_json::from_str(json)?; + println!("{:?}", client); + client.save(conn) +} -- cgit v1.2.3