Skip to content

Commit b58c067

Browse files
committed
simplify the HttpClient implementation for everyone
1 parent 4d6c2bd commit b58c067

File tree

1 file changed

+40
-37
lines changed

1 file changed

+40
-37
lines changed

src/request.rs

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::convert::Infallible;
33
use async_trait::async_trait;
44
use log::{error, trace, warn};
55
use serde::{de::DeserializeOwned, Serialize};
6-
use serde_json::from_str;
6+
use serde_json::{from_str, to_vec};
77

88
use crate::errors::{Error, MeilisearchCommunicationError, MeilisearchError};
99

@@ -17,6 +17,25 @@ pub enum Method<Q, B> {
1717
}
1818

1919
impl<Q, B> Method<Q, B> {
20+
pub fn map_body<B2>(self, f: impl Fn(B) -> B2) -> Method<Q, B2> {
21+
match self {
22+
Method::Get { query } => Method::Get { query },
23+
Method::Delete { query } => Method::Delete { query },
24+
Method::Post { query, body } => Method::Post {
25+
query,
26+
body: f(body),
27+
},
28+
Method::Patch { query, body } => Method::Patch {
29+
query,
30+
body: f(body),
31+
},
32+
Method::Put { query, body } => Method::Put {
33+
query,
34+
body: f(body),
35+
},
36+
}
37+
}
38+
2039
pub fn query(&self) -> &Q {
2140
match self {
2241
Method::Get { query } => query,
@@ -27,6 +46,15 @@ impl<Q, B> Method<Q, B> {
2746
}
2847
}
2948

49+
pub fn body(&self) -> Option<&B> {
50+
match self {
51+
Method::Get { query: _ } | Method::Delete { query: _ } => None,
52+
Method::Post { body, query: _ } => Some(body),
53+
Method::Put { body, query: _ } => Some(body),
54+
Method::Patch { body, query: _ } => Some(body),
55+
}
56+
}
57+
3058
pub fn into_body(self) -> Option<B> {
3159
match self {
3260
Method::Get { query: _ } | Method::Delete { query: _ } => None,
@@ -59,7 +87,17 @@ pub trait HttpClient: Clone + Send + Sync {
5987
where
6088
Query: Serialize + Send + Sync,
6189
Body: Serialize + Send + Sync,
62-
Output: DeserializeOwned + 'static + Send;
90+
Output: DeserializeOwned + 'static + Send,
91+
{
92+
use futures::io::Cursor;
93+
self.stream_request(
94+
url,
95+
method.map_body(|body| Cursor::new(to_vec(&body).unwrap())),
96+
"application/json",
97+
expected_status_code,
98+
)
99+
.await
100+
}
63101

64102
async fn stream_request<
65103
'a,
@@ -110,41 +148,6 @@ impl ReqwestClient {
110148
#[cfg(feature = "reqwest")]
111149
#[async_trait(?Send)]
112150
impl HttpClient for ReqwestClient {
113-
async fn request<Query, Body, Output>(
114-
&self,
115-
url: &str,
116-
method: Method<Query, Body>,
117-
expected_status_code: u16,
118-
) -> Result<Output, Error>
119-
where
120-
Query: Serialize + Send + Sync,
121-
Body: Serialize + Send + Sync,
122-
Output: DeserializeOwned + 'static + Send,
123-
{
124-
use reqwest::header;
125-
use serde_json::to_string;
126-
127-
let url = add_query_parameters(url, method.query())?;
128-
129-
let mut request = self.client.request(method.verb(), &url);
130-
131-
if let Some(body) = method.into_body() {
132-
request = request
133-
.header(header::CONTENT_TYPE, "application/json")
134-
.body(to_string(&body).unwrap());
135-
}
136-
137-
let response = self.client.execute(request.build()?).await?;
138-
let status = response.status().as_u16();
139-
let mut body = response.text().await?;
140-
141-
if body.is_empty() {
142-
body = "null".to_string();
143-
}
144-
145-
parse_response(status, expected_status_code, &body, url.to_string())
146-
}
147-
148151
async fn stream_request<
149152
'a,
150153
Query: Serialize + Send + Sync,

0 commit comments

Comments
 (0)