vesys-bank-server/http-client/src/api.rs

35 lines
1.0 KiB
Rust

use wasm_bindgen::JsValue;
use http_lib::json_account::JsonAccount;
use crate::client;
use crate::client::FetchError;
pub async fn fetch_account_nrs(host: &str) -> Result<Vec<String>, FetchError> {
match client::get_json(&format! {"{}/accounts", host}).await? {
None => Ok(vec![]),
Some(response) => {
let nrs: Vec<String> = serde_json::from_str(&response)?;
Ok(nrs)
}
}
}
pub async fn fetch_account(host: &str, nr: &str) -> Result<JsonAccount, FetchError> {
match client::get_json(&format! {"{}/accounts/{}", host, nr}).await? {
None => Err(FetchError {
err: JsValue::from_str("no such account"),
}),
Some(response) => {
let acc: JsonAccount = serde_json::from_str(&response)?;
Ok(acc)
}
}
}
pub async fn set_balance(host: &str, nr: &str, balance: f64) -> Result<(), FetchError> {
let data = JsValue::from_str(&format!("amount={}", balance));
client::put(&format! {"{}/accounts/{}", host, nr}, data).await?;
Ok(())
}