Struct yew::services::fetch::FetchService
source · pub struct FetchService {}
Expand description
A service to fetch resources.
Implementations
sourceimpl FetchService
impl FetchService
sourcepub fn fetch<IN, OUT: 'static>(
&mut self,
request: Request<IN>,
callback: Callback<Response<OUT>>
) -> FetchTaskwhere
IN: Into<Text>,
OUT: From<Text>,
pub fn fetch<IN, OUT: 'static>(
&mut self,
request: Request<IN>,
callback: Callback<Response<OUT>>
) -> FetchTaskwhere
IN: Into<Text>,
OUT: From<Text>,
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
}
}
Trait Implementations
sourceimpl Default for FetchService
impl Default for FetchService
sourcefn default() -> FetchService
fn default() -> FetchService
Returns the “default value” for a type. Read more
Auto Trait Implementations
impl RefUnwindSafe for FetchService
impl Send for FetchService
impl Sync for FetchService
impl Unpin for FetchService
impl UnwindSafe for FetchService
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more