fetch data from api and serde it to shoppinglistentry

This commit is contained in:
mightypanders 2023-02-15 23:30:12 +01:00
parent 9907105d85
commit e1a782ea91

View File

@ -1,6 +1,9 @@
use cart_sync::models::{shopping_list_entry, ShoppingListEntry};
use reqwest::{self, Client};
use serde::Deserialize;
use std::env;
pub async fn fetch_shopping_list_entries(api_token: &str) {
pub async fn fetch_shopping_list_entries(api_token: &str) -> Option<Vec<ShoppingListEntry>> {
let base_url = env::var("TANDOOR_BASE_URL").expect("Base Url needed");
let route = "/shopping-list-entry/";
@ -10,22 +13,32 @@ pub async fn fetch_shopping_list_entries(api_token: &str) {
.get(&url)
.query(&[("checked", "false")])
.bearer_auth(api_token);
match req.send().await {
let response = req.send().await;
match response {
Ok(resolved) => match resolved.status() {
reqwest::StatusCode::OK => {
//println!("Success {:#?}", resolved);
println!("{}", resolved.text().await.unwrap());
println!("Watch me")
let json_body = resolved.json::<Vec<ShoppingListEntry>>().await;
match json_body {
Ok(j) => Some(j),
Err(e) => {
println!("{}", e);
None
}
}
}
reqwest::StatusCode::FORBIDDEN => {
println!("Denied {:#?}", resolved)
println!("Denied {:#?}", resolved);
None
}
_ => {
println!("Fail {:#?}", resolved);
None
}
},
Err(failed) => {
println!("There was an error in the reqwest: {failed}")
println!("There was an error in the reqwest: {failed}");
None
}
}
}