1
- extern crate reqwest;
2
-
3
1
use reqwest:: {
4
2
header:: { HeaderMap , HeaderValue } ,
5
3
Client , Error , Method , Response ,
@@ -19,8 +17,9 @@ pub struct Builder {
19
17
// TODO: Complex filters (not, and, or)
20
18
// TODO: Exact, planned, estimated count (HEAD verb)
21
19
// TODO: Response format
22
- // TODO: Embedded resources
23
- // TODO: Content type (csv, etc.)
20
+ // TODO: Resource embedding (embedded filters, etc.)
21
+ // TODO: Content-Type (text/csv, etc.)
22
+ // TODO: Reject update/delete w/o filters
24
23
impl Builder {
25
24
pub fn new < S > ( url : S , schema : Option < String > ) -> Self
26
25
where
@@ -126,10 +125,9 @@ impl Builder {
126
125
S : Into < String > ,
127
126
{
128
127
self . method = Method :: POST ;
129
- self . headers . append (
128
+ self . headers . insert (
130
129
"Prefer" ,
131
- // Maybe check if this works as intended...
132
- HeaderValue :: from_static ( "return=representation; resolution=merge-duplicates" ) ,
130
+ HeaderValue :: from_static ( "return=representation,resolution=merge-duplicates" ) ,
133
131
) ;
134
132
self . body = Some ( body. into ( ) ) ;
135
133
self
@@ -143,7 +141,7 @@ impl Builder {
143
141
{
144
142
self . method = Method :: PUT ;
145
143
self . headers
146
- . append ( "Prefer" , HeaderValue :: from_static ( "return=representation" ) ) ;
144
+ . insert ( "Prefer" , HeaderValue :: from_static ( "return=representation" ) ) ;
147
145
self . queries
148
146
. push ( ( primary_column. into ( ) , format ! ( "eq.{}" , key. into( ) ) ) ) ;
149
147
self . body = Some ( body. into ( ) ) ;
@@ -156,15 +154,15 @@ impl Builder {
156
154
{
157
155
self . method = Method :: PATCH ;
158
156
self . headers
159
- . append ( "Prefer" , HeaderValue :: from_static ( "return=representation" ) ) ;
157
+ . insert ( "Prefer" , HeaderValue :: from_static ( "return=representation" ) ) ;
160
158
self . body = Some ( body. into ( ) ) ;
161
159
self
162
160
}
163
161
164
162
pub fn delete ( mut self ) -> Self {
165
163
self . method = Method :: DELETE ;
166
164
self . headers
167
- . append ( "Prefer" , HeaderValue :: from_static ( "return=representation" ) ) ;
165
+ . insert ( "Prefer" , HeaderValue :: from_static ( "return=representation" ) ) ;
168
166
self
169
167
}
170
168
@@ -189,6 +187,10 @@ impl Builder {
189
187
self . headers
190
188
. append ( key, HeaderValue :: from_str ( & schema) . unwrap ( ) ) ;
191
189
}
190
+ if self . method != Method :: GET && self . method != Method :: HEAD {
191
+ self . headers
192
+ . insert ( "Content-Type" , HeaderValue :: from_static ( "application/json" ) ) ;
193
+ }
192
194
req = req. headers ( self . headers ) . query ( & self . queries ) ;
193
195
if let Some ( body) = self . body {
194
196
req = req. body ( body) ;
@@ -278,7 +280,7 @@ mod tests {
278
280
let builder = Builder :: new ( TABLE_URL , None ) . upsert ( "ignored" ) ;
279
281
assert_eq ! (
280
282
builder. headers. get( "Prefer" ) . unwrap( ) ,
281
- HeaderValue :: from_static( "return=representation; resolution=merge-duplicates" )
283
+ HeaderValue :: from_static( "return=representation, resolution=merge-duplicates" )
282
284
) ;
283
285
}
284
286
@@ -303,4 +305,21 @@ mod tests {
303
305
assert_eq ! ( builder. body. unwrap( ) , "{\" a\" : 1, \" b\" : 2}" ) ;
304
306
assert_eq ! ( builder. is_rpc, true ) ;
305
307
}
308
+
309
+ #[ test]
310
+ fn chain_filters ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
311
+ let builder = Builder :: new ( TABLE_URL , None )
312
+ . eq ( "username" , "supabot" )
313
+ . neq ( "message" , "hello world" )
314
+ . gte ( "channel_id" , "1" )
315
+ . select ( "*" ) ;
316
+
317
+ let queries = builder. queries ;
318
+ assert_eq ! ( queries. len( ) , 4 ) ;
319
+ assert ! ( queries. contains( & ( "username" . into( ) , "eq.supabot" . into( ) ) ) ) ;
320
+ assert ! ( queries. contains( & ( "message" . into( ) , "neq.hello world" . into( ) ) ) ) ;
321
+ assert ! ( queries. contains( & ( "channel_id" . into( ) , "gte.1" . into( ) ) ) ) ;
322
+
323
+ Ok ( ( ) )
324
+ }
306
325
}
0 commit comments