1
- use reqwest:: { Client , Error , Method , Response } ;
1
+ use reqwest:: {
2
+ header:: { HeaderMap , HeaderValue } ,
3
+ Client , Error , Method , Response ,
4
+ } ;
2
5
3
6
macro_rules! filter {
4
7
( $( $op: ident ) ,* ) => {
@@ -19,7 +22,7 @@ pub struct Builder {
19
22
schema : Option < String > ,
20
23
queries : Vec < ( String , String ) > ,
21
24
// TODO: Maybe change to HeaderMap in the future
22
- headers : Vec < ( String , String ) > ,
25
+ headers : HeaderMap ,
23
26
body : Option < String > ,
24
27
is_rpc : bool ,
25
28
}
@@ -34,6 +37,7 @@ impl Builder {
34
37
method : Method :: GET ,
35
38
url : url. to_string ( ) ,
36
39
schema,
40
+ headers : HeaderMap :: new ( ) ,
37
41
..Default :: default ( )
38
42
}
39
43
}
@@ -60,18 +64,27 @@ impl Builder {
60
64
self
61
65
}
62
66
63
- // TODO: Open-ended range
64
67
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
+ ) ;
67
80
self
68
81
}
69
82
70
83
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
+ ) ;
75
88
self
76
89
}
77
90
@@ -81,34 +94,34 @@ impl Builder {
81
94
pub fn insert ( mut self , body : & str ) -> Self {
82
95
self . method = Method :: POST ;
83
96
self . headers
84
- . push ( ( "Prefer" . to_string ( ) , "return=representation" . to_string ( ) ) ) ;
97
+ . append ( "Prefer" , HeaderValue :: from_static ( "return=representation" ) ) ;
85
98
self . body = Some ( body. to_string ( ) ) ;
86
99
self
87
100
}
88
101
89
102
pub fn insert_csv ( mut self , body : & str ) -> Self {
90
103
self . headers
91
- . push ( ( "Content-Type" . to_string ( ) , "text/csv" . to_string ( ) ) ) ;
104
+ . append ( "Content-Type" , HeaderValue :: from_static ( "text/csv" ) ) ;
92
105
self . insert ( body)
93
106
}
94
107
95
108
// TODO: Allow Prefer: resolution=ignore-duplicates
96
109
// TODO: on_conflict (make UPSERT work on UNIQUE columns)
97
110
pub fn upsert ( mut self , body : & str ) -> Self {
98
111
self . method = Method :: POST ;
99
- self . headers . push ( (
100
- "Prefer" . to_string ( ) ,
112
+ self . headers . append (
113
+ "Prefer" ,
101
114
// 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
+ ) ;
104
117
self . body = Some ( body. to_string ( ) ) ;
105
118
self
106
119
}
107
120
108
121
pub fn single_upsert ( mut self , primary_column : & str , key : & str , body : & str ) -> Self {
109
122
self . method = Method :: PUT ;
110
123
self . headers
111
- . push ( ( "Prefer" . to_string ( ) , "return=representation" . to_string ( ) ) ) ;
124
+ . append ( "Prefer" , HeaderValue :: from_static ( "return=representation" ) ) ;
112
125
self . queries
113
126
. push ( ( primary_column. to_string ( ) , format ! ( "eq.{}" , key) ) ) ;
114
127
self . body = Some ( body. to_string ( ) ) ;
@@ -118,15 +131,15 @@ impl Builder {
118
131
pub fn update ( mut self , body : & str ) -> Self {
119
132
self . method = Method :: PATCH ;
120
133
self . headers
121
- . push ( ( "Prefer" . to_string ( ) , "return=representation" . to_string ( ) ) ) ;
134
+ . append ( "Prefer" , HeaderValue :: from_static ( "return=representation" ) ) ;
122
135
self . body = Some ( body. to_string ( ) ) ;
123
136
self
124
137
}
125
138
126
139
pub fn delete ( mut self ) -> Self {
127
140
self . method = Method :: DELETE ;
128
141
self . headers
129
- . push ( ( "Prefer" . to_string ( ) , "return=representation" . to_string ( ) ) ) ;
142
+ . append ( "Prefer" , HeaderValue :: from_static ( "return=representation" ) ) ;
130
143
self
131
144
}
132
145
@@ -150,7 +163,7 @@ impl Builder {
150
163
self
151
164
}
152
165
153
- pub async fn execute ( self ) -> Result < Response , Error > {
166
+ pub async fn execute ( mut self ) -> Result < Response , Error > {
154
167
let mut req = Client :: new ( ) . request ( self . method . clone ( ) , & self . url ) ;
155
168
if let Some ( schema) = self . schema {
156
169
// NOTE: Upstream bug: RPC only works with Accept-Profile
@@ -160,12 +173,10 @@ impl Builder {
160
173
} else {
161
174
"Content-Profile"
162
175
} ;
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 ( ) ) ;
167
178
}
168
- req = req. query ( & self . queries ) ;
179
+ req = req. headers ( self . headers ) . query ( & self . queries ) ;
169
180
if let Some ( body) = self . body {
170
181
req = req. body ( body) ;
171
182
}
0 commit comments