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