1
+ extern crate reqwest;
2
+
1
3
use reqwest:: {
2
4
header:: { HeaderMap , HeaderValue } ,
3
5
Client , Error , Method , Response ,
@@ -20,13 +22,17 @@ pub struct Builder {
20
22
// TODO: Embedded resources
21
23
impl Builder {
22
24
pub fn new ( url : & str , schema : Option < String > ) -> Self {
23
- Builder {
25
+ let mut builder = Builder {
24
26
method : Method :: GET ,
25
27
url : url. to_string ( ) ,
26
28
schema,
27
29
headers : HeaderMap :: new ( ) ,
28
30
..Default :: default ( )
29
- }
31
+ } ;
32
+ builder
33
+ . headers
34
+ . insert ( "Accept" , HeaderValue :: from_static ( "application/json" ) ) ;
35
+ builder
30
36
}
31
37
32
38
pub fn auth ( mut self , token : & str ) -> Self {
@@ -60,16 +66,20 @@ impl Builder {
60
66
}
61
67
62
68
pub fn limit ( mut self , count : usize ) -> Self {
63
- self . headers . append (
64
- "Content-Range" ,
69
+ self . headers
70
+ . insert ( "Range-Unit" , HeaderValue :: from_static ( "items" ) ) ;
71
+ self . headers . insert (
72
+ "Range" ,
65
73
HeaderValue :: from_str ( & format ! ( "0-{}" , count - 1 ) ) . unwrap ( ) ,
66
74
) ;
67
75
self
68
76
}
69
77
70
78
pub fn range ( mut self , low : usize , high : usize ) -> Self {
71
- self . headers . append (
72
- "Content-Range" ,
79
+ self . headers
80
+ . insert ( "Range-Unit" , HeaderValue :: from_static ( "items" ) ) ;
81
+ self . headers . insert (
82
+ "Range" ,
73
83
HeaderValue :: from_str ( & format ! ( "{}-{}" , low, high) ) . unwrap ( ) ,
74
84
) ;
75
85
self
@@ -89,14 +99,14 @@ impl Builder {
89
99
pub fn insert ( mut self , body : & str ) -> Self {
90
100
self . method = Method :: POST ;
91
101
self . headers
92
- . append ( "Prefer" , HeaderValue :: from_static ( "return=representation" ) ) ;
102
+ . insert ( "Prefer" , HeaderValue :: from_static ( "return=representation" ) ) ;
93
103
self . body = Some ( body. to_string ( ) ) ;
94
104
self
95
105
}
96
106
97
107
pub fn insert_csv ( mut self , body : & str ) -> Self {
98
108
self . headers
99
- . append ( "Content-Type" , HeaderValue :: from_static ( "text/csv" ) ) ;
109
+ . insert ( "Content-Type" , HeaderValue :: from_static ( "text/csv" ) ) ;
100
110
self . insert ( body)
101
111
}
102
112
@@ -148,9 +158,7 @@ impl Builder {
148
158
pub async fn execute ( mut self ) -> Result < Response , Error > {
149
159
let mut req = Client :: new ( ) . request ( self . method . clone ( ) , & self . url ) ;
150
160
if let Some ( schema) = self . schema {
151
- // NOTE: Upstream bug: RPC only works with Accept-Profile
152
- // Will change when upstream is fixed
153
- let key = if !self . is_rpc || self . method == Method :: GET || self . method == Method :: HEAD {
161
+ let key = if self . method == Method :: GET || self . method == Method :: HEAD {
154
162
"Accept-Profile"
155
163
} else {
156
164
"Content-Profile"
@@ -163,8 +171,9 @@ impl Builder {
163
171
req = req. body ( body) ;
164
172
}
165
173
166
- let resp = req. send ( ) . await ?;
174
+ req. send ( ) . await
175
+ }
176
+ }
167
177
168
- Ok ( resp)
169
178
}
170
179
}
0 commit comments