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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//! High-level interface for web_sys HTTP requests.
//! https://rustwasm.github.io/wasm-bindgen/examples/fetch.html
//! See https://rustwasm.github.io/wasm-bindgen/reference/js-promises-and-rust-futures.html
//! https://rustwasm.github.io/wasm-bindgen/api/web_sys/struct.Request.html
//! https://rustwasm.github.io/wasm-bindgen/api/wasm_bindgen_futures/

//#[macro_use]
//extern crate serde_derive;

use std::collections::HashMap;
use::std::hash::BuildHasher;

use futures::{future, Future};
//use js_sys;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
//use wasm_bindgen_futures::future_to_promise;
use wasm_bindgen_futures;
use web_sys::{Request, RequestInit, RequestMode, Response};


// todo debuggins
#[derive(Clone, Serialize, Deserialize)]
struct Data{
    val: i32,
    text: String,
}


/// https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
pub enum Method {
    Get,
    Head,
    Post,
    Put,
    Delete,
    Connect,
    Options,
    Trace,
    Patch
}

impl Method {
    fn as_str(&self) -> &str {
        match *self {
            Method::Get => "GET",
            Method::Head => "HEAD",
            Method::Post => "POST",
            Method::Put => "PUT",
            Method::Delete => "DELETE",
            Method::Connect => "CONNECT",
            Method::Options => "OPTIONS",
            Method::Trace => "TRACE",
            Method::Patch => "PATCH",
        }
    }
}

/// A wrapper over web_sys's fetch api, to simplify code
/// https://rustwasm.github.io/wasm-bindgen/examples/fetch.html
pub fn fetch<S>(method: Method, url: &str, payload: Option<String>,
//             headers: Option<HashMap<&str, &str>>,
//             cl: impl FnMut(wasm_bindgen::JsValue) -> future::FutureResult) -> js_sys::Promise {
//             headers: Option<HashMap<&str, &str, S>>, cl: impl FnMut(wasm_bindgen::JsValue)) -> js_sys::Promise
             headers: Option<HashMap<&str, &str, S>>) -> js_sys::Promise
//             headers: Option<HashMap<&str, &str>>) -> wasm_bindgen_futures::JsFuture {
//             headers: Option<HashMap<&str, &str>>) -> wasm_bindgen_futures::JsFuture {
    where S: BuildHasher
{
    let mut opts = RequestInit::new();
    opts.method(method.as_str());
    opts.mode(RequestMode::Cors);

    let request = Request::new_with_str_and_init(url, &opts).unwrap();

    // Set headers:
    // https://rustwasm.github.io/wasm-bindgen/api/web_sys/struct.Headers.html
    // https://developer.mozilla.org/en-US/docs/Web/API/Headers/set
    if let Some(h) = headers {
        let req_headers = request.headers();
        for (name, value) in &h {
            req_headers.set(&name, &value).unwrap();
        }
    }
//    req_headers.unwrap();

    let window = web_sys::window().unwrap();
    let request_promise = window.fetch_with_request(&request);

    let future = wasm_bindgen_futures::JsFuture::from(request_promise)
        .and_then(|resp_value| {
            // `resp_value` is a `Response` object.
            assert!(resp_value.is_instance_of::<Response>());
            let resp: Response = resp_value.dyn_into().unwrap();
            resp.json()
        })
        .and_then(|json_value: js_sys::Promise| {
            // Convert this other `Promise` into a rust `Future`.
            wasm_bindgen_futures::JsFuture::from(json_value)
        })

//        .and_then(cl);


        .and_then(|json| {
            // Use serde to parse the JSON into a struct.
            let data: Data = json.into_serde().unwrap();


            crate::log(data.text.clone());



            // Send the `Branch` struct back to JS as an `Object`.
            future::ok(JsValue::from_serde(&data).unwrap())
        });

    // Convert this Rust `Future` back into a JS `Promise`.

//    future
    wasm_bindgen_futures::future_to_promise(future)
}



//
///// A wrapper over web_sys's fetch api, to simplify code
///// https://rustwasm.github.io/wasm-bindgen/examples/fetch.html
//pub fn fetch<T>(method: ReqMethod, url: &str, payload: Option<String>,
//         headers: HashMap<String, String>, ex: T) -> T
//    where T: serde::Serialize + serde::Deserialize
//{
//    let mut opts = RequestInit::new();
//    opts.method(method.as_str());
//    opts.mode(RequestMode::Cors);
//
//    let request = Request::new_with_str_and_init(url, &opts).unwrap();
//
//    // Set headers:
//    // https://rustwasm.github.io/wasm-bindgen/api/web_sys/struct.Headers.html
//    // https://developer.mozilla.org/en-US/docs/Web/API/Headers/set
//    let req_headers = request.headers();
//    for (name, value) in &headers {
//        req_headers.set(&name, &value);
//    }
////    req_headers.unwrap();
//
//    let window = web_sys::window().unwrap();
//    let request_promise = window.fetch_with_request(&request);
//
//
//    let result: T;
//
//    let future = JsFuture::from(request_promise)
//        .and_then(|resp_value| {
//            // `resp_value` is a `Response` object.
//            assert!(resp_value.is_instance_of::<Response>());
//            let resp: Response = resp_value.dyn_into().unwrap();
//            resp.json()
//        })
//        .and_then(|json_value: js_sys::Promise| {
//            // Convert this other `Promise` into a rust `Future`.
//            JsFuture::from(json_value)
//        })
//        .and_then(|json| {
//            // Use serde to parse the JSON into a struct.
//            result = json.into_serde().unwrap();
//
//            // Send the `Branch` struct back to JS as an `Object`.
////            future::ok(JsValue::from_serde(&branch_info).unwrap())
//        });
//    result
//
//    // Convert this Rust `Future` back into a JS `Promise`.
////    wasm_bindgen_futures::future_to_promise(future)
//}







// todo add these back once you've stabilized your api.
//pub fn get(url: &str, payload: Option<String>, headers: HashMap<String, String>) {
//    fetch(ReqMethod::Post, url, payload, headers)
//}
//
//pub fn post(url: &str, payload: Option<String>, headers: HashMap<String, String>) {
//    fetch(ReqMethod::Post, url, payload, headers)
//}