diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2024-04-24 11:27:42 +0200 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2024-04-24 11:27:42 +0200 |
commit | a4dbf815cc418514a268ca54464c2d1b9c413055 (patch) | |
tree | 81d6a5944a00839f37c7d9ea469274f8d8425338 /cli/src/api/client.rs | |
parent | 1c3e4dba9fe90faa318933a99eecdb9cd5e7481a (diff) | |
download | faktura-a4dbf815cc418514a268ca54464c2d1b9c413055.tar.gz faktura-a4dbf815cc418514a268ca54464c2d1b9c413055.tar.bz2 faktura-a4dbf815cc418514a268ca54464c2d1b9c413055.zip |
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.
Diffstat (limited to 'cli/src/api/client.rs')
-rw-r--r-- | cli/src/api/client.rs | 23 |
1 files changed, 16 insertions, 7 deletions
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<String>, pub address: Option<String>, pub email: String, pub phone: Option<String>, + + #[serde(default)] pub vat: bool, } impl Client { - pub fn all() -> Result<Vec<Self>, Box<dyn Error>> { - Ok(ureq::get("http://faktura.ddev.site/api/clients") - .set("Accept", "application/json") - .call()? - .into_json()?) + pub fn all(conn: Connection) -> Result<Vec<Self>, Box<dyn Error>> { + Ok(conn.get("clients")?) + } + + pub fn save(&self, conn: Connection) -> Result<(), Box<dyn Error>> { + let resp = conn.post("clients", &self)?; + + println!("{}", resp); + Ok(()) } } |