yew_stdweb::services::fetch

Struct FetchService

Source
pub struct FetchService {}
Expand description

A service to fetch resources.

Implementations§

Source§

impl FetchService

Source

pub fn fetch<IN, OUT>( request: Request<IN>, callback: Callback<Response<OUT>>, ) -> Result<FetchTask, &'static str>
where IN: Into<Text>, OUT: From<Text> + 'static,

Sends a request to a remote server given a Request object and a callback function 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 analyzing the response body and metadata.

let task = FetchService::fetch(
    post_request,
    link.callback(|response: Response<Result<String, anyhow::Error>>| {
        if response.status().is_success() {
            Msg::Noop
        } else {
            Msg::Error
        }
    }),
);

For a full example, you can specify that the response must be in the JSON format, and be a specific serialized data type. If the mesage isn’t Json, or isn’t the specified data type, then you will get a message indicating failure.

#[derive(Deserialize)]
struct Data {
   value: String
}

let get_request = Request::get("/thing").body(Nothing).unwrap();
let callback = link.callback(|response: Response<Json<Result<Data, anyhow::Error>>>| {
    if let (meta, Json(Ok(body))) = response.into_parts() {
        if meta.status.is_success() {
            return Msg::FetchResourceComplete(body);
        }
    }
    Msg::FetchResourceFailed
});

let task = FetchService::fetch(get_request, callback);
Source

pub fn fetch_with_options<IN, OUT>( request: Request<IN>, options: FetchOptions, callback: Callback<Response<OUT>>, ) -> Result<FetchTask, &'static str>
where IN: Into<Text>, OUT: From<Text> + 'static,

fetch with provided FetchOptions object. Use it if you need to send cookies with a request:

let request = fetch::Request::get("/path/")
    .body(Nothing)
    .unwrap();
let options = FetchOptions {
    credentials: Some(Credentials::SameOrigin),
    ..FetchOptions::default()
};
let task = FetchService::fetch_with_options(request, options, callback);
Source

pub fn fetch_binary<IN, OUT>( request: Request<IN>, callback: Callback<Response<OUT>>, ) -> Result<FetchTask, &'static str>
where IN: Into<Binary>, OUT: From<Binary> + 'static,

Fetch the data in binary format.

Source

pub fn fetch_binary_with_options<IN, OUT>( request: Request<IN>, options: FetchOptions, callback: Callback<Response<OUT>>, ) -> Result<FetchTask, &'static str>
where IN: Into<Binary>, OUT: From<Binary> + 'static,

Fetch the data in binary format using the provided request options.

Trait Implementations§

Source§

impl Debug for FetchService

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for FetchService

Source§

fn default() -> FetchService

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

Auto Trait Implementations§

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, V> IntoOptPropValue<V> for T
where T: IntoPropValue<Option<V>>,

Source§

fn into_opt_prop_value(self) -> Option<V>

Convert self to an optional value of a Properties struct.
Source§

impl<T> IntoPropValue<Option<T>> for T

Source§

fn into_prop_value(self) -> Option<T>

Convert self to a value of a Properties struct.
Source§

impl<T> IntoPropValue<T> for T

Source§

fn into_prop_value(self) -> T

Convert self to a value of a Properties struct.
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, 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> Any for T
where T: Any,