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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//! Stores the wrapper functions that can be called from either native or wasm
//! code (Most of the platform specific code was moved to the main-loop-async
//! crate)
use spawn;
use error;
// Using * imports to bring them up to this level
use crate::;
/// Wraps the call to [fetch] with the surrounding boilerplate.
///
/// # Example
/// ```rust,ignore-wasm32
/// # 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(){}
/// ```
/// Performs a HTTP requests and calls the given callback when done with the
/// result of the request. This is a more flexible API but requires more
/// boilerplate, see [fetch_plus][crate::fetch_plus] which wraps a lot more of
/// the boilerplate especially if you need a "wake_up" function. 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, put, etc.
/// (See [reqwest::Client]). Also see the examples
/// [folder](https://github.com/c-git/reqwest-cross/tree/main/examples)
/// for more complete examples.
///
/// # Example
/// ```rust
/// # use reqwest_cross::fetch;
///
/// # #[cfg(all(not(target_arch = "wasm32"),feature = "native-tokio"))]
/// # #[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 (tx, rx) = futures::channel::oneshot::channel();
///
/// 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");
/// });
///
/// 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(){}
/// ```