Skip to content

Commit 5e421b8

Browse files
committed
Add range() and refactor headers
1 parent fd9feab commit 5e421b8

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

src/builder.rs

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use reqwest::{Client, Error, Method, Response};
1+
use reqwest::{
2+
header::{HeaderMap, HeaderValue},
3+
Client, Error, Method, Response,
4+
};
25

36
macro_rules! filter {
47
( $( $op:ident ),* ) => {
@@ -19,7 +22,7 @@ pub struct Builder {
1922
schema: Option<String>,
2023
queries: Vec<(String, String)>,
2124
// TODO: Maybe change to HeaderMap in the future
22-
headers: Vec<(String, String)>,
25+
headers: HeaderMap,
2326
body: Option<String>,
2427
is_rpc: bool,
2528
}
@@ -34,6 +37,7 @@ impl Builder {
3437
method: Method::GET,
3538
url: url.to_string(),
3639
schema,
40+
headers: HeaderMap::new(),
3741
..Default::default()
3842
}
3943
}
@@ -60,18 +64,27 @@ impl Builder {
6064
self
6165
}
6266

63-
// TODO: Open-ended range
6467
pub fn limit(mut self, count: usize) -> Self {
65-
self.headers
66-
.push(("Content-Range".to_string(), format!("0-{}", count - 1)));
68+
self.headers.append(
69+
"Content-Range",
70+
HeaderValue::from_str(&format!("0-{}", count - 1)).unwrap(),
71+
);
72+
self
73+
}
74+
75+
pub fn range(mut self, low: usize, high: usize) -> Self {
76+
self.headers.append(
77+
"Content-Range",
78+
HeaderValue::from_str(&format!("{}-{}", low, high)).unwrap(),
79+
);
6780
self
6881
}
6982

7083
pub fn single(mut self) -> Self {
71-
self.headers.push((
72-
"Accept".to_string(),
73-
"application/vnd.pgrst.object+json".to_string(),
74-
));
84+
self.headers.append(
85+
"Accept",
86+
HeaderValue::from_static("application/vnd.pgrst.object+json"),
87+
);
7588
self
7689
}
7790

@@ -81,34 +94,34 @@ impl Builder {
8194
pub fn insert(mut self, body: &str) -> Self {
8295
self.method = Method::POST;
8396
self.headers
84-
.push(("Prefer".to_string(), "return=representation".to_string()));
97+
.append("Prefer", HeaderValue::from_static("return=representation"));
8598
self.body = Some(body.to_string());
8699
self
87100
}
88101

89102
pub fn insert_csv(mut self, body: &str) -> Self {
90103
self.headers
91-
.push(("Content-Type".to_string(), "text/csv".to_string()));
104+
.append("Content-Type", HeaderValue::from_static("text/csv"));
92105
self.insert(body)
93106
}
94107

95108
// TODO: Allow Prefer: resolution=ignore-duplicates
96109
// TODO: on_conflict (make UPSERT work on UNIQUE columns)
97110
pub fn upsert(mut self, body: &str) -> Self {
98111
self.method = Method::POST;
99-
self.headers.push((
100-
"Prefer".to_string(),
112+
self.headers.append(
113+
"Prefer",
101114
// Maybe check if this works as intended...
102-
"return=representation; resolution=merge-duplicates".to_string(),
103-
));
115+
HeaderValue::from_static("return=representation; resolution=merge-duplicates"),
116+
);
104117
self.body = Some(body.to_string());
105118
self
106119
}
107120

108121
pub fn single_upsert(mut self, primary_column: &str, key: &str, body: &str) -> Self {
109122
self.method = Method::PUT;
110123
self.headers
111-
.push(("Prefer".to_string(), "return=representation".to_string()));
124+
.append("Prefer", HeaderValue::from_static("return=representation"));
112125
self.queries
113126
.push((primary_column.to_string(), format!("eq.{}", key)));
114127
self.body = Some(body.to_string());
@@ -118,15 +131,15 @@ impl Builder {
118131
pub fn update(mut self, body: &str) -> Self {
119132
self.method = Method::PATCH;
120133
self.headers
121-
.push(("Prefer".to_string(), "return=representation".to_string()));
134+
.append("Prefer", HeaderValue::from_static("return=representation"));
122135
self.body = Some(body.to_string());
123136
self
124137
}
125138

126139
pub fn delete(mut self) -> Self {
127140
self.method = Method::DELETE;
128141
self.headers
129-
.push(("Prefer".to_string(), "return=representation".to_string()));
142+
.append("Prefer", HeaderValue::from_static("return=representation"));
130143
self
131144
}
132145

@@ -150,7 +163,7 @@ impl Builder {
150163
self
151164
}
152165

153-
pub async fn execute(self) -> Result<Response, Error> {
166+
pub async fn execute(mut self) -> Result<Response, Error> {
154167
let mut req = Client::new().request(self.method.clone(), &self.url);
155168
if let Some(schema) = self.schema {
156169
// NOTE: Upstream bug: RPC only works with Accept-Profile
@@ -160,12 +173,10 @@ impl Builder {
160173
} else {
161174
"Content-Profile"
162175
};
163-
req = req.header(key, schema);
164-
}
165-
for (k, v) in &self.headers {
166-
req = req.header(k, v);
176+
self.headers
177+
.append(key, HeaderValue::from_str(&schema).unwrap());
167178
}
168-
req = req.query(&self.queries);
179+
req = req.headers(self.headers).query(&self.queries);
169180
if let Some(body) = self.body {
170181
req = req.body(body);
171182
}

0 commit comments

Comments
 (0)