BevyReqwest

Struct BevyReqwest 

Source
pub struct BevyReqwest<'w, 's> { /* private fields */ }
Expand description

Systemparam to have a shorthand for creating http calls in systems

Implementations§

Source§

impl<'w, 's> BevyReqwest<'w, 's>

Source

pub fn send(&mut self, req: Request) -> BevyReqwestBuilder<'_>

Starts sending and processing the supplied reqwest::Request then use the BevyReqwestBuilder to add handlers for responses and errors

Examples found in repository?
examples/post.rs (line 24)
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
Hide additional examples
examples/json.rs (line 22)
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}
examples/minimal.rs (line 21)
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}
Source

pub fn send_using_entity( &mut self, entity: Entity, req: Request, ) -> Result<BevyReqwestBuilder<'_>, Box<dyn Error>>

Starts sending and processing the supplied reqwest::Request on the supplied Entity if it exists and then use the BevyReqwestBuilder to add handlers for responses and errors

Source

pub fn client(&self) -> &Client

get access to the underlying ReqwestClient

Methods from Deref<Target = Client>§

Source

pub fn get<U>(&self, url: U) -> RequestBuilder
where U: IntoUrl,

Convenience method to make a GET request to a URL.

§Errors

This method fails whenever the supplied Url cannot be parsed.

Examples found in repository?
examples/json.rs (line 18)
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
Hide additional examples
examples/minimal.rs (line 17)
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}
Source

pub fn post<U>(&self, url: U) -> RequestBuilder
where U: IntoUrl,

Convenience method to make a POST request to a URL.

§Errors

This method fails whenever the supplied Url cannot be parsed.

Examples found in repository?
examples/post.rs (line 22)
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}
Source

pub fn put<U>(&self, url: U) -> RequestBuilder
where U: IntoUrl,

Convenience method to make a PUT request to a URL.

§Errors

This method fails whenever the supplied Url cannot be parsed.

Source

pub fn patch<U>(&self, url: U) -> RequestBuilder
where U: IntoUrl,

Convenience method to make a PATCH request to a URL.

§Errors

This method fails whenever the supplied Url cannot be parsed.

Source

pub fn delete<U>(&self, url: U) -> RequestBuilder
where U: IntoUrl,

Convenience method to make a DELETE request to a URL.

§Errors

This method fails whenever the supplied Url cannot be parsed.

Source

pub fn head<U>(&self, url: U) -> RequestBuilder
where U: IntoUrl,

Convenience method to make a HEAD request to a URL.

§Errors

This method fails whenever the supplied Url cannot be parsed.

Source

pub fn request<U>(&self, method: Method, url: U) -> RequestBuilder
where U: IntoUrl,

Start building a Request with the Method and Url.

Returns a RequestBuilder, which will allow setting headers and the request body before sending.

§Errors

This method fails whenever the supplied Url cannot be parsed.

Source

pub fn execute( &self, request: Request, ) -> impl Future<Output = Result<Response, Error>>

Executes a Request.

A Request can be built manually with Request::new() or obtained from a RequestBuilder with RequestBuilder::build().

You should prefer to use the RequestBuilder and RequestBuilder::send().

§Errors

This method fails if there was an error while sending request, redirect loop was detected or redirect limit was exhausted.

Trait Implementations§

Source§

impl<'w, 's> Deref for BevyReqwest<'w, 's>

Source§

type Target = Client

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl SystemParam for BevyReqwest<'_, '_>

Source§

type State = FetchState

Used to store data which persists across invocations of a system.
Source§

type Item<'w, 's> = BevyReqwest<'w, 's>

The item type returned when constructing this system param. The value of this associated type should be Self, instantiated with new lifetimes. Read more
Source§

fn init_state(world: &mut World) -> Self::State

Creates a new instance of this param’s State.
Source§

fn init_access( state: &Self::State, system_meta: &mut SystemMeta, component_access_set: &mut FilteredAccessSet, world: &mut World, )

Registers any World access used by this SystemParam
Source§

fn apply(state: &mut Self::State, system_meta: &SystemMeta, world: &mut World)

Applies any deferred mutations stored in this SystemParam’s state. This is used to apply Commands during ApplyDeferred.
Source§

fn queue( state: &mut Self::State, system_meta: &SystemMeta, world: DeferredWorld<'_>, )

Queues any deferred mutations to be applied at the next ApplyDeferred.
Source§

unsafe fn validate_param<'w, 's>( state: &'s mut Self::State, _system_meta: &SystemMeta, _world: UnsafeWorldCell<'w>, ) -> Result<(), SystemParamValidationError>

Validates that the param can be acquired by the get_param. Read more
Source§

unsafe fn get_param<'w, 's>( state: &'s mut Self::State, system_meta: &SystemMeta, world: UnsafeWorldCell<'w>, change_tick: Tick, ) -> Self::Item<'w, 's>

Creates a parameter to be passed into a SystemParamFunction. Read more
Source§

impl<'w, 's> ReadOnlySystemParam for BevyReqwest<'w, 's>

Auto Trait Implementations§

§

impl<'w, 's> Freeze for BevyReqwest<'w, 's>

§

impl<'w, 's> !RefUnwindSafe for BevyReqwest<'w, 's>

§

impl<'w, 's> Send for BevyReqwest<'w, 's>

§

impl<'w, 's> Sync for BevyReqwest<'w, 's>

§

impl<'w, 's> Unpin for BevyReqwest<'w, 's>

§

impl<'w, 's> !UnwindSafe for BevyReqwest<'w, 's>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CompatExt for T

Source§

fn compat(self) -> Compat<T>

Applies the Compat adapter by value. Read more
Source§

fn compat_ref(&self) -> Compat<&T>

Applies the Compat adapter by shared reference. Read more
Source§

fn compat_mut(&mut self) -> Compat<&mut T>

Applies the Compat adapter by mutable reference. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

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>

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)

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)

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
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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 more
Source§

impl<T> IntoResult<T> for T

Source§

fn into_result(self) -> Result<T, RunSystemError>

Converts this type into the system output type.
Source§

impl<A> Is for A
where A: Any,

Source§

fn is<T>() -> bool
where T: Any,

Checks if the current type “is” another type, using a TypeId equality comparison. This is most useful in the context of generic logic. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ConditionalSend for T
where T: Send,

Source§

impl<T> ErasedDestructor for T
where T: 'static,