Struct yew::services::FetchService [−][src]
pub struct FetchService {}
Expand description
A service to fetch resources.
Implementations
impl FetchService
[src]
impl FetchService
[src]pub fn fetch<IN, OUT: 'static>(
request: Request<IN>,
callback: Callback<Response<OUT>>
) -> Result<FetchTask, Error> where
IN: Into<Text>,
OUT: From<Text>,
[src]
pub fn fetch<IN, OUT: 'static>(
request: Request<IN>,
callback: Callback<Response<OUT>>
) -> Result<FetchTask, Error> where
IN: Into<Text>,
OUT: From<Text>,
[src]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, 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 message isn’t JSON, or isn’t the specified data type, then you will get an error message.
#[derive(Deserialize)] struct Data { value: String } let get_request = Request::get("/thing").body(Nothing).unwrap(); let callback = link.callback(|response: Response<Json<Result<Data, 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);
pub fn fetch_with_options<IN, OUT: 'static>(
request: Request<IN>,
options: FetchOptions,
callback: Callback<Response<OUT>>
) -> Result<FetchTask, Error> where
IN: Into<Text>,
OUT: From<Text>,
[src]
pub fn fetch_with_options<IN, OUT: 'static>(
request: Request<IN>,
options: FetchOptions,
callback: Callback<Response<OUT>>
) -> Result<FetchTask, Error> where
IN: Into<Text>,
OUT: From<Text>,
[src]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);
Trait Implementations
impl Debug for FetchService
[src]
impl Debug for FetchService
[src]impl Default for FetchService
[src]
impl Default for FetchService
[src]fn default() -> FetchService
[src]
fn default() -> FetchService
[src]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
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]pub fn borrow_mut(&mut self) -> &mut T
[src]
pub fn borrow_mut(&mut self) -> &mut T
[src]Mutably borrows from an owned value. Read more
impl<T> Any for T where
T: Any,
[src]
T: Any,