reqwest_cross/
platform.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//! Stores the wrapper functions that can be called from either native or wasm
//! code

use tracing::error;
#[cfg(not(target_arch = "wasm32"))]
mod native;
#[cfg(target_arch = "wasm32")]
mod wasm;

// Using * imports to bring them up to this level
use crate::{BoundedFuture, ResponseHandler, UiCallBack, ValidReturn};
#[cfg(not(target_arch = "wasm32"))]
pub use native::*;
#[cfg(target_arch = "wasm32")]
pub use wasm::*;

/// Wraps the call to [fetch] with the surrounding boilerplate.
///
/// # Example
/// ```rust
/// # use reqwest_cross::fetch_plus;
/// #
/// # #[tokio::main(flavor = "current_thread")]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let client = reqwest::Client::new();
///     let request = client.get("https://httpbin.org/get");
///     let handler = |result: Result<reqwest::Response, reqwest::Error>| async {
///         result.expect("Expecting Response not Error").status()
///     };
///     let rx = fetch_plus(request, handler, || {});
///     let status = rx.await?; //In actual use case code to prevent blocking use try_recv instead
///     assert_eq!(status, 200);
/// #    Ok(())
/// # }
///
/// # #[cfg(target_arch = "wasm32")]
/// # fn main(){}
/// ```
#[must_use = "receiver is needed to get the response"]
pub fn fetch_plus<FResponseHandler, FNotify, Fut, Ret>(
    req: reqwest::RequestBuilder,
    response_handler: FResponseHandler,
    ui_notify: FNotify,
) -> crate::oneshot::Receiver<Ret>
where
    FResponseHandler: ResponseHandler<Fut, Ret>,
    Fut: BoundedFuture<Ret>,
    Ret: ValidReturn,
    FNotify: UiCallBack,
{
    let (tx, rx) = crate::oneshot::channel();
    let on_done = move |resp: reqwest::Result<reqwest::Response>| async {
        let output = response_handler(resp).await;
        match tx.send(output) {
            Ok(()) => {}
            Err(_output) => error!("failed to send output from handler"),
        };
        ui_notify();
    };
    fetch(req, on_done);
    rx
}