Introduce unstable, incomplete WASM support
This commit is contained in:
		
							
								
								
									
										73
									
								
								src/wasm/response.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										73
									
								
								src/wasm/response.rs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,73 @@ | ||||
| use std::fmt; | ||||
|  | ||||
| use http::{HeaderMap, StatusCode}; | ||||
|  | ||||
| /// A Response to a submitted `Request`. | ||||
| pub struct Response { | ||||
|     http: http::Response<web_sys::Response>, | ||||
| } | ||||
|  | ||||
| impl Response { | ||||
|     pub(super) fn new( | ||||
|         res: http::Response<web_sys::Response>, | ||||
|         //url: Url, | ||||
|     ) -> Response { | ||||
|         Response { | ||||
|             http: res, | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /// Get the `StatusCode` of this `Response`. | ||||
|     #[inline] | ||||
|     pub fn status(&self) -> StatusCode { | ||||
|         self.http.status() | ||||
|     } | ||||
|  | ||||
|     /// Get the `Headers` of this `Response`. | ||||
|     #[inline] | ||||
|     pub fn headers(&self) -> &HeaderMap { | ||||
|         self.http.headers() | ||||
|     } | ||||
|  | ||||
|     /// Get a mutable reference to the `Headers` of this `Response`. | ||||
|     #[inline] | ||||
|     pub fn headers_mut(&mut self) -> &mut HeaderMap { | ||||
|         self.http.headers_mut() | ||||
|     } | ||||
|  | ||||
|     /* It might not be possible to detect this in JS? | ||||
|     /// Get the HTTP `Version` of this `Response`. | ||||
|     #[inline] | ||||
|     pub fn version(&self) -> Version { | ||||
|         self.http.version() | ||||
|     } | ||||
|     */ | ||||
|  | ||||
|     // pub async fn json() | ||||
|  | ||||
|  | ||||
|     /// Get the response text. | ||||
|     pub async fn text(self) -> crate::Result<String> { | ||||
|         let p = self.http.body().text() | ||||
|             .map_err(crate::error::wasm) | ||||
|             .map_err(crate::error::decode)?; | ||||
|         let js_val = super::promise::<wasm_bindgen::JsValue>(p) | ||||
|             .await | ||||
|             .map_err(crate::error::decode)?; | ||||
|         if let Some(s) = js_val.as_string() { | ||||
|             Ok(s) | ||||
|         } else { | ||||
|             Err(crate::error::decode("response.text isn't string")) | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| impl fmt::Debug for Response { | ||||
|     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||||
|         f.debug_struct("Response") | ||||
|             //.field("url", self.url()) | ||||
|             .field("status", &self.status()) | ||||
|             .field("headers", self.headers()) | ||||
|             .finish() | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user