Skip to content

Commit

Permalink
Make fetch service methods static. (#1235)
Browse files Browse the repository at this point in the history
* Make fetch methods static.

* Update `FetchService.fetch_binary`

* Updated documentation.

* Update examples.

* Added explicit lifetimes.

* Reformat code.

* Update a reference from `fetch_service` to `FetchService`.

* Reformat yew-stdweb example.

* Update a doc comment.
  • Loading branch information
teymour-aldridge authored May 16, 2020
1 parent 244ac3b commit 5f7e03b
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 53 deletions.
8 changes: 4 additions & 4 deletions examples/dashboard/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ impl Model {
);
let request = Request::get("/data.json").body(Nothing).unwrap();
if binary {
FetchService::new().fetch_binary(request, callback).unwrap()
FetchService::fetch_binary(request, callback).unwrap()
} else {
FetchService::new().fetch(request, callback).unwrap()
FetchService::fetch(request, callback).unwrap()
}
}

Expand All @@ -110,9 +110,9 @@ impl Model {
);
let request = Request::get("/data.toml").body(Nothing).unwrap();
if binary {
FetchService::new().fetch_binary(request, callback).unwrap()
FetchService::fetch_binary(request, callback).unwrap()
} else {
FetchService::new().fetch(request, callback).unwrap()
FetchService::fetch(request, callback).unwrap()
}
}
}
Expand Down
10 changes: 3 additions & 7 deletions examples/npm_and_rest/src/gravatar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,11 @@ pub struct Entry {
}

#[derive(Default)]
pub struct GravatarService {
web: FetchService,
}
pub struct GravatarService {}

impl GravatarService {
pub fn new() -> Self {
Self {
web: FetchService::new(),
}
Self {}
}

pub fn profile(&mut self, hash: &str, callback: Callback<Result<Profile, Error>>) -> FetchTask {
Expand All @@ -45,6 +41,6 @@ impl GravatarService {
}
};
let request = Request::get(url.as_str()).body(Nothing).unwrap();
self.web.fetch(request, handler.into()).unwrap()
FetchService::fetch(request, handler.into()).unwrap()
}
}
10 changes: 3 additions & 7 deletions yew-stdweb/examples/npm_and_rest/src/gravatar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,11 @@ pub struct Entry {
}

#[derive(Default)]
pub struct GravatarService {
web: FetchService,
}
pub struct GravatarService {}

