Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.4.0 #8

Merged
merged 9 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "reqwest-cross"
version = "0.3.1"
version = "0.4.0"
authors = ["One <[email protected]>"]
categories = ["web-programming::http-client", "wasm"]
documentation = "https://docs.rs/reqwest-cross"
Expand All @@ -14,6 +14,7 @@ description = "Wrapper around reqwest for use in both native and wasm"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
futures = "0.3.28"
reqwest = { version = "0.12.3", default-features = false }

# For native compilation
Expand All @@ -27,7 +28,6 @@ js-sys = { version = "0.3.69", optional = true }
web-sys = { version = "0.3.69", optional = true }

[dev-dependencies]
futures = "0.3.28"
reqwest = { version = "0.12.3" }
wasm-bindgen-test = "0.3.34"

Expand Down
2 changes: 1 addition & 1 deletion examples/loop_yield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async fn common_code() -> Result<(), Box<dyn std::error::Error>> {
let (tx, rx) = futures::channel::oneshot::channel();
fetch(
request,
move |result: Result<reqwest::Response, reqwest::Error>| {
move |result: Result<reqwest::Response, reqwest::Error>| async {
tx.send(result.expect("Expecting Response not Error").status())
.expect("Receiver should still be available");
},
Expand Down
2 changes: 1 addition & 1 deletion examples/simple_fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async fn common_code() -> Result<(), Box<dyn std::error::Error>> {

fetch(
request,
move |result: Result<reqwest::Response, reqwest::Error>| {
move |result: Result<reqwest::Response, reqwest::Error>| async {
tx.send(result.expect("Expecting Response not Error").status())
.expect("Receiver should still be available");
},
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![cfg_attr(docsrs, feature(doc_cfg))]
#![forbid(unsafe_code)]
#![cfg_attr(test, deny(warnings))]
// dox - used as documentation for duplicate wasm functions (Uncertain if this will cause problems but seen this in Reqwest)

//! # reqwest-cross
//!
Expand Down
10 changes: 4 additions & 6 deletions src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
compile_error!("Must chose a native runtime by enabling a feature flag. Right now only tokio is supported. If you have a different runtime that you want please create an issue on github.");

#[cfg(feature = "native-tokio")]
pub fn fetch<F>(request: reqwest::RequestBuilder, on_done: F)
pub fn spawn<F>(future: F)
where
F: 'static + Send + FnOnce(Result<reqwest::Response, reqwest::Error>),
F: 'static + Send + futures::Future,
F::Output: Send + 'static,
{
tokio::spawn(async move {
let result = request.send().await;
on_done(result)
});
tokio::spawn(future);
}
9 changes: 3 additions & 6 deletions src/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
//! Stores the code specific to wasm compilations

pub fn fetch<F>(request: reqwest::RequestBuilder, on_done: F)
pub fn spawn<F>(future: F)
where
F: 'static + Send + FnOnce(reqwest::Result<reqwest::Response>),
F: futures::Future<Output = ()> + 'static,
{
wasm_bindgen_futures::spawn_local(async move {
let result = request.send().await;
on_done(result)
});
wasm_bindgen_futures::spawn_local(future);
}
48 changes: 41 additions & 7 deletions src/wrappers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Stores the wrapper functions that can be called from either native or wasm
//! code

#[cfg(not(target_arch = "wasm32"))]
/// Performs a HTTP requests and calls the given callback when done. NB: Needs
/// to use a callback to prevent blocking on the thread that initiates the
/// fetch. Note: Instead of calling get like in the example you can use post,
Expand All @@ -19,7 +20,7 @@
/// let request = client.get("http://httpbin.org/get");
/// let (tx, rx) = futures::channel::oneshot::channel();
///
/// fetch(request, move |result: Result<reqwest::Response, reqwest::Error>| {
/// fetch(request, move |result: Result<reqwest::Response, reqwest::Error>| async {
/// tx.send(result.expect("Expecting Response not Error").status())
/// .expect("Receiver should still be available");
/// });
Expand All @@ -32,15 +33,48 @@
/// # #[cfg(target_arch = "wasm32")]
/// # fn main(){}
/// ```
pub fn fetch<F>(request: reqwest::RequestBuilder, on_done: F)
pub fn fetch<F, O>(request: reqwest::RequestBuilder, on_done: F)
where
F: 'static + Send + FnOnce(reqwest::Result<reqwest::Response>),
F: 'static + Send + FnOnce(reqwest::Result<reqwest::Response>) -> O,
O: futures::Future<Output = ()> + Send,
{
#[cfg(not(target_arch = "wasm32"))]
crate::native::fetch(request, on_done);
let future = async move {
let result = request.send().await;
on_done(result).await;
};
spawn(future);
}

/// dox
#[cfg(target_arch = "wasm32")]
pub fn fetch<F, O>(request: reqwest::RequestBuilder, on_done: F)
where
F: 'static + FnOnce(reqwest::Result<reqwest::Response>) -> O,
O: futures::Future<Output = ()>,
{
let future = async move {
let result = request.send().await;
on_done(result).await;
};
spawn(future);
}

#[cfg(target_arch = "wasm32")]
crate::wasm::fetch(request, on_done);
#[cfg(not(target_arch = "wasm32"))]
/// Spawns a future on the underlying runtime in a cross platform way
pub fn spawn<F>(future: F)
where
F: futures::Future<Output = ()> + 'static + Send,
{
crate::native::spawn(future);
}

#[cfg(target_arch = "wasm32")]
/// dox
pub fn spawn<F>(future: F)
where
F: futures::Future<Output = ()> + 'static,
{
crate::wasm::spawn(future);
}

// TODO 3: Test link in documentation after pushing to main
2 changes: 1 addition & 1 deletion tests/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn main() {

fetch(
request,
move |result: Result<reqwest::Response, reqwest::Error>| {
move |result: Result<reqwest::Response, reqwest::Error>| async {
tx.send(result.expect("Expecting Response not Error").status())
.expect("Receiver should still be available");
},
Expand Down