|
| 1 | +use bytes::Bytes; |
| 2 | + |
| 3 | +use crate::transport::HttpLowLevel; |
| 4 | +use crate::transport::Transport; |
| 5 | +use crate::Result; |
| 6 | + |
| 7 | +mod builder; |
| 8 | +pub use builder::ClientBuilder; |
| 9 | + |
| 10 | +#[allow(dead_code)] |
| 11 | +pub const TYPESENSE_API_KEY_HEADER_NAME: &str = "X-TYPESENSE-API-KEY"; |
| 12 | + |
| 13 | +/// Root client for top level APIs |
| 14 | +pub struct Client<'a, T> { |
| 15 | + transport: Transport<T>, |
| 16 | + host: &'a str, |
| 17 | + api_key: &'a str, |
| 18 | +} |
| 19 | + |
| 20 | +impl<'a, T> Client<'a, T> { |
| 21 | + /// Gets the transport of the client |
| 22 | + pub fn transport(&self) -> &Transport<T> { |
| 23 | + &self.transport |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +#[allow(dead_code)] |
| 28 | +impl<'a, C> Client<'a, C> |
| 29 | +where |
| 30 | + C: HttpLowLevel, |
| 31 | +{ |
| 32 | + pub(crate) async fn send( |
| 33 | + &self, |
| 34 | + method: http::Method, |
| 35 | + path: &str, |
| 36 | + body: Bytes, |
| 37 | + ) -> Result<C::Response> { |
| 38 | + let uri = format!("{}{}", self.host, path); |
| 39 | + let mut headers = http::HeaderMap::default(); |
| 40 | + headers.insert(TYPESENSE_API_KEY_HEADER_NAME, self.api_key.parse().unwrap()); |
| 41 | + self.transport.send(method, &uri, headers, body).await |
| 42 | + } |
| 43 | + |
| 44 | + pub(crate) async fn get(&self, path: &str) -> Result<C::Response> { |
| 45 | + self.send(http::Method::GET, path, Bytes::new()).await |
| 46 | + } |
| 47 | + |
| 48 | + pub(crate) async fn post(&self, path: &str, body: Bytes) -> Result<C::Response> { |
| 49 | + self.send(http::Method::POST, path, body).await |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +#[cfg(all(test, feature = "tokio-rt", not(target_arch = "wasm32")))] |
| 54 | +mod hyper_tests { |
| 55 | + use http::StatusCode; |
| 56 | + |
| 57 | + use super::*; |
| 58 | + |
| 59 | + #[tokio::test] |
| 60 | + async fn hyper() -> crate::Result<()> { |
| 61 | + let body = String::from("Test with api key successful"); |
| 62 | + let host = "http://localhost:5000"; |
| 63 | + let api_key = "VerySecretKey"; |
| 64 | + |
| 65 | + let client = ClientBuilder::new_hyper() |
| 66 | + .host(host) |
| 67 | + .api_key(api_key) |
| 68 | + .build() |
| 69 | + .unwrap(); |
| 70 | + |
| 71 | + let response = client.get("/test_api_key").await?; |
| 72 | + |
| 73 | + assert_eq!(response.status(), StatusCode::OK); |
| 74 | + let bytes = hyper::body::to_bytes(response).await?; |
| 75 | + assert_eq!(bytes, body.as_bytes()); |
| 76 | + |
| 77 | + let response = client.post("/test_api_key", body.clone().into()).await?; |
| 78 | + |
| 79 | + assert_eq!(response.status(), StatusCode::OK); |
| 80 | + let bytes = hyper::body::to_bytes(response).await?; |
| 81 | + assert_eq!(bytes, body.as_bytes()); |
| 82 | + |
| 83 | + Ok(()) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +#[cfg(all(test, target_arch = "wasm32"))] |
| 88 | +mod wasm_test { |
| 89 | + wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); |
| 90 | + |
| 91 | + use http::StatusCode; |
| 92 | + use wasm_bindgen::prelude::*; |
| 93 | + use wasm_bindgen_test::wasm_bindgen_test; |
| 94 | + |
| 95 | + use super::*; |
| 96 | + |
| 97 | + #[wasm_bindgen] |
| 98 | + extern "C" { |
| 99 | + #[wasm_bindgen(js_namespace = console)] |
| 100 | + fn log(s: &str); |
| 101 | + } |
| 102 | + |
| 103 | + macro_rules! console_log { |
| 104 | + ($($t:tt)*) => (log(&format_args!($($t)*).to_string())) |
| 105 | + } |
| 106 | + |
| 107 | + #[wasm_bindgen_test] |
| 108 | + async fn wasm() { |
| 109 | + console_error_panic_hook::set_once(); |
| 110 | + |
| 111 | + console_log!("Test Started."); |
| 112 | + match try_wasm().await { |
| 113 | + Ok(_) => console_log!("Test Successful."), |
| 114 | + Err(e) => console_log!("Test failed: {:?}", e), |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + async fn try_wasm() -> crate::Result<()> { |
| 119 | + let body = String::from("Test with api key successful"); |
| 120 | + |
| 121 | + let host = "http://localhost:5000"; |
| 122 | + let api_key = "VerySecretKey"; |
| 123 | + |
| 124 | + let client = ClientBuilder::new_wasm() |
| 125 | + .host(host) |
| 126 | + .api_key(api_key) |
| 127 | + .build() |
| 128 | + .unwrap(); |
| 129 | + |
| 130 | + let response = client.get("/test_api_key").await?; |
| 131 | + |
| 132 | + assert_eq!(response.status(), StatusCode::OK); |
| 133 | + assert_eq!(response.body(), body.as_bytes()); |
| 134 | + |
| 135 | + let response = client.post("/test_api_key", body.clone().into()).await?; |
| 136 | + |
| 137 | + assert_eq!(response.status(), StatusCode::OK); |
| 138 | + assert_eq!(response.body(), body.as_bytes()); |
| 139 | + |
| 140 | + Ok(()) |
| 141 | + } |
| 142 | +} |
0 commit comments