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
//! The crate doesn't designed to be used itself anywhere. Outside
//! uniui_* crates familiy.

pub use implz::*;

use thiserror::Error;

/// Different kind of errors
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum UniNetError {
	#[error("Complicated error w/o known reason")]
	Unknown,
}

#[doc(hidden)]
#[cfg(target_os = "linux")]
pub mod implz {
	use super::UniNetError;

	/// HTTP-POST data as JSON to provided url
	pub fn post_json<F: 'static + FnOnce(u16, String)>(
		url: &str,
		data: &str,
		_f: F,
	) -> Result<(), UniNetError> {
		log::info!("post not implemented. url:{}, data:{}", url, data);
		return Ok(());
	}

	/// HTTP-GET data from provided url
	pub fn get<F: 'static + FnOnce(u16, String)>(
		url: &str,
		_f: F,
	) -> Result<(), UniNetError> {
		log::info!("get not implemented. address:{}", url);
		return Ok(());
	}

	/// Open provided location
	pub fn open(location: &str) -> Result<(), UniNetError> {
		log::info!("open not implemented. location:{}", location);
		return Ok(());
	}
}

#[doc(hidden)]
#[cfg(target_arch = "wasm32")]
pub mod implz {
	use super::UniNetError;

	use wasm_bindgen::{
		closure::Closure,
		JsValue,
	};
	use web_sys::{
		Request,
		RequestInit,
	};

	use std::{
		cell::RefCell,
		rc::Rc,
	};

	#[allow(unused_must_use)]
	pub fn post_json<F: 'static + FnOnce(u16, String)>(
		address: &str,
		data: &str,
		f: F,
	) -> Result<(), UniNetError> {
		let mut opts = RequestInit::new();
		opts.method("POST");
		opts.mode(web_sys::RequestMode::SameOrigin);
		opts.body(Some(&JsValue::from_str(data)));
		opts.redirect(web_sys::RequestRedirect::Manual);

		let request = Request::new_with_str_and_init(address, &opts).unwrap();
		request.headers().set("Content-Type", "application/json").unwrap();

		let window = web_sys::window().unwrap();

		let promise = window.fetch_with_request(&request);

		let o = Rc::new(RefCell::new(None));
		let oclone = o.clone();

		let o2 = Rc::new(RefCell::new(None));
		let oclone2 = o2.clone();

		let cb = Closure::once(move |v: JsValue| {
			let response: web_sys::Response = v.into();
			extract_payload(response, f);
			oclone.replace(None);
			oclone2.replace(None);
		});


		let oclone_err = o.clone();
		let oclone_err2 = o2.clone();

		let rej = Closure::once(move |_v: JsValue| {
			// process error
			oclone_err.replace(None);
			oclone_err2.replace(None);
		});

		promise.then2(&cb, &rej);
		o.replace(Some(cb));
		o2.replace(Some(rej));

		return Ok(());
	}

	#[allow(unused_must_use)]
	pub fn get<F: 'static + FnOnce(u16, String)>(
		address: &str,
		f: F,
	) -> Result<(), UniNetError> {
		let mut opts = RequestInit::new();
		opts.method("GET");
		opts.mode(web_sys::RequestMode::SameOrigin);
		opts.redirect(web_sys::RequestRedirect::Manual);

		let request = Request::new_with_str_and_init(address, &opts).unwrap();
		request.headers().set("Content-Type", "application/json").unwrap();
		let window = web_sys::window().unwrap();

		let promise = window.fetch_with_request(&request);

		let o = Rc::new(RefCell::new(None));
		let oclone = o.clone();
		let cb = Closure::once(move |v: JsValue| {
			let response: web_sys::Response = v.into();
			extract_payload(response, f);
			oclone.replace(None);
		});

		// process error
		promise.then(&cb);
		o.replace(Some(cb));

		return Ok(());
	}

	#[allow(unused_must_use)]
	fn extract_payload<F: 'static + FnOnce(u16, String)>(
		response: web_sys::Response,
		f: F,
	) {
		let code = response.status();

		let o = Rc::new(RefCell::new(None));
		let oclone = o.clone();
		let cb = Closure::once(move |v: JsValue| {
			match v.as_string() {
				None => {
					// process error
				},
				Some(s) => {
					f(code, s);
				},
			};
			oclone.replace(None);
		});

		response.text().unwrap().then(&cb);
		o.replace(Some(cb));
	}

	pub fn open(location: &str) -> Result<(), UniNetError> {
		return match web_sys::window() {
			Some(window) => {
				match window.open_with_url_and_target(location, "_self") {
					Ok(_) => Ok(()),
					Err(_) => Err(UniNetError::Unknown),
				}
			},
			None => Err(UniNetError::Unknown),
		};
	}
}