diff --git a/Cargo.toml b/Cargo.toml index 2bdf209..48d415f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,3 +14,4 @@ tokio = { version = "1.39.2", features = ["full"] } tokio-macros = { version = "2.4.0" } async-trait = "0.1.86" futures = "0.3.31" +reqwest = "0.12.12" diff --git a/src/app/metar/mod.rs b/src/app/metar/mod.rs index ae2b9c3..71e61a7 100644 --- a/src/app/metar/mod.rs +++ b/src/app/metar/mod.rs @@ -1,12 +1,12 @@ use super::req::Requests; -#[derive(PartialEq, Eq, Debug)] +#[derive(PartialEq, Eq, Debug, Clone)] pub struct Metar { pub api: Requests, } impl Default for Metar { fn default() -> Self { - Metar { api: Requests{} } + Metar { api: Requests::new() } } } diff --git a/src/app/req/mod.rs b/src/app/req/mod.rs index a006569..08ddc1f 100644 --- a/src/app/req/mod.rs +++ b/src/app/req/mod.rs @@ -1,28 +1,35 @@ use std::error::Error; +use crate::error::AvBagError; +use reqwest::Client; -use tokio::{io::AsyncReadExt, net::TcpStream}; +#[derive(Debug, Clone)] +pub struct Requests { + client: Client, +} -#[derive(PartialEq, Eq, Debug)] -pub struct Requests {} - -impl Requests { - pub async fn make_request(&self, target: String) -> Result> { - - let tasks = tokio::spawn(async { - let mut buf: String = String::from(""); - - let mut stream = match TcpStream::connect(target).await { - Ok(stream) => stream, - Err(err) => return Err(Box::new(err)), - }; - // Buffer to read HTTP request into - let _ = stream.read_to_string(&mut buf); - - Ok(buf) - }); - - let buf = tasks.await??; - - Ok(buf) +impl PartialEq for Requests { + fn eq(&self, _: &Self) -> bool { + return true; } } + +impl Eq for Requests {} + +impl Requests { + pub fn new() -> Self { + Requests { + client: Client::new(), + } + } + + pub async fn make_request(&self, target: String) -> Result> { + let response = self.client.get(&target).send().await?; + + if response.status().is_success() { + let body = response.text().await?; + Ok(body) + } else { + Err(Box::new(AvBagError::RequestNotAvailable)) + } + } +} \ No newline at end of file diff --git a/src/app/ui/mod.rs b/src/app/ui/mod.rs index 054e44d..5702515 100644 --- a/src/app/ui/mod.rs +++ b/src/app/ui/mod.rs @@ -13,13 +13,12 @@ impl<'a> eframe::App for App<'a> { // Display tabs for main UI match self.tab_state.get_current_tab() { Some(tab) => { - println!("Displaying tab"); block_on(tab.display_tab(ctx, &frame)); }, - // What to do if the tab is not available + // TODO: What to do if the tab is not available None => { - println!("No tab available"); + eprintln!("Tab not available"); }, }; diff --git a/src/app/ui/tabs/metar_tab.rs b/src/app/ui/tabs/metar_tab.rs index 48f3648..9114212 100644 --- a/src/app/ui/tabs/metar_tab.rs +++ b/src/app/ui/tabs/metar_tab.rs @@ -1,15 +1,19 @@ use egui::Key; use async_trait::async_trait; +use tokio::task; + +use std::sync::Arc; +use tokio::sync::Mutex; use crate::app::metar::Metar; use super::Tab; -#[derive(Debug, PartialEq, Eq)] +#[derive(Debug)] pub struct MetarTab { pub icaos: Vec, pub metars: Vec, - pub metar: Metar + pub metar: Arc> } impl Default for MetarTab { @@ -17,16 +21,23 @@ impl Default for MetarTab { Self { icaos: vec!["".to_string()], metars: vec![], - metar: Metar::default() + metar: Arc::new(Mutex::new(Metar::default())), } } } +impl PartialEq for MetarTab { + fn eq(&self, other: &Self) -> bool { + self.icaos == other.icaos && self.metars == other.metars + } +} + +impl Eq for MetarTab {} + #[async_trait] impl Tab for MetarTab { async fn display_tab(&mut self, ctx: &egui::Context, frame: &egui::Frame) { - println!("Displaying METAR Tab"); - egui::CentralPanel::default().frame(*frame).show(ctx, |ui| { + egui::CentralPanel::default().frame(*frame).show(ctx, |ui|{ // Given Key pressed, place focus on next item let new_icao_focus: bool = ui.input(|i| i.key_pressed(Key::Space)); @@ -36,6 +47,17 @@ impl Tab for MetarTab { if ui.button("Add More").clicked() || new_icao_focus { self.icaos.push("".to_string()); + + // TODO: Improve error handling here + // Spawn an async task to handle the async operation + let metar_clone = Arc::clone(&self.metar); + task::spawn(async move { + let metar = metar_clone.lock().await; + match metar.api.make_request(String::from("https://google.co.uk")).await { + Ok(response) => println!("{:?}", response), + Err(e) => eprintln!("Request failed: {:?}", e), + } + }); } }); }