use bevy::{prelude::*, time::common_conditions::on_timer}; use bevy_http_client::prelude::*; use serde::Deserialize; #[derive(Debug, Clone, Deserialize, Default)] pub struct IpInfo { pub ip: String, } fn main() { let mut app = App::new(); app.add_plugins((MinimalPlugins, HttpClientPlugin)) .add_systems(Update, (handle_response, handle_error)) .add_systems( Update, send_request.run_if(on_timer(std::time::Duration::from_secs(1))), ); app.register_request_type::(); app.run(); } fn send_request(mut ev_request: EventWriter>) { ev_request.send( HttpClient::new() .get("https://api.ipify.org?format=json") .with_type::(), ); } // fn handle_response(mut ev_response: EventReader>) { // for response in ev_response.read() { // println!("ip: {}", response.ip); // } // } /// consume TypedResponse events fn handle_response(mut events: ResMut>>) { for response in events.drain() { let response: IpInfo = response.into_inner(); println!("ip info: {:?}", response); } } fn handle_error(mut ev_error: EventReader>) { for error in ev_error.read() { println!("Error retrieving IP: {}", error.err); } }