#http-client #http-request #bevy #async-http #run-time #web #focus

bevy_mod_reqwest

Bevy http client using reqwest, with a focus on simple usage within the bevy runtime

21 releases (8 breaking)

new 0.18.0 Nov 30, 2024
0.16.0 Aug 5, 2024
0.15.0 Jul 4, 2024
0.14.0 Feb 20, 2024
0.11.0 Jul 12, 2023

#175 in Game dev

Download history 183/week @ 2024-08-16 135/week @ 2024-08-23 162/week @ 2024-08-30 172/week @ 2024-09-06 170/week @ 2024-09-13 176/week @ 2024-09-20 155/week @ 2024-09-27 183/week @ 2024-10-04 51/week @ 2024-10-11 114/week @ 2024-10-18 134/week @ 2024-10-25 255/week @ 2024-11-01 317/week @ 2024-11-08 173/week @ 2024-11-15 184/week @ 2024-11-22 354/week @ 2024-11-29

1,073 downloads per month

MIT license

24KB
327 lines

bevy_mod_reqwest

crates.io docs.rs

This crate helps when trying to use reqwest with bevy, without having to deal with async stuff, and it works on both web and and native ( only tested on x86_64 and wasm for now)

Bevy version bevy_mod_reqwest version
0.15 0.18
0.14 0.15 - 0.17
0.13 0.14
0.12 0.12 - 0.13

Example

use std::time::Duration;

use bevy::{log::LogPlugin, prelude::*, time::common_conditions::on_timer};
use bevy_mod_reqwest::*;

#[derive(Default, Resource)]
// just a vector that stores all the responses as strings to showcase that the `on_response` methods
// are just regular observersystems, that function very much like regular systems
struct History {
    pub responses: Vec<String>,
}

fn send_requests(mut client: BevyReqwest) {
    let url = "https://bored-api.appbrewery.com/random";

    // use regular reqwest http calls, then poll them to completion.
    let reqwest_request = client.get(url).build().unwrap();

    client
        // Sends the created http request
        .send(reqwest_request)
        // The response from the http request can be reached using an observersystem,
        // where the only requirement is that the first parameter in the system is the specific Trigger type
        // the rest is the same as a regular system
        .on_response(
            |trigger: Trigger<ReqwestResponseEvent>, mut history: ResMut<History>| {
                let response = trigger.event();
                let data = response.as_str();
                let status = response.status();
                // let headers = req.response_headers();
                bevy::log::info!("code: {status}, data: {data:?}");
                if let Ok(data) = data {
                    history.responses.push(format!("OK: {data}"));
                }
            },
        )
        // In case of request error, it can be reached using an observersystem as well
        .on_error(
            |trigger: Trigger<ReqwestErrorEvent>, mut history: ResMut<History>| {
                let e = &trigger.event().0;
                bevy::log::info!("error: {e:?}");
                history.responses.push(format!("ERROR: {e:?}"));
            },
        );
}

fn main() {
    App::new()
        .add_plugins(MinimalPlugins)
        .add_plugins(LogPlugin::default())
        .add_plugins(ReqwestPlugin::default())
        .init_resource::<History>()
        .add_systems(
            Update,
            send_requests.run_if(on_timer(Duration::from_secs(5))),
        )
        .run();
}

Dependencies

~26–41MB
~681K SLoC