pub struct BevyReqwestBuilder<'a>(/* private fields */);Expand description
Wrapper around EntityCommands to create the on_response and on_error
Implementations§
Source§impl<'a> BevyReqwestBuilder<'a>
impl<'a> BevyReqwestBuilder<'a>
Sourcepub fn on_response<RB: Bundle, RM, OR: IntoObserverSystem<ReqwestResponseEvent, RB, RM>>(
self,
onresponse: OR,
) -> Self
pub fn on_response<RB: Bundle, RM, OR: IntoObserverSystem<ReqwestResponseEvent, RB, RM>>( self, onresponse: OR, ) -> Self
Provide a system where the first argument is Trigger ReqwestResponseEvent that will run on the
response from the http request
§Examples
use bevy::prelude::Trigger;
use bevy_mod_reqwest::ReqwestResponseEvent;
|trigger: Trigger<ReqwestResponseEvent>| {
bevy::log::info!("response: {:?}", trigger.event());
};Examples found in repository?
examples/post.rs (lines 25-29)
15fn send_requests(mut client: BevyReqwest) {
16 let url = "https://jsonplaceholder.typicode.com/posts";
17 let body = Post {
18 title: "hello".into(),
19 body: "world".into(),
20 user_id: 1,
21 };
22 let req = client.post(url).json(&body).build().unwrap();
23 client
24 .send(req)
25 .on_response(|req: On<ReqwestResponseEvent>| {
26 let req = req.event();
27 let res = req.as_str();
28 bevy::log::info!("return data: {res:?}");
29 });
30}More examples
examples/minimal.rs (lines 25-36)
13fn send_requests(mut client: BevyReqwest) {
14 let url = "https://bored-api.appbrewery.com/random";
15
16 // use regular reqwest http calls, then poll them to completion.
17 let reqwest_request = client.get(url).build().unwrap();
18
19 client
20 // Sends the created http request
21 .send(reqwest_request)
22 // The response from the http request can be reached using an observersystem,
23 // where the only requirement is that the first parameter in the system is the specific Trigger type
24 // the rest is the same as a regular system
25 .on_response(
26 |trigger: On<ReqwestResponseEvent>, mut history: ResMut<History>| {
27 let response = trigger.event();
28 let data = response.as_str();
29 let status = response.status();
30 // let headers = req.response_headers();
31 bevy::log::info!("code: {status}, data: {data:?}");
32 if let Ok(data) = data {
33 history.responses.push(format!("OK: {data}"));
34 }
35 },
36 )
37 // In case of request error, it can be reached using an observersystem as well
38 .on_error(
39 |trigger: On<ReqwestErrorEvent>, mut history: ResMut<History>| {
40 let e = &trigger.event().entity;
41 bevy::log::info!("error: {e:?}");
42 history.responses.push(format!("ERROR: {e:?}"));
43 },
44 );
45}Sourcepub fn on_json_response<T: Sync + Send + DeserializeOwned + 'static, RB: Bundle, RM, OR: IntoObserverSystem<JsonResponse<T>, RB, RM>>(
self,
onresponse: OR,
) -> Self
pub fn on_json_response<T: Sync + Send + DeserializeOwned + 'static, RB: Bundle, RM, OR: IntoObserverSystem<JsonResponse<T>, RB, RM>>( self, onresponse: OR, ) -> Self
Provide a system where the first argument is Trigger JsonResponse that will run on the
response from the http request, skipping some boilerplate of having to manually doing the JSON
parsing
§Examples
use bevy::prelude::Trigger;
use bevy_mod_reqwest::JsonResponse;
|trigger: Trigger<JsonResponse<T>>| {
bevy::log::info!("response: {:?}", trigger.event());
};Examples found in repository?
examples/json.rs (lines 24-28)
14fn send_requests(mut client: BevyReqwest) {
15 let url = "https://bored-api.appbrewery.com/random";
16
17 // use regular reqwest http calls, then poll them to completion.
18 let reqwest_request = client.get(url).build().unwrap();
19
20 client
21 // Sends the created http request
22 .send(reqwest_request)
23 // The response from the http request can be reached using an observersystem
24 .on_json_response(|trigger: On<JsonResponse<Bored>>| {
25 let data: &Bored = &trigger.event().data;
26 // let headers = req.response_headers();
27 bevy::log::info!("data: {data:?}");
28 })
29 // In case of request error, it can be reached using an observersystem
30 .on_error(|trigger: On<ReqwestErrorEvent>| {
31 let e = &trigger.event().error;
32 bevy::log::info!("error: {e:?}");
33 });
34}Sourcepub fn on_error<EB: Bundle, EM, OE: IntoObserverSystem<ReqwestErrorEvent, EB, EM>>(
self,
onerror: OE,
) -> Self
pub fn on_error<EB: Bundle, EM, OE: IntoObserverSystem<ReqwestErrorEvent, EB, EM>>( self, onerror: OE, ) -> Self
Provide a system where the first argument is Trigger ReqwestErrorEvent that will run on the
response from the http request
§Examples
use bevy::prelude::Trigger;
use bevy_mod_reqwest::ReqwestErrorEvent;
|trigger: Trigger<ReqwestErrorEvent>| {
bevy::log::info!("response: {:?}", trigger.event());
};Examples found in repository?
examples/json.rs (lines 30-33)
14fn send_requests(mut client: BevyReqwest) {
15 let url = "https://bored-api.appbrewery.com/random";
16
17 // use regular reqwest http calls, then poll them to completion.
18 let reqwest_request = client.get(url).build().unwrap();
19
20 client
21 // Sends the created http request
22 .send(reqwest_request)
23 // The response from the http request can be reached using an observersystem
24 .on_json_response(|trigger: On<JsonResponse<Bored>>| {
25 let data: &Bored = &trigger.event().data;
26 // let headers = req.response_headers();
27 bevy::log::info!("data: {data:?}");
28 })
29 // In case of request error, it can be reached using an observersystem
30 .on_error(|trigger: On<ReqwestErrorEvent>| {
31 let e = &trigger.event().error;
32 bevy::log::info!("error: {e:?}");
33 });
34}More examples
examples/minimal.rs (lines 38-44)
13fn send_requests(mut client: BevyReqwest) {
14 let url = "https://bored-api.appbrewery.com/random";
15
16 // use regular reqwest http calls, then poll them to completion.
17 let reqwest_request = client.get(url).build().unwrap();
18
19 client
20 // Sends the created http request
21 .send(reqwest_request)
22 // The response from the http request can be reached using an observersystem,
23 // where the only requirement is that the first parameter in the system is the specific Trigger type
24 // the rest is the same as a regular system
25 .on_response(
26 |trigger: On<ReqwestResponseEvent>, mut history: ResMut<History>| {
27 let response = trigger.event();
28 let data = response.as_str();
29 let status = response.status();
30 // let headers = req.response_headers();
31 bevy::log::info!("code: {status}, data: {data:?}");
32 if let Ok(data) = data {
33 history.responses.push(format!("OK: {data}"));
34 }
35 },
36 )
37 // In case of request error, it can be reached using an observersystem as well
38 .on_error(
39 |trigger: On<ReqwestErrorEvent>, mut history: ResMut<History>| {
40 let e = &trigger.event().entity;
41 bevy::log::info!("error: {e:?}");
42 history.responses.push(format!("ERROR: {e:?}"));
43 },
44 );
45}Auto Trait Implementations§
impl<'a> Freeze for BevyReqwestBuilder<'a>
impl<'a> RefUnwindSafe for BevyReqwestBuilder<'a>
impl<'a> Send for BevyReqwestBuilder<'a>
impl<'a> Sync for BevyReqwestBuilder<'a>
impl<'a> Unpin for BevyReqwestBuilder<'a>
impl<'a> !UnwindSafe for BevyReqwestBuilder<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Converts
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Converts
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Converts
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Converts
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoResult<T> for T
impl<T> IntoResult<T> for T
Source§fn into_result(self) -> Result<T, RunSystemError>
fn into_result(self) -> Result<T, RunSystemError>
Converts this type into the system output type.