#bevy #http #plugin

bevy_http_client

A simple HTTP client for Bevy

2 unstable releases

0.2.0 Nov 6, 2023
0.1.0 Aug 28, 2023

#401 in HTTP client

34 downloads per month

MIT/Apache

22KB
104 lines

bevy_http_client

Crates.io crates.io Documentation MIT/Apache 2.0

A simple HTTP client Bevy Plugin for both native and WASM.

Example

use std::time::Duration;

use bevy::{app::ScheduleRunnerPlugin, prelude::*};
use bevy_http_client::*;

fn main() {
    App::new()
        .add_plugins(
            MinimalPlugins.set(ScheduleRunnerPlugin::run_loop(Duration::from_secs_f64(
                1.0 / 60.0,
            ))),
        )
        .add_plugins(HttpClientPlugin)
        .init_resource::<ApiTimer>()
        .add_systems(Update, (send_request, handle_response))
        .run()
}

#[derive(Resource, Deref, DerefMut)]
pub struct ApiTimer(pub Timer);

impl Default for ApiTimer {
    fn default() -> Self {
        Self(Timer::from_seconds(1.0, TimerMode::Repeating))
    }
}

fn send_request(mut commands: Commands, time: Res<Time>, mut timer: ResMut<ApiTimer>) {
    timer.tick(time.delta());

    if timer.just_finished() {
        let req = ehttp::Request::get("https://api.ipify.org");
        commands.spawn(HttpRequest(req));
    }
}

fn handle_response(
    mut commands: Commands,
    mut responses: Query<(Entity, &HttpResponse), Without<HttpResponseError>>,
) {
    for (entity, response) in responses.iter() {
        println!("response: {:?}", response.text());
        commands.entity(entity).despawn_recursive();
    }
}

Dependencies

~24–65MB
~1M SLoC