1use alloc::{
2 string::{String, ToString},
3 vec::Vec,
4};
5
6#[derive(Debug, Clone, PartialEq)]
8pub enum NetError {
9 Network(String),
11 Http(u16),
13 Other(String),
15}
16
17#[cfg(feature = "std")]
20pub async fn fetch_url(url: &str, headers: &[(&str, &str)]) -> Result<Vec<u8>, NetError> {
21 let client = surf::client();
22
23 let mut req = surf::get(url);
24 for (k, v) in headers {
25 req = req.header(*k, *v);
26 }
27
28 let mut res = client.send(req).await.map_err(|e| NetError::Network(e.to_string()))?;
29
30 if !res.status().is_success() {
31 return Err(NetError::Http(res.status().into()));
32 }
33
34 res.body_bytes().await.map_err(|e| NetError::Other(e.to_string()))
35}
36
37#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
39pub async fn fetch_url(url: &str, headers: &[(&str, &str)]) -> Result<Vec<u8>, NetError> {
40 let mut req = surf::get(url);
41 for (k, v) in headers {
42 req = req.set_header(*k, *v);
43 }
44
45 let mut res = req.await.map_err(|e| NetError::Network(e.to_string()))?;
46
47 if !res.status().is_success() {
48 return Err(NetError::Http(res.status().into()));
49 }
50
51 res.body_bytes().await.map_err(|e| NetError::Other(e.to_string()))
52}