2022-03-25 18:18:55 +00:00
|
|
|
use std::error::Error;
|
|
|
|
use std::fmt;
|
|
|
|
use std::fmt::{Debug, Display, Formatter};
|
|
|
|
use wasm_bindgen::{JsCast, JsValue};
|
|
|
|
use wasm_bindgen_futures::JsFuture;
|
|
|
|
use web_sys::{Request, RequestInit, RequestMode, Response};
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub struct FetchError {
|
|
|
|
pub err: JsValue,
|
|
|
|
}
|
|
|
|
impl Display for FetchError {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
Debug::fmt(&self.err, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Error for FetchError {}
|
|
|
|
|
|
|
|
impl From<JsValue> for FetchError {
|
|
|
|
fn from(value: JsValue) -> Self {
|
|
|
|
Self { err: value }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<serde_json::Error> for FetchError {
|
|
|
|
fn from(e: serde_json::Error) -> Self {
|
|
|
|
Self {
|
|
|
|
err: JsValue::from(e.to_string()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn send_request(request: Request) -> Result<Response, FetchError> {
|
|
|
|
let window = web_sys::window().unwrap();
|
|
|
|
let resp_value = JsFuture::from(window.fetch_with_request(&request)).await?;
|
|
|
|
let resp: Response = resp_value.dyn_into()?;
|
|
|
|
|
|
|
|
Ok(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_json(url: &str) -> Result<Option<String>, FetchError> {
|
|
|
|
let mut opts = RequestInit::new();
|
|
|
|
opts.method("GET");
|
|
|
|
opts.mode(RequestMode::Cors);
|
|
|
|
|
|
|
|
let request = Request::new_with_str_and_init(url, &opts)?;
|
|
|
|
|
|
|
|
request.headers().set("Accept", "application/json")?;
|
|
|
|
|
|
|
|
let resp = self::send_request(request).await?;
|
|
|
|
let text = JsFuture::from(resp.text()?).await?;
|
|
|
|
|
|
|
|
Ok(text.as_string())
|
|
|
|
}
|
|
|
|
|
2022-03-30 07:36:59 +00:00
|
|
|
pub async fn put(url: &str, data: JsValue, if_match: Option<String>) -> Result<(), FetchError> {
|
2022-03-25 18:18:55 +00:00
|
|
|
let mut opts = RequestInit::new();
|
|
|
|
opts.method("PUT");
|
|
|
|
opts.mode(RequestMode::Cors);
|
|
|
|
opts.body(Some(&data));
|
|
|
|
|
|
|
|
let request = Request::new_with_str_and_init(url, &opts)?;
|
2022-03-26 06:57:57 +00:00
|
|
|
request
|
|
|
|
.headers()
|
|
|
|
.set("Content-Type", "application/x-www-form-urlencoded")?;
|
2022-03-30 07:36:59 +00:00
|
|
|
|
|
|
|
if let Some(etag) = if_match {
|
|
|
|
request.headers().set("If-match", &etag)?;
|
|
|
|
}
|
|
|
|
|
2022-03-25 18:18:55 +00:00
|
|
|
self::send_request(request).await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|