@@ -3,7 +3,7 @@ use std::convert::Infallible;
3
3
use async_trait:: async_trait;
4
4
use log:: { error, trace, warn} ;
5
5
use serde:: { de:: DeserializeOwned , Serialize } ;
6
- use serde_json:: from_str;
6
+ use serde_json:: { from_str, to_vec } ;
7
7
8
8
use crate :: errors:: { Error , MeilisearchCommunicationError , MeilisearchError } ;
9
9
@@ -17,6 +17,25 @@ pub enum Method<Q, B> {
17
17
}
18
18
19
19
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
+
20
39
pub fn query ( & self ) -> & Q {
21
40
match self {
22
41
Method :: Get { query } => query,
@@ -27,6 +46,15 @@ impl<Q, B> Method<Q, B> {
27
46
}
28
47
}
29
48
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
+
30
58
pub fn into_body ( self ) -> Option < B > {
31
59
match self {
32
60
Method :: Get { query : _ } | Method :: Delete { query : _ } => None ,
@@ -59,7 +87,17 @@ pub trait HttpClient: Clone + Send + Sync {
59
87
where
60
88
Query : Serialize + Send + Sync ,
61
89
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
+ }
63
101
64
102
async fn stream_request <
65
103
' a ,
@@ -110,41 +148,6 @@ impl ReqwestClient {
110
148
#[ cfg( feature = "reqwest" ) ]
111
149
#[ async_trait( ?Send ) ]
112
150
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
-
148
151
async fn stream_request <
149
152
' a ,
150
153
Query : Serialize + Send + Sync ,
0 commit comments