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,10 +37,19 @@ 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
}
40
44
45
+ pub fn auth ( mut self , token : & str ) -> Self {
46
+ self . headers . append (
47
+ "Authorization" ,
48
+ HeaderValue :: from_str ( & format ! ( "Bearer {}" , token) ) . unwrap ( ) ,
49
+ ) ;
50
+ self
51
+ }
52
+
41
53
// TODO: Multiple columns
42
54
// TODO: Renaming columns
43
55
// TODO: Casting columns
@@ -60,18 +72,27 @@ impl Builder {
60
72
self
61
73
}
62
74
63
- // TODO: Open-ended range
64
75
pub fn limit ( mut self , count : usize ) -> Self {
65
- self . headers
66
- . push ( ( "Content-Range" . to_string ( ) , format ! ( "0-{}" , count - 1 ) ) ) ;
76
+ self . headers . append (
77
+ "Content-Range" ,
78
+ HeaderValue :: from_str ( & format ! ( "0-{}" , count - 1 ) ) . unwrap ( ) ,
79
+ ) ;
80
+ self
81
+ }
82
+
83
+ pub fn range ( mut self , low : usize , high : usize ) -> Self {
84
+ self . headers . append (
85
+ "Content-Range" ,
86
+ HeaderValue :: from_str ( & format ! ( "{}-{}" , low, high) ) . unwrap ( ) ,
87
+ ) ;
67
88
self
68
89
}
69
90
70
91
pub fn single ( mut self ) -> Self {
71
- self . headers . push ( (
72
- "Accept" . to_string ( ) ,
73
- "application/vnd.pgrst.object+json" . to_string ( ) ,
74
- ) ) ;
92
+ self . headers . insert (
93
+ "Accept" ,
94
+ HeaderValue :: from_static ( "application/vnd.pgrst.object+json" ) ,
95
+ ) ;
75
96
self
76
97
}
77
98
@@ -81,34 +102,34 @@ impl Builder {
81
102
pub fn insert ( mut self , body : & str ) -> Self {
82
103
self . method = Method :: POST ;
83
104
self . headers
84
- . push ( ( "Prefer" . to_string ( ) , "return=representation" . to_string ( ) ) ) ;
105
+ . append ( "Prefer" , HeaderValue :: from_static ( "return=representation" ) ) ;
85
106
self . body = Some ( body. to_string ( ) ) ;
86
107
self
87
108
}
88
109
89
110
pub fn insert_csv ( mut self , body : & str ) -> Self {
90
111
self . headers
91
- . push ( ( "Content-Type" . to_string ( ) , "text/csv" . to_string ( ) ) ) ;
112
+ . append ( "Content-Type" , HeaderValue :: from_static ( "text/csv" ) ) ;
92
113
self . insert ( body)
93
114
}
94
115
95
116
// TODO: Allow Prefer: resolution=ignore-duplicates
96
117
// TODO: on_conflict (make UPSERT work on UNIQUE columns)
97
118
pub fn upsert ( mut self , body : & str ) -> Self {
98
119
self . method = Method :: POST ;
99
- self . headers . push ( (
100
- "Prefer" . to_string ( ) ,
120
+ self . headers . append (
121
+ "Prefer" ,
101
122
// Maybe check if this works as intended...
102
- "return=representation; resolution=merge-duplicates" . to_string ( ) ,
103
- ) ) ;
123
+ HeaderValue :: from_static ( "return=representation; resolution=merge-duplicates" ) ,
124
+ ) ;
104
125
self . body = Some ( body. to_string ( ) ) ;
105
126
self
106
127
}
107
128
108
129
pub fn single_upsert ( mut self , primary_column : & str , key : & str , body : & str ) -> Self {
109
130
self . method = Method :: PUT ;
110
131
self . headers
111
- . push ( ( "Prefer" . to_string ( ) , "return=representation" . to_string ( ) ) ) ;
132
+ . append ( "Prefer" , HeaderValue :: from_static ( "return=representation" ) ) ;
112
133
self . queries
113
134
. push ( ( primary_column. to_string ( ) , format ! ( "eq.{}" , key) ) ) ;
114
135
self . body = Some ( body. to_string ( ) ) ;
@@ -118,15 +139,15 @@ impl Builder {
118
139
pub fn update ( mut self , body : & str ) -> Self {
119
140
self . method = Method :: PATCH ;
120
141
self . headers
121
- . push ( ( "Prefer" . to_string ( ) , "return=representation" . to_string ( ) ) ) ;
142
+ . append ( "Prefer" , HeaderValue :: from_static ( "return=representation" ) ) ;
122
143
self . body = Some ( body. to_string ( ) ) ;
123
144
self
124
145
}
125
146
126
147
pub fn delete ( mut self ) -> Self {
127
148
self . method = Method :: DELETE ;
128
149
self . headers
129
- . push ( ( "Prefer" . to_string ( ) , "return=representation" . to_string ( ) ) ) ;
150
+ . append ( "Prefer" , HeaderValue :: from_static ( "return=representation" ) ) ;
130
151
self
131
152
}
132
153
@@ -150,7 +171,7 @@ impl Builder {
150
171
self
151
172
}
152
173
153
- pub async fn execute ( self ) -> Result < Response , Error > {
174
+ pub async fn execute ( mut self ) -> Result < Response , Error > {
154
175
let mut req = Client :: new ( ) . request ( self . method . clone ( ) , & self . url ) ;
155
176
if let Some ( schema) = self . schema {
156
177
// NOTE: Upstream bug: RPC only works with Accept-Profile
@@ -160,12 +181,10 @@ impl Builder {
160
181
} else {
161
182
"Content-Profile"
162
183
} ;
163
- req = req. header ( key, schema) ;
164
- }
165
- for ( k, v) in & self . headers {
166
- req = req. header ( k, v) ;
184
+ self . headers
185
+ . append ( key, HeaderValue :: from_str ( & schema) . unwrap ( ) ) ;
167
186
}
168
- req = req. query ( & self . queries ) ;
187
+ req = req. headers ( self . headers ) . query ( & self . queries ) ;
169
188
if let Some ( body) = self . body {
170
189
req = req. body ( body) ;
171
190
}
0 commit comments