Skip to content

Commit eae4c74

Browse files
committed
More fixes of builder
1 parent 8c54138 commit eae4c74

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/builder.rs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
extern crate reqwest;
2-
31
use reqwest::{
42
header::{HeaderMap, HeaderValue},
53
Client, Error, Method, Response,
@@ -21,6 +19,7 @@ pub struct Builder {
2119
// TODO: Response format
2220
// TODO: Resource embedding (embedded filters, etc.)
2321
// TODO: Content-Type (text/csv, etc.)
22+
// TODO: Reject update/delete w/o filters
2423
impl Builder {
2524
pub fn new<S>(url: S, schema: Option<String>) -> Self
2625
where
@@ -128,8 +127,7 @@ impl Builder {
128127
self.method = Method::POST;
129128
self.headers.insert(
130129
"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"),
133131
);
134132
self.body = Some(body.into());
135133
self
@@ -189,6 +187,10 @@ impl Builder {
189187
self.headers
190188
.append(key, HeaderValue::from_str(&schema).unwrap());
191189
}
190+
if self.method != Method::GET && self.method != Method::HEAD {
191+
self.headers
192+
.insert("Content-Type", HeaderValue::from_static("application/json"));
193+
}
192194
req = req.headers(self.headers).query(&self.queries);
193195
if let Some(body) = self.body {
194196
req = req.body(body);
@@ -278,7 +280,7 @@ mod tests {
278280
let builder = Builder::new(TABLE_URL, None).upsert("ignored");
279281
assert_eq!(
280282
builder.headers.get("Prefer").unwrap(),
281-
HeaderValue::from_static("return=representation; resolution=merge-duplicates")
283+
HeaderValue::from_static("return=representation,resolution=merge-duplicates")
282284
);
283285
}
284286

@@ -303,4 +305,21 @@ mod tests {
303305
assert_eq!(builder.body.unwrap(), "{\"a\": 1, \"b\": 2}");
304306
assert_eq!(builder.is_rpc, true);
305307
}
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+
}
306325
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
extern crate reqwest;
2+
13
mod builder;
24
mod filter;
35

0 commit comments

Comments
 (0)