Created
December 30, 2018 00:05
-
-
Save David-OConnor/55c385dac86538fb19fcc6a43f1f3ae7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use wasm_bindgen::prelude::*; | |
pub fn log<S: ToString>(text: S) { | |
web_sys::console::log_1(&text.to_string().into()); | |
} | |
pub fn fetch( | |
method: &str, | |
url: &str, | |
callback: Box<Fn(JsValue)>) | |
{ | |
let mut opts = web_sys::RequestInit::new(); | |
opts.method(method); | |
opts.mode(web_sys::RequestMode::Cors); | |
let request = web_sys::Request::new_with_str_and_init(url, &opts) | |
.expect("Problem with request"); | |
request.headers().set("Content-Type", "application").unwrap(); | |
request.headers().set("Accept", "application/vnd.github.v3+json").unwrap(); | |
request.headers().set("Accept-Language", "en-us").unwrap(); | |
log(format!("CT: {:?}", request.headers().get("Content-Type").unwrap())); | |
log(format!("A: {:?}", request.headers().get("Accept").unwrap())); | |
log(format!("AL: {:?}", request.headers().get("Accept-Language").unwrap())); | |
let window = web_sys::window().expect("Can't find window"); | |
let request_promise = window.fetch_with_request(&request); | |
let f = wasm_bindgen_futures::JsFuture::from(request_promise) | |
.and_then(|resp_value| { | |
let resp: web_sys::Response = resp_value.dyn_into() | |
.expect("Problem casting response as Reponse."); | |
resp.json() | |
}) | |
.and_then(|json_value| { | |
// Convert this other `Promise` into a rust `Future`. | |
wasm_bindgen_futures::JsFuture::from(json_value) | |
}) | |
.and_then(move |v| { | |
callback(v); | |
future::ok(JsValue::null()) | |
}); | |
wasm_bindgen_futures::future_to_promise(f); | |
} | |
fn call_fetch(app: seed::App<Msg, Model>) { | |
let url = "https://infinitea.herokuapp.com/api/contact"; | |
let callback = |json: JsValue| { | |
}; | |
seed::fetch("get", url, Box::new(callback)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment