Skip to content

Commit 426c152

Browse files
committed
get rid of the WebSysClient for wasm since reqwest supports it by default
1 parent 315fbc3 commit 426c152

File tree

1 file changed

+0
-123
lines changed

1 file changed

+0
-123
lines changed

src/request.rs

Lines changed: 0 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ pub trait HttpClient: Clone + Send + Sync {
3030
Body: Serialize + Send + Sync,
3131
Output: DeserializeOwned + 'static + Send;
3232

33-
#[cfg(not(target_arch = "wasm32"))]
3433
async fn stream_request<
3534
'a,
3635
Query: Serialize + Send + Sync,
@@ -247,115 +246,6 @@ impl HttpClient for ReqwestClient {
247246
}
248247
}
249248

250-
#[cfg(target_arch = "wasm32")]
251-
#[async_trait(?Send)]
252-
impl HttpClient for WebSysClient {
253-
async fn request<Query, Body, Output>(
254-
self,
255-
url: &str,
256-
apikey: Option<&str>,
257-
method: Method<Query, Body>,
258-
expected_status_code: u16,
259-
) -> Result<Output, Error>
260-
where
261-
Query: Serialize + Send + Sync,
262-
Body: Serialize + Send + Sync,
263-
Output: DeserializeOwned + 'static,
264-
{
265-
use serde_json::to_string;
266-
use wasm_bindgen::JsValue;
267-
use wasm_bindgen_futures::JsFuture;
268-
use web_sys::{Headers, RequestInit, Response};
269-
270-
const CONTENT_TYPE: &str = "Content-Type";
271-
const JSON: &str = "application/json";
272-
273-
// The 2 following unwraps should not be able to fail
274-
let mut mut_url = url.clone().to_string();
275-
let headers = Headers::new().unwrap();
276-
if let Some(apikey) = apikey {
277-
headers
278-
.append("Authorization", format!("Bearer {}", apikey).as_str())
279-
.unwrap();
280-
}
281-
headers
282-
.append("X-Meilisearch-Client", qualified_version().as_str())
283-
.unwrap();
284-
285-
let mut request: RequestInit = RequestInit::new();
286-
request.headers(&headers);
287-
288-
match &method {
289-
Method::Get { query } => {
290-
mut_url = add_query_parameters(mut_url, &query)?;
291-
292-
request.method("GET");
293-
}
294-
Method::Delete { query } => {
295-
mut_url = add_query_parameters(mut_url, &query)?;
296-
request.method("DELETE");
297-
}
298-
Method::Patch { query, body } => {
299-
mut_url = add_query_parameters(mut_url, &query)?;
300-
request.method("PATCH");
301-
headers.append(CONTENT_TYPE, JSON).unwrap();
302-
request.body(Some(&JsValue::from_str(&to_string(body).unwrap())));
303-
}
304-
Method::Post { query, body } => {
305-
mut_url = add_query_parameters(mut_url, &query)?;
306-
request.method("POST");
307-
headers.append(CONTENT_TYPE, JSON).unwrap();
308-
request.body(Some(&JsValue::from_str(&to_string(body).unwrap())));
309-
}
310-
Method::Put { query, body } => {
311-
mut_url = add_query_parameters(mut_url, &query)?;
312-
request.method("PUT");
313-
headers.append(CONTENT_TYPE, JSON).unwrap();
314-
request.body(Some(&JsValue::from_str(&to_string(body).unwrap())));
315-
}
316-
}
317-
318-
let window = web_sys::window().unwrap(); // TODO remove this unwrap
319-
let response = match JsFuture::from(
320-
window.fetch_with_str_and_init(mut_url.as_str(), &request),
321-
)
322-
.await
323-
{
324-
Ok(response) => Response::from(response),
325-
Err(e) => {
326-
error!("Network error: {:?}", e);
327-
return Err(Error::UnreachableServer);
328-
}
329-
};
330-
let status = response.status() as u16;
331-
let text = match response.text() {
332-
Ok(text) => match JsFuture::from(text).await {
333-
Ok(text) => text,
334-
Err(e) => {
335-
error!("Invalid response: {:?}", e);
336-
return Err(Error::HttpError("Invalid response".to_string()));
337-
}
338-
},
339-
Err(e) => {
340-
error!("Invalid response: {:?}", e);
341-
return Err(Error::HttpError("Invalid response".to_string()));
342-
}
343-
};
344-
345-
if let Some(t) = text.as_string() {
346-
if t.is_empty() {
347-
parse_response(status, expected_status_code, "null", url.to_string())
348-
} else {
349-
parse_response(status, expected_status_code, &t, url.to_string())
350-
}
351-
} else {
352-
error!("Invalid response");
353-
Err(Error::HttpError("Invalid utf8".to_string()))
354-
}
355-
}
356-
}
357-
358-
#[cfg(not(target_arch = "wasm32"))]
359249
pub fn add_query_parameters<Query: Serialize>(url: &str, query: &Query) -> Result<String, Error> {
360250
let query = yaup::to_string(query)?;
361251

@@ -366,19 +256,6 @@ pub fn add_query_parameters<Query: Serialize>(url: &str, query: &Query) -> Resul
366256
}
367257
}
368258

369-
#[cfg(target_arch = "wasm32")]
370-
pub fn add_query_parameters<Query: Serialize>(
371-
mut url: String,
372-
query: &Query,
373-
) -> Result<String, Error> {
374-
let query = yaup::to_string(query)?;
375-
376-
if !query.is_empty() {
377-
url = format!("{}?{}", url, query);
378-
};
379-
return Ok(url);
380-
}
381-
382259
pub fn parse_response<Output: DeserializeOwned>(
383260
status_code: u16,
384261
expected_status_code: u16,

0 commit comments

Comments
 (0)