mirror of
https://github.com/zaphar/kitchen.git
synced 2025-07-22 19:40:14 -04:00
maint: Use gloo_net directly
This commit is contained in:
parent
d7cea46427
commit
c64605f9e7
@ -43,8 +43,8 @@ features = ["fmt", "time"]
|
|||||||
version = "0.4.22"
|
version = "0.4.22"
|
||||||
features = ["serde"]
|
features = ["serde"]
|
||||||
|
|
||||||
[dependencies.reqwasm]
|
[dependencies.gloo-net]
|
||||||
version = "0.5.0"
|
version = "0.4.0"
|
||||||
|
|
||||||
[dependencies.wasm-bindgen]
|
[dependencies.wasm-bindgen]
|
||||||
# we need wasm-bindgen v0.2.84 exactly
|
# we need wasm-bindgen v0.2.84 exactly
|
||||||
|
110
web/src/api.rs
110
web/src/api.rs
@ -15,7 +15,7 @@ use std::collections::{BTreeMap, BTreeSet};
|
|||||||
|
|
||||||
use base64::{self, Engine};
|
use base64::{self, Engine};
|
||||||
use chrono::NaiveDate;
|
use chrono::NaiveDate;
|
||||||
use reqwasm;
|
use gloo_net;
|
||||||
use serde_json::{from_str, to_string};
|
use serde_json::{from_str, to_string};
|
||||||
use sycamore::prelude::*;
|
use sycamore::prelude::*;
|
||||||
use tracing::{debug, error, instrument};
|
use tracing::{debug, error, instrument};
|
||||||
@ -66,8 +66,8 @@ impl From<std::string::FromUtf8Error> for Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<reqwasm::Error> for Error {
|
impl From<gloo_net::Error> for Error {
|
||||||
fn from(item: reqwasm::Error) -> Self {
|
fn from(item: gloo_net::Error) -> Self {
|
||||||
Error(format!("{:?}", item))
|
Error(format!("{:?}", item))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -94,8 +94,15 @@ impl LocalStore {
|
|||||||
|
|
||||||
pub fn store_app_state(&self, state: &AppState) {
|
pub fn store_app_state(&self, state: &AppState) {
|
||||||
self.migrate_local_store();
|
self.migrate_local_store();
|
||||||
|
let state = match to_string(state) {
|
||||||
|
Ok(state) => state,
|
||||||
|
Err(err) => {
|
||||||
|
error!(?err, ?state, "Error deserializing app_state");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
self.store
|
self.store
|
||||||
.set("app_state", &to_string(state).unwrap())
|
.set("app_state", &state)
|
||||||
.expect("Failed to set our app state");
|
.expect("Failed to set our app state");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -266,13 +273,16 @@ impl HttpStore {
|
|||||||
debug!("attempting login request against api.");
|
debug!("attempting login request against api.");
|
||||||
let mut path = self.v2_path();
|
let mut path = self.v2_path();
|
||||||
path.push_str("/auth");
|
path.push_str("/auth");
|
||||||
let result = reqwasm::http::Request::get(&path)
|
let request = gloo_net::http::Request::get(&path)
|
||||||
.header(
|
.header(
|
||||||
"Authorization",
|
"authorization",
|
||||||
format!("Basic {}", token68(user, pass)).as_str(),
|
format!("Basic {}", token68(user, pass)).as_str(),
|
||||||
)
|
)
|
||||||
.send()
|
.mode(web_sys::RequestMode::SameOrigin)
|
||||||
.await;
|
.credentials(web_sys::RequestCredentials::SameOrigin)
|
||||||
|
.build().expect("Failed to build request");
|
||||||
|
debug!(?request, "Sending auth request");
|
||||||
|
let result = request.send().await;
|
||||||
if let Ok(resp) = &result {
|
if let Ok(resp) = &result {
|
||||||
if resp.status() == 200 {
|
if resp.status() == 200 {
|
||||||
let user_data = resp
|
let user_data = resp
|
||||||
@ -294,7 +304,7 @@ impl HttpStore {
|
|||||||
debug!("Retrieving User Account data");
|
debug!("Retrieving User Account data");
|
||||||
let mut path = self.v2_path();
|
let mut path = self.v2_path();
|
||||||
path.push_str("/account");
|
path.push_str("/account");
|
||||||
let result = reqwasm::http::Request::get(&path).send().await;
|
let result = gloo_net::http::Request::get(&path).send().await;
|
||||||
if let Ok(resp) = &result {
|
if let Ok(resp) = &result {
|
||||||
if resp.status() == 200 {
|
if resp.status() == 200 {
|
||||||
let user_data = resp
|
let user_data = resp
|
||||||
@ -315,9 +325,9 @@ impl HttpStore {
|
|||||||
pub async fn fetch_categories(&self) -> Result<Option<Vec<(String, String)>>, Error> {
|
pub async fn fetch_categories(&self) -> Result<Option<Vec<(String, String)>>, Error> {
|
||||||
let mut path = self.v2_path();
|
let mut path = self.v2_path();
|
||||||
path.push_str("/category_map");
|
path.push_str("/category_map");
|
||||||
let resp = match reqwasm::http::Request::get(&path).send().await {
|
let resp = match gloo_net::http::Request::get(&path).send().await {
|
||||||
Ok(resp) => resp,
|
Ok(resp) => resp,
|
||||||
Err(reqwasm::Error::JsError(err)) => {
|
Err(gloo_net::Error::JsError(err)) => {
|
||||||
error!(path, ?err, "Error hitting api");
|
error!(path, ?err, "Error hitting api");
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
}
|
}
|
||||||
@ -345,9 +355,9 @@ impl HttpStore {
|
|||||||
pub async fn fetch_recipes(&self) -> Result<Option<Vec<RecipeEntry>>, Error> {
|
pub async fn fetch_recipes(&self) -> Result<Option<Vec<RecipeEntry>>, Error> {
|
||||||
let mut path = self.v2_path();
|
let mut path = self.v2_path();
|
||||||
path.push_str("/recipes");
|
path.push_str("/recipes");
|
||||||
let resp = match reqwasm::http::Request::get(&path).send().await {
|
let resp = match gloo_net::http::Request::get(&path).send().await {
|
||||||
Ok(resp) => resp,
|
Ok(resp) => resp,
|
||||||
Err(reqwasm::Error::JsError(err)) => {
|
Err(gloo_net::Error::JsError(err)) => {
|
||||||
error!(path, ?err, "Error hitting api");
|
error!(path, ?err, "Error hitting api");
|
||||||
return Ok(self.local_store.get_recipes());
|
return Ok(self.local_store.get_recipes());
|
||||||
}
|
}
|
||||||
@ -375,9 +385,9 @@ impl HttpStore {
|
|||||||
let mut path = self.v2_path();
|
let mut path = self.v2_path();
|
||||||
path.push_str("/recipe/");
|
path.push_str("/recipe/");
|
||||||
path.push_str(id.as_ref());
|
path.push_str(id.as_ref());
|
||||||
let resp = match reqwasm::http::Request::get(&path).send().await {
|
let resp = match gloo_net::http::Request::get(&path).send().await {
|
||||||
Ok(resp) => resp,
|
Ok(resp) => resp,
|
||||||
Err(reqwasm::Error::JsError(err)) => {
|
Err(gloo_net::Error::JsError(err)) => {
|
||||||
error!(path, ?err, "Error hitting api");
|
error!(path, ?err, "Error hitting api");
|
||||||
return Ok(self.local_store.get_recipe_entry(id.as_ref()));
|
return Ok(self.local_store.get_recipe_entry(id.as_ref()));
|
||||||
}
|
}
|
||||||
@ -413,7 +423,7 @@ impl HttpStore {
|
|||||||
let mut path = self.v2_path();
|
let mut path = self.v2_path();
|
||||||
path.push_str("/recipe");
|
path.push_str("/recipe");
|
||||||
path.push_str(&format!("/{}", recipe.as_ref()));
|
path.push_str(&format!("/{}", recipe.as_ref()));
|
||||||
let resp = reqwasm::http::Request::delete(&path).send().await?;
|
let resp = gloo_net::http::Request::delete(&path).send().await?;
|
||||||
if resp.status() != 200 {
|
if resp.status() != 200 {
|
||||||
Err(format!("Status: {}", resp.status()).into())
|
Err(format!("Status: {}", resp.status()).into())
|
||||||
} else {
|
} else {
|
||||||
@ -431,10 +441,9 @@ impl HttpStore {
|
|||||||
return Err("Recipe Ids can not be empty".into());
|
return Err("Recipe Ids can not be empty".into());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let serialized = to_string(&recipes).expect("Unable to serialize recipe entries");
|
let resp = gloo_net::http::Request::post(&path)
|
||||||
let resp = reqwasm::http::Request::post(&path)
|
.json(&recipes)
|
||||||
.body(&serialized)
|
.expect("Failed to set body")
|
||||||
.header("content-type", "application/json")
|
|
||||||
.send()
|
.send()
|
||||||
.await?;
|
.await?;
|
||||||
if resp.status() != 200 {
|
if resp.status() != 200 {
|
||||||
@ -449,9 +458,9 @@ impl HttpStore {
|
|||||||
pub async fn store_categories(&self, categories: &Vec<(String, String)>) -> Result<(), Error> {
|
pub async fn store_categories(&self, categories: &Vec<(String, String)>) -> Result<(), Error> {
|
||||||
let mut path = self.v2_path();
|
let mut path = self.v2_path();
|
||||||
path.push_str("/category_map");
|
path.push_str("/category_map");
|
||||||
let resp = reqwasm::http::Request::post(&path)
|
let resp = gloo_net::http::Request::post(&path)
|
||||||
.body(to_string(&categories).expect("Unable to encode categories as json"))
|
.json(&categories)
|
||||||
.header("content-type", "application/json")
|
.expect("Failed to set body")
|
||||||
.send()
|
.send()
|
||||||
.await?;
|
.await?;
|
||||||
if resp.status() != 200 {
|
if resp.status() != 200 {
|
||||||
@ -503,9 +512,9 @@ impl HttpStore {
|
|||||||
pub async fn store_plan(&self, plan: Vec<(String, i32)>) -> Result<(), Error> {
|
pub async fn store_plan(&self, plan: Vec<(String, i32)>) -> Result<(), Error> {
|
||||||
let mut path = self.v2_path();
|
let mut path = self.v2_path();
|
||||||
path.push_str("/plan");
|
path.push_str("/plan");
|
||||||
let resp = reqwasm::http::Request::post(&path)
|
let resp = gloo_net::http::Request::post(&path)
|
||||||
.body(to_string(&plan).expect("Unable to encode plan as json"))
|
.json(&plan)
|
||||||
.header("content-type", "application/json")
|
.expect("Failed to set body")
|
||||||
.send()
|
.send()
|
||||||
.await?;
|
.await?;
|
||||||
if resp.status() != 200 {
|
if resp.status() != 200 {
|
||||||
@ -525,9 +534,9 @@ impl HttpStore {
|
|||||||
path.push_str("/plan");
|
path.push_str("/plan");
|
||||||
path.push_str("/at");
|
path.push_str("/at");
|
||||||
path.push_str(&format!("/{}", date));
|
path.push_str(&format!("/{}", date));
|
||||||
let resp = reqwasm::http::Request::post(&path)
|
let resp = gloo_net::http::Request::post(&path)
|
||||||
.body(to_string(&plan).expect("Unable to encode plan as json"))
|
.json(&plan)
|
||||||
.header("content-type", "application/json")
|
.expect("Failed to set body")
|
||||||
.send()
|
.send()
|
||||||
.await?;
|
.await?;
|
||||||
if resp.status() != 200 {
|
if resp.status() != 200 {
|
||||||
@ -542,7 +551,7 @@ impl HttpStore {
|
|||||||
let mut path = self.v2_path();
|
let mut path = self.v2_path();
|
||||||
path.push_str("/plan");
|
path.push_str("/plan");
|
||||||
path.push_str("/all");
|
path.push_str("/all");
|
||||||
let resp = reqwasm::http::Request::get(&path).send().await?;
|
let resp = gloo_net::http::Request::get(&path).send().await?;
|
||||||
if resp.status() != 200 {
|
if resp.status() != 200 {
|
||||||
Err(format!("Status: {}", resp.status()).into())
|
Err(format!("Status: {}", resp.status()).into())
|
||||||
} else {
|
} else {
|
||||||
@ -561,7 +570,7 @@ impl HttpStore {
|
|||||||
path.push_str("/plan");
|
path.push_str("/plan");
|
||||||
path.push_str("/at");
|
path.push_str("/at");
|
||||||
path.push_str(&format!("/{}", date));
|
path.push_str(&format!("/{}", date));
|
||||||
let resp = reqwasm::http::Request::delete(&path).send().await?;
|
let resp = gloo_net::http::Request::delete(&path).send().await?;
|
||||||
if resp.status() != 200 {
|
if resp.status() != 200 {
|
||||||
Err(format!("Status: {}", resp.status()).into())
|
Err(format!("Status: {}", resp.status()).into())
|
||||||
} else {
|
} else {
|
||||||
@ -577,7 +586,7 @@ impl HttpStore {
|
|||||||
path.push_str("/plan");
|
path.push_str("/plan");
|
||||||
path.push_str("/at");
|
path.push_str("/at");
|
||||||
path.push_str(&format!("/{}", date));
|
path.push_str(&format!("/{}", date));
|
||||||
let resp = reqwasm::http::Request::get(&path).send().await?;
|
let resp = gloo_net::http::Request::get(&path).send().await?;
|
||||||
if resp.status() != 200 {
|
if resp.status() != 200 {
|
||||||
Err(format!("Status: {}", resp.status()).into())
|
Err(format!("Status: {}", resp.status()).into())
|
||||||
} else {
|
} else {
|
||||||
@ -594,7 +603,7 @@ impl HttpStore {
|
|||||||
//pub async fn fetch_plan(&self) -> Result<Option<Vec<(String, i32)>>, Error> {
|
//pub async fn fetch_plan(&self) -> Result<Option<Vec<(String, i32)>>, Error> {
|
||||||
// let mut path = self.v2_path();
|
// let mut path = self.v2_path();
|
||||||
// path.push_str("/plan");
|
// path.push_str("/plan");
|
||||||
// let resp = reqwasm::http::Request::get(&path).send().await?;
|
// let resp = gloo_net::http::Request::get(&path).send().await?;
|
||||||
// if resp.status() != 200 {
|
// if resp.status() != 200 {
|
||||||
// Err(format!("Status: {}", resp.status()).into())
|
// Err(format!("Status: {}", resp.status()).into())
|
||||||
// } else {
|
// } else {
|
||||||
@ -623,7 +632,7 @@ impl HttpStore {
|
|||||||
path.push_str("/inventory");
|
path.push_str("/inventory");
|
||||||
path.push_str("/at");
|
path.push_str("/at");
|
||||||
path.push_str(&format!("/{}", date));
|
path.push_str(&format!("/{}", date));
|
||||||
let resp = reqwasm::http::Request::get(&path).send().await?;
|
let resp = gloo_net::http::Request::get(&path).send().await?;
|
||||||
if resp.status() != 200 {
|
if resp.status() != 200 {
|
||||||
Err(format!("Status: {}", resp.status()).into())
|
Err(format!("Status: {}", resp.status()).into())
|
||||||
} else {
|
} else {
|
||||||
@ -658,7 +667,7 @@ impl HttpStore {
|
|||||||
> {
|
> {
|
||||||
let mut path = self.v2_path();
|
let mut path = self.v2_path();
|
||||||
path.push_str("/inventory");
|
path.push_str("/inventory");
|
||||||
let resp = reqwasm::http::Request::get(&path).send().await?;
|
let resp = gloo_net::http::Request::get(&path).send().await?;
|
||||||
if resp.status() != 200 {
|
if resp.status() != 200 {
|
||||||
Err(format!("Status: {}", resp.status()).into())
|
Err(format!("Status: {}", resp.status()).into())
|
||||||
} else {
|
} else {
|
||||||
@ -695,13 +704,10 @@ impl HttpStore {
|
|||||||
path.push_str(&format!("/{}", date));
|
path.push_str(&format!("/{}", date));
|
||||||
let filtered_ingredients: Vec<IngredientKey> = filtered_ingredients.into_iter().collect();
|
let filtered_ingredients: Vec<IngredientKey> = filtered_ingredients.into_iter().collect();
|
||||||
let modified_amts: Vec<(IngredientKey, String)> = modified_amts.into_iter().collect();
|
let modified_amts: Vec<(IngredientKey, String)> = modified_amts.into_iter().collect();
|
||||||
debug!("Storing inventory data in cache");
|
|
||||||
let serialized_inventory = to_string(&(filtered_ingredients, modified_amts, extra_items))
|
|
||||||
.expect("Unable to encode plan as json");
|
|
||||||
debug!("Storing inventory data via API");
|
debug!("Storing inventory data via API");
|
||||||
let resp = reqwasm::http::Request::post(&path)
|
let resp = gloo_net::http::Request::post(&path)
|
||||||
.body(&serialized_inventory)
|
.json(&(filtered_ingredients, modified_amts, extra_items))
|
||||||
.header("content-type", "application/json")
|
.expect("Failed to set body")
|
||||||
.send()
|
.send()
|
||||||
.await?;
|
.await?;
|
||||||
if resp.status() != 200 {
|
if resp.status() != 200 {
|
||||||
@ -724,13 +730,10 @@ impl HttpStore {
|
|||||||
path.push_str("/inventory");
|
path.push_str("/inventory");
|
||||||
let filtered_ingredients: Vec<IngredientKey> = filtered_ingredients.into_iter().collect();
|
let filtered_ingredients: Vec<IngredientKey> = filtered_ingredients.into_iter().collect();
|
||||||
let modified_amts: Vec<(IngredientKey, String)> = modified_amts.into_iter().collect();
|
let modified_amts: Vec<(IngredientKey, String)> = modified_amts.into_iter().collect();
|
||||||
debug!("Storing inventory data in cache");
|
|
||||||
let serialized_inventory = to_string(&(filtered_ingredients, modified_amts, extra_items))
|
|
||||||
.expect("Unable to encode plan as json");
|
|
||||||
debug!("Storing inventory data via API");
|
debug!("Storing inventory data via API");
|
||||||
let resp = reqwasm::http::Request::post(&path)
|
let resp = gloo_net::http::Request::post(&path)
|
||||||
.body(&serialized_inventory)
|
.json(&(filtered_ingredients, modified_amts, extra_items))
|
||||||
.header("content-type", "application/json")
|
.expect("Failed to set body")
|
||||||
.send()
|
.send()
|
||||||
.await?;
|
.await?;
|
||||||
if resp.status() != 200 {
|
if resp.status() != 200 {
|
||||||
@ -745,7 +748,7 @@ impl HttpStore {
|
|||||||
pub async fn fetch_staples(&self) -> Result<Option<String>, Error> {
|
pub async fn fetch_staples(&self) -> Result<Option<String>, Error> {
|
||||||
let mut path = self.v2_path();
|
let mut path = self.v2_path();
|
||||||
path.push_str("/staples");
|
path.push_str("/staples");
|
||||||
let resp = reqwasm::http::Request::get(&path).send().await?;
|
let resp = gloo_net::http::Request::get(&path).send().await?;
|
||||||
if resp.status() != 200 {
|
if resp.status() != 200 {
|
||||||
debug!("Invalid response back");
|
debug!("Invalid response back");
|
||||||
Err(format!("Status: {}", resp.status()).into())
|
Err(format!("Status: {}", resp.status()).into())
|
||||||
@ -759,15 +762,12 @@ impl HttpStore {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn store_staples<S: AsRef<str>>(&self, content: S) -> Result<(), Error> {
|
pub async fn store_staples<S: AsRef<str> + serde::Serialize>(&self, content: S) -> Result<(), Error> {
|
||||||
let mut path = self.v2_path();
|
let mut path = self.v2_path();
|
||||||
path.push_str("/staples");
|
path.push_str("/staples");
|
||||||
let serialized_staples: String =
|
let resp = gloo_net::http::Request::post(&path)
|
||||||
to_string(content.as_ref()).expect("Failed to serialize staples to json");
|
.json(&content)
|
||||||
|
.expect("Failed to set body")
|
||||||
let resp = reqwasm::http::Request::post(&path)
|
|
||||||
.body(&serialized_staples)
|
|
||||||
.header("content-type", "application/json")
|
|
||||||
.send()
|
.send()
|
||||||
.await?;
|
.await?;
|
||||||
if resp.status() != 200 {
|
if resp.status() != 200 {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user