impl GravatarService {
pub fn new() -> Self {
Self {
web: FetchService::new(),
}
Self {}
}

pub fn profile(&mut self, hash: &str, callback: Callback<Result<Profile, Error>>) -> FetchTask {
Expand All @@ -45,6 +41,6 @@ impl GravatarService {
}
};
let request = Request::get(url.as_str()).body(Nothing).unwrap();
self.web.fetch(request, handler.into()).unwrap()
FetchService::fetch(request, handler.into()).unwrap()
}
}
26 changes: 13 additions & 13 deletions yew/src/services/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ mod tests {
let options = FetchOptions::default();
let cb_future = CallbackFuture::<Response<Json<Result<HttpBin, anyhow::Error>>>>::default();
let callback: Callback<_> = cb_future.clone().into();
let _task = FetchService::new().fetch_with_options(request, options, callback);
let _task = FetchService::fetch_with_options(request, options, callback);
let resp = cb_future.await;
assert_eq!(resp.status(), StatusCode::OK);
if let Json(Ok(http_bin)) = resp.body() {
Expand All @@ -78,7 +78,7 @@ mod tests {
};
let cb_future = CallbackFuture::<Response<Json<Result<HttpBin, anyhow::Error>>>>::default();
let callback: Callback<_> = cb_future.clone().into();
let _task = FetchService::new().fetch_with_options(request, options, callback);
let _task = FetchService::fetch_with_options(request, options, callback);
let resp = cb_future.await;
assert_eq!(resp.status(), StatusCode::OK);
if let Json(Ok(http_bin)) = resp.body() {
Expand All @@ -100,7 +100,7 @@ mod tests {
};
let cb_future = CallbackFuture::<Response<Json<Result<HttpBin, anyhow::Error>>>>::default();
let callback: Callback<_> = cb_future.clone().into();
let _task = FetchService::new().fetch_with_options(request, options, callback);
let _task = FetchService::fetch_with_options(request, options, callback);
let resp = cb_future.await;
assert_eq!(resp.status(), StatusCode::OK);
if let Json(Ok(http_bin)) = resp.body() {
Expand All @@ -121,7 +121,7 @@ mod tests {
};
let cb_future = CallbackFuture::<Response<Json<Result<HttpBin, anyhow::Error>>>>::default();
let callback: Callback<_> = cb_future.clone().into();
let _task = FetchService::new().fetch_with_options(request, options, callback);
let _task = FetchService::fetch_with_options(request, options, callback);
let resp = cb_future.await;
assert_eq!(resp.status(), StatusCode::OK);
if let Json(Ok(http_bin)) = resp.body() {
Expand All @@ -139,7 +139,7 @@ mod tests {
let options = FetchOptions::default();
let cb_future = CallbackFuture::<Response<Json<Result<HttpBin, anyhow::Error>>>>::default();
let callback: Callback<_> = cb_future.clone().into();
let _task = FetchService::new().fetch_with_options(request, options, callback);
let _task = FetchService::fetch_with_options(request, options, callback);
let resp = cb_future.await;
assert_eq!(resp.status(), StatusCode::OK);
if let Json(Ok(http_bin)) = resp.body() {
Expand All @@ -160,7 +160,7 @@ mod tests {
};
let cb_future = CallbackFuture::<Response<Json<Result<HttpBin, anyhow::Error>>>>::default();
let callback: Callback<_> = cb_future.clone().into();
let _task = FetchService::new().fetch_with_options(request, options, callback);
let _task = FetchService::fetch_with_options(request, options, callback);
let resp = cb_future.await;
assert_eq!(resp.status(), StatusCode::OK);
if let Json(Ok(http_bin)) = resp.body() {
Expand All @@ -181,7 +181,7 @@ mod tests {
};
let cb_future = CallbackFuture::<Response<Result<String, anyhow::Error>>>::default();
let callback: Callback<_> = cb_future.clone().into();
let _task = FetchService::new().fetch_with_options(request, options, callback);
let _task = FetchService::fetch_with_options(request, options, callback);
let resp = cb_future.await;
assert_eq!(resp.status(), StatusCode::REQUEST_TIMEOUT);
}
Expand All @@ -197,7 +197,7 @@ mod tests {
};
let cb_future = CallbackFuture::<Response<Result<String, anyhow::Error>>>::default();
let callback: Callback<_> = cb_future.clone().into();
let _task = FetchService::new().fetch_with_options(request, options, callback);
let _task = FetchService::fetch_with_options(request, options, callback);
let resp = cb_future.await;
assert_eq!(resp.status(), StatusCode::OK);
// body is empty because the response is opaque for manual redirects
Expand All @@ -219,7 +219,7 @@ mod tests {
};
let cb_future = CallbackFuture::<Response<Result<String, anyhow::Error>>>::default();
let callback: Callback<_> = cb_future.clone().into();
let _task = FetchService::new().fetch_with_options(request, options, callback);
let _task = FetchService::fetch_with_options(request, options, callback);
let resp = cb_future.await;
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(resp.body().as_ref().unwrap(), resource);
Expand All @@ -240,7 +240,7 @@ mod tests {
};
let cb_future = CallbackFuture::<Response<Result<String, anyhow::Error>>>::default();
let callback: Callback<_> = cb_future.clone().into();
let _task = FetchService::new().fetch_with_options(request, options, callback);
let _task = FetchService::fetch_with_options(request, options, callback);
let resp = cb_future.await;
assert!(resp.body().is_err());
}
Expand All @@ -250,7 +250,7 @@ mod tests {
let request = Request::get("https://fetch.fail").body(Nothing).unwrap();
let cb_future = CallbackFuture::<Response<Result<String, anyhow::Error>>>::default();
let callback: Callback<_> = cb_future.clone().into();
let _task = FetchService::new().fetch(request, callback);
let _task = FetchService::fetch(request, callback);
let resp = cb_future.await;
#[cfg(feature = "std_web")]
assert!(resp.body().is_err());
Expand All @@ -273,7 +273,7 @@ mod tests {
let cb_future =
CallbackFuture::<Response<Json<Result<HttpBinHeaders, anyhow::Error>>>>::default();
let callback: Callback<_> = cb_future.clone().into();
let _task = FetchService::new().fetch_with_options(request, options, callback);
let _task = FetchService::fetch_with_options(request, options, callback);
let resp = cb_future.await;
assert_eq!(resp.status(), StatusCode::OK);
if let Json(Ok(httpbin_headers)) = resp.body() {
Expand All @@ -295,7 +295,7 @@ mod tests {
let cb_future =
CallbackFuture::<Response<Json<Result<HttpBinHeaders, anyhow::Error>>>>::default();
let callback: Callback<_> = cb_future.clone().into();
let _task = FetchService::new().fetch_with_options(request, options, callback);
let _task = FetchService::fetch_with_options(request, options, callback);
let resp = cb_future.await;
assert_eq!(resp.status(), StatusCode::OK);
if let Json(Ok(httpbin_headers)) = resp.body() {
Expand Down
21 changes: 8 additions & 13 deletions yew/src/services/fetch/std_web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,8 @@ impl FetchService {
///# }
///# fn dont_execute() {
///# let link: ComponentLink<Comp> = unimplemented!();
///# let mut fetch_service: FetchService = FetchService::new();
///# let post_request: Request<Result<String, anyhow::Error>> = unimplemented!();
/// let task = fetch_service.fetch(
/// let task = FetchService::fetch(
/// post_request,
/// link.callback(|response: Response<Result<String, anyhow::Error>>| {
/// if response.status().is_success() {
Expand Down Expand Up @@ -262,15 +261,14 @@ impl FetchService {
/// Msg::FetchResourceFailed
/// });
///
/// let task = FetchService::new().fetch(get_request, callback);
/// let task = FetchService::fetch(get_request, callback);
///# }
/// ```
///
pub fn fetch<IN, OUT: 'static>(
&mut self,
request: Request<IN>,
callback: Callback<Response<OUT>>,
) -> Result<FetchTask, &str>
) -> Result<FetchTask, &'static str>
where
IN: Into<Text>,
OUT: From<Text>,
Expand Down Expand Up @@ -306,15 +304,14 @@ impl FetchService {
/// credentials: Some(Credentials::SameOrigin),
/// ..FetchOptions::default()
/// };
/// let task = FetchService::new().fetch_with_options(request, options, callback);
/// let task = FetchService::fetch_with_options(request, options, callback);
///# }
/// ```
pub fn fetch_with_options<IN, OUT: 'static>(
&mut self,
request: Request<IN>,
options: FetchOptions,
callback: Callback<Response<OUT>>,
) -> Result<FetchTask, &str>
) -> Result<FetchTask, &'static str>
where
IN: Into<Text>,
OUT: From<Text>,
Expand All @@ -324,24 +321,22 @@ impl FetchService {

/// Fetch the data in binary format.
pub fn fetch_binary<IN, OUT: 'static>(
&mut self,
request: Request<IN>,
callback: Callback<Response<OUT>>,
) -> Result<FetchTask, &str>
) -> Result<FetchTask, &'static str>
where
IN: Into<Binary>,
OUT: From<Binary>,
{
fetch_impl::<IN, OUT, Vec<u8>, ArrayBuffer>(true, request, None, callback)
}

/// Fetch the data in binary format.
/// Fetch the data in binary format using the provided request options.
pub fn fetch_binary_with_options<IN, OUT: 'static>(
&mut self,
request: Request<IN>,
options: FetchOptions,
callback: Callback<Response<OUT>>,
) -> Result<FetchTask, &str>
) -> Result<FetchTask, &'static str>
where
IN: Into<Binary>,
OUT: From<Binary>,
Expand Down
13 changes: 4 additions & 9 deletions yew/src/services/fetch/web_sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,8 @@ impl FetchService {
///# }
///# fn dont_execute() {
///# let link: ComponentLink<Comp> = unimplemented!();
///# let mut fetch_service: FetchService = FetchService::new();
///# let post_request: Request<Result<String, Error>> = unimplemented!();
/// let task = fetch_service.fetch(
/// let task = FetchService::fetch(
/// post_request,
/// link.callback(|response: Response<Result<String, Error>>| {
/// if response.status().is_success() {
Expand Down Expand Up @@ -266,12 +265,11 @@ impl FetchService {
/// Msg::FetchResourceFailed
/// });
///
/// let task = FetchService::new().fetch(get_request, callback);
/// let task = FetchService::fetch(get_request, callback);
///# }
/// ```
///
pub fn fetch<IN, OUT: 'static>(
&mut self,
request: Request<IN>,
callback: Callback<Response<OUT>>,
) -> Result<FetchTask, Error>
Expand Down Expand Up @@ -311,11 +309,10 @@ impl FetchService {
/// credentials: Some(Credentials::SameOrigin),
/// ..FetchOptions::default()
/// };
/// let task = FetchService::new().fetch_with_options(request, options, callback);
/// let task = FetchService::fetch_with_options(request, options, callback);
///# }
/// ```
pub fn fetch_with_options<IN, OUT: 'static>(
&mut self,
request: Request<IN>,
options: FetchOptions,
callback: Callback<Response<OUT>>,
Expand All @@ -329,7 +326,6 @@ impl FetchService {

/// Fetch the data in binary format.
pub fn fetch_binary<IN, OUT: 'static>(
&mut self,
request: Request<IN>,
callback: Callback<Response<OUT>>,
) -> Result<FetchTask, Error>
Expand All @@ -340,9 +336,8 @@ impl FetchService {
fetch_impl::<IN, OUT, Vec<u8>>(true, request, None, callback)
}

/// Fetch the data in binary format.
/// Fetch the data in binary format with the provided request options.
pub fn fetch_binary_with_options<IN, OUT: 'static>(
&mut self,
request: Request<IN>,
options: FetchOptions,
callback: Callback<Response<OUT>>,
Expand Down

0 comments on commit 5f7e03b

Please sign in to comment.