diff options
| author | LLLL Colonq <llll@colonq> | 2024-10-15 14:15:28 -0400 |
|---|---|---|
| committer | LLLL Colonq <llll@colonq> | 2024-10-15 14:15:28 -0400 |
| commit | aa465e6ebee75e9e9ea03196db4dd83fde766f0a (patch) | |
| tree | 62393a050e54489a3c0d3d474a3ffbd000f59684 | |
| parent | 92c18ba3b9d6252094cbe04ae750713327526ec3 (diff) | |
Fix request
| -rw-r--r-- | Cargo.lock | 1 | ||||
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | src/lib.rs | 6 | ||||
| -rw-r--r-- | src/state.rs | 26 |
4 files changed, 26 insertions, 8 deletions
@@ -1759,6 +1759,7 @@ name = "teleia" version = "0.1.0" dependencies = [ "bimap", + "bytes", "console_error_panic_hook", "console_log", "enum-map", @@ -34,6 +34,7 @@ js-sys = "*" # browser APIs to interact with JS runtime (e.g. run WASM) enum-map = "*" # fast maps with enums as keys bimap = "*" # bijective maps reqwest = "*" # http requests +bytes = "*" # bytes for http responses [dependencies.web-sys] # common browser APIs version = "*" @@ -125,7 +125,11 @@ where match std::future::Future::poll(f.as_mut(), &mut st.waker_ctx) { std::task::Poll::Pending => {}, std::task::Poll::Ready(res) => { - st.request_returned(&ctx, game, res); + st.request = None; + match res { + Ok(r) => st.request_returned(&ctx, game, r), + Err(e) => log::warn!("error during HTTP request: {}", e), + } }, } // f.poll(); diff --git a/src/state.rs b/src/state.rs index 89d1a13..8c70c51 100644 --- a/src/state.rs +++ b/src/state.rs @@ -8,7 +8,6 @@ use crate::{context, framebuffer, shader, audio}; const DELTA_TIME: f64 = 1.0 / 60.0; pub struct WinitWaker {} - impl WinitWaker { fn new() -> Self { Self {} } } @@ -16,6 +15,12 @@ impl std::task::Wake for WinitWaker { fn wake(self: std::sync::Arc<Self>) {} } +pub struct Response { + pub url: String, + pub status: reqwest::StatusCode, + pub body: bytes::Bytes, +} + pub trait Game { fn initialize_audio(&self, ctx: &context::Context, st: &State, actx: &audio::Context) -> HashMap<String, audio::Audio> @@ -25,7 +30,7 @@ pub trait Game { fn finish_title(&mut self, st: &mut State) {} fn mouse_move(&mut self, ctx: &context::Context, st: &mut State, x: i32, y: i32) {} fn mouse_press(&mut self, ctx: &context::Context, st: &mut State) {} - fn request_return(&mut self, ctx: &context::Context, st: &mut State, res: reqwest::Response) {} + fn request_return(&mut self, ctx: &context::Context, st: &mut State, res: Response) {} fn update(&mut self, ctx: &context::Context, st: &mut State) -> Option<()> { Some(()) } fn render(&mut self, ctx: &context::Context, st: &mut State) -> Option<()> { Some(()) } } @@ -109,7 +114,7 @@ pub struct State { pub waker_ctx: std::task::Context<'static>, pub http_client: reqwest::Client, - pub request: Option<std::pin::Pin<Box<dyn std::future::Future<Output = reqwest::Response>>>>, + pub request: Option<std::pin::Pin<Box<dyn std::future::Future<Output = reqwest::Result<Response>>>>>, pub log: Vec<(u64, String)>, } @@ -388,23 +393,30 @@ impl State { self.rebinding = Some(*k); } - pub fn request<F>(&mut self, ctx: &context::Context, f: F) + pub fn request<F>(&mut self, f: F) where F: Fn(&reqwest::Client) -> reqwest::RequestBuilder { let builder = f(&self.http_client); let fut = async { - builder.send().await.expect("failed to send HTTP request") + let resp = builder.send().await?; + let url = resp.url().clone().to_string(); + let status = resp.status().clone(); + let body = resp.bytes().await?; + reqwest::Result::Ok(Response { + url, + status, + body, + }) }; self.request = Some(Box::pin(fut)); } pub fn requesting(&self) -> bool { self.request.is_some() } - pub fn request_returned<G>(&mut self, ctx: &context::Context, game: &mut G, res: reqwest::Response) + pub fn request_returned<G>(&mut self, ctx: &context::Context, game: &mut G, res: Response) where G: Game { game.request_return(ctx, self, res); - self.request = None; } pub fn run_update<G>(&mut self, ctx: &context::Context, game: &mut G) where G: Game { |
