When my app starts I need to make a reqwest request every 30 minutes, I tried the loop function with thread::sleep() but when the loop function starts, my main code stops.
This is my main.rs file
.setup(|app| {
let config = app::storage::config::read(&app.config()).unwrap();
if config.show_window_on_start {
app::window::new(app.handle());
}
app::api::firebase::set_ip(server::network::get_ip(), &app.config());
Ok(())
})
firebase.rs with set_ip function
#[tokio::main]
pub async fn set_ip(ip: std::net::IpAddr, config: &tauri::Config) {
let client = Client::new();
let user = config::read(config).unwrap().user_data;
let token = auth(user.email.as_str(), user.password.as_str()).await.unwrap();
let response = client
.get(
format!(
"https://firestore.googleapis.com/v1/my_db/(default)/documents/users/{}",
user.email
)
)
.header("Authorization", format!("Bearer {}", token))
.header("content-type", "application/json")
.send().await
.unwrap()
.text().await
.unwrap();
let mut data: serde_json::Value = serde_json::from_str(&response).expect("ERROR parsing");
data["fields"]["ip"]["stringValue"] = serde_json::Value::String(ip.to_string());
let _post_ip = client
.patch(
format!(
"https://firestore.googleapis.com/v1/projects/my_db/documents/users/{}",
user.email
)
)
.body(serde_json::to_string(&data).expect("Error stringing value"))
.header("Authorization", format!("Bearer {}", token))
.header("content-type", "application/json")
.send().await
.unwrap();
}