use alloc::{
string::{String, ToString},
vec::Vec,
};
use serde::Serialize;
#[derive(Debug, Clone, PartialEq)]
pub enum NetError {
Network(String),
Http(u16),
Other(String),
}
#[derive(Debug, Default, Clone, Copy, PartialEq)]
pub enum Method {
#[default]
Get,
Post,
Put,
Delete,
Head,
Options,
}
#[cfg(any(feature = "std", target_arch = "wasm32", feature = "wasm"))]
impl From<Method> for surf::http::Method {
fn from(m: Method) -> surf::http::Method {
match m {
Method::Get => surf::http::Method::Get,
Method::Post => surf::http::Method::Post,
Method::Put => surf::http::Method::Put,
Method::Delete => surf::http::Method::Delete,
Method::Head => surf::http::Method::Head,
Method::Options => surf::http::Method::Options,
}
}
}
#[cfg(feature = "std")]
pub async fn fetch_url<T: Serialize>(
url: &str,
headers: &[(&str, &str)],
method: Option<Method>,
body: Option<&T>,
) -> Result<Vec<u8>, NetError> {
let method = method.unwrap_or_default();
let client = surf::client();
let url = url.parse::<surf::Url>().map_err(|e| NetError::Other(e.to_string()))?;
let mut req = surf::Request::new(method.into(), url);
for (k, v) in headers {
req.set_header(*k, *v);
}
if let Some(b) = body {
let json_str = serde_json::to_string(b).map_err(|e| NetError::Other(e.to_string()))?;
req.set_body(surf::Body::from(json_str));
req.set_content_type("application/json".into());
}
let mut res = client.send(req).await.map_err(|e| NetError::Network(e.to_string()))?;
if !res.status().is_success() {
return Err(NetError::Http(res.status().into()));
}
res.body_bytes().await.map_err(|e| NetError::Other(e.to_string()))
}
#[cfg(any(target_arch = "wasm32", feature = "wasm"))]
pub async fn fetch_url<T: Serialize>(
url: &str,
headers: &[(&str, &str)],
method: Option<Method>,
body: Option<&T>,
) -> Result<Vec<u8>, NetError> {
let method = method.unwrap_or_default();
let client = surf::client();
let url = url.parse::<surf::Url>().map_err(|e| NetError::Other(e.to_string()))?;
let mut req = surf::Request::new(method.into(), url);
for (k, v) in headers {
req.set_header(*k, *v);
}
if let Some(b) = body {
let json_str = serde_json::to_string(b).map_err(|e| NetError::Other(e.to_string()))?;
req.set_body(surf::Body::from(json_str));
req.set_content_type("application/json".into());
}
let mut res = client.send(req).await.map_err(|e| NetError::Network(e.to_string()))?;
if !res.status().is_success() {
return Err(NetError::Http(res.status().into()));
}
res.body_bytes().await.map_err(|e| NetError::Other(e.to_string()))
}