pub struct FetchService {}
Expand description

A service to fetch resources.

Implementations

Creates a new service instance connected to App by provided sender.

Sends a request to a remote server given a Request object and a callback fuction to convert a Response object into a loop’s message.

You may use a Request builder to build your request declaratively as on the following examples:

   let post_request = Request::post("https://my.api/v1/resource")
           .header("Content-Type", "application/json")
           .body(Json(&json!({"foo": "bar"})))
           .expect("Failed to build request.");

   let get_request = Request::get("https://my.api/v1/resource")
           .body(Nothing)
           .expect("Failed to build request.");

The callback function can build a loop message by passing or analizing the response body and metadata.

    context.web.fetch(
        post_request,
        |response| {
            if response.status().is_success() {
                Msg::Noop
            } else {
                Msg::Error
            }
        }

One can also simply consume and pass the response or body object into the message.

    context.web.fetch(
        get_request,
        |response| {
            let (meta, Json(body)) = response.into_parts();
            if meta.status.is_success() {
                Msg::FetchResourceComplete(body)
            } else {
                Msg::FetchResourceFailed
            }
        }

Fetch the data in binary format.

Trait Implementations

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.