@@ -54,6 +54,7 @@ impl Debug for HttpClient {
5454}
5555
5656impl HttpClient {
57+ /// Create a new http client.
5758 pub fn new ( cfg : & RestCatalogConfig ) -> Result < Self > {
5859 Ok ( HttpClient {
5960 client : Client :: new ( ) ,
@@ -66,6 +67,32 @@ impl HttpClient {
6667 } )
6768 }
6869
70+ /// Update the http client with new configuration.
71+ ///
72+ /// If cfg carries new value, we will use cfg instead.
73+ /// Otherwise, we will keep the old value.
74+ pub fn update_with ( self , cfg : & RestCatalogConfig ) -> Result < Self > {
75+ Ok ( HttpClient {
76+ client : self . client ,
77+
78+ token : Mutex :: new (
79+ cfg. token ( )
80+ . or_else ( || self . token . into_inner ( ) . ok ( ) . flatten ( ) ) ,
81+ ) ,
82+ token_endpoint : ( !cfg. get_token_endpoint ( ) . is_empty ( ) )
83+ . then ( || cfg. get_token_endpoint ( ) )
84+ . unwrap_or ( self . token_endpoint ) ,
85+ credential : cfg. credential ( ) . or ( self . credential ) ,
86+ extra_headers : ( !cfg. extra_headers ( ) ?. is_empty ( ) )
87+ . then ( || cfg. extra_headers ( ) )
88+ . transpose ( ) ?
89+ . unwrap_or ( self . extra_headers ) ,
90+ extra_oauth_params : ( !cfg. extra_oauth_params ( ) . is_empty ( ) )
91+ . then ( || cfg. extra_oauth_params ( ) )
92+ . unwrap_or ( self . extra_oauth_params ) ,
93+ } )
94+ }
95+
6996 /// This API is testing only to assert the token.
7097 #[ cfg( test) ]
7198 pub ( crate ) async fn token ( & self ) -> Option < String > {
@@ -134,28 +161,39 @@ impl HttpClient {
134161 . request ( Method :: POST , & self . token_endpoint )
135162 . form ( & params)
136163 . build ( ) ?;
164+ let auth_url = auth_req. url ( ) . clone ( ) ;
137165 let auth_resp = self . client . execute ( auth_req) . await ?;
138166
139167 let auth_res: TokenResponse = if auth_resp. status ( ) . as_u16 ( ) == OK {
140- let text = auth_resp. bytes ( ) . await ?;
168+ let text = auth_resp
169+ . bytes ( )
170+ . await
171+ . map_err ( |err| err. with_url ( auth_url. clone ( ) ) ) ?;
141172 Ok ( serde_json:: from_slice ( & text) . map_err ( |e| {
142173 Error :: new (
143174 ErrorKind :: Unexpected ,
144175 "Failed to parse response from rest catalog server!" ,
145176 )
177+ . with_context ( "operation" , "auth" )
178+ . with_context ( "url" , auth_url. to_string ( ) )
146179 . with_context ( "json" , String :: from_utf8_lossy ( & text) )
147180 . with_source ( e)
148181 } ) ?)
149182 } else {
150183 let code = auth_resp. status ( ) ;
151- let text = auth_resp. bytes ( ) . await ?;
184+ let text = auth_resp
185+ . bytes ( )
186+ . await
187+ . map_err ( |err| err. with_url ( auth_url. clone ( ) ) ) ?;
152188 let e: ErrorResponse = serde_json:: from_slice ( & text) . map_err ( |e| {
153189 Error :: new (
154190 ErrorKind :: Unexpected ,
155191 "Failed to parse response from rest catalog server!" ,
156192 )
157- . with_context ( "json" , String :: from_utf8_lossy ( & text) )
158193 . with_context ( "code" , code. to_string ( ) )
194+ . with_context ( "operation" , "auth" )
195+ . with_context ( "url" , auth_url. to_string ( ) )
196+ . with_context ( "json" , String :: from_utf8_lossy ( & text) )
159197 . with_source ( e)
160198 } ) ?;
161199 Err ( Error :: from ( e) )
@@ -193,28 +231,41 @@ impl HttpClient {
193231 ) -> Result < R > {
194232 self . authenticate ( & mut request) . await ?;
195233
234+ let method = request. method ( ) . clone ( ) ;
235+ let url = request. url ( ) . clone ( ) ;
236+
196237 let resp = self . client . execute ( request) . await ?;
197238
198239 if resp. status ( ) . as_u16 ( ) == SUCCESS_CODE {
199- let text = resp. bytes ( ) . await ?;
240+ let text = resp
241+ . bytes ( )
242+ . await
243+ . map_err ( |err| err. with_url ( url. clone ( ) ) ) ?;
200244 Ok ( serde_json:: from_slice :: < R > ( & text) . map_err ( |e| {
201245 Error :: new (
202246 ErrorKind :: Unexpected ,
203247 "Failed to parse response from rest catalog server!" ,
204248 )
249+ . with_context ( "method" , method. to_string ( ) )
250+ . with_context ( "url" , url. to_string ( ) )
205251 . with_context ( "json" , String :: from_utf8_lossy ( & text) )
206252 . with_source ( e)
207253 } ) ?)
208254 } else {
209255 let code = resp. status ( ) ;
210- let text = resp. bytes ( ) . await ?;
256+ let text = resp
257+ . bytes ( )
258+ . await
259+ . map_err ( |err| err. with_url ( url. clone ( ) ) ) ?;
211260 let e = serde_json:: from_slice :: < E > ( & text) . map_err ( |e| {
212261 Error :: new (
213262 ErrorKind :: Unexpected ,
214263 "Failed to parse response from rest catalog server!" ,
215264 )
216- . with_context ( "json" , String :: from_utf8_lossy ( & text) )
217265 . with_context ( "code" , code. to_string ( ) )
266+ . with_context ( "method" , method. to_string ( ) )
267+ . with_context ( "url" , url. to_string ( ) )
268+ . with_context ( "json" , String :: from_utf8_lossy ( & text) )
218269 . with_source ( e)
219270 } ) ?;
220271 Err ( e. into ( ) )
@@ -227,20 +278,28 @@ impl HttpClient {
227278 ) -> Result < ( ) > {
228279 self . authenticate ( & mut request) . await ?;
229280
281+ let method = request. method ( ) . clone ( ) ;
282+ let url = request. url ( ) . clone ( ) ;
283+
230284 let resp = self . client . execute ( request) . await ?;
231285
232286 if resp. status ( ) . as_u16 ( ) == SUCCESS_CODE {
233287 Ok ( ( ) )
234288 } else {
235289 let code = resp. status ( ) ;
236- let text = resp. bytes ( ) . await ?;
290+ let text = resp
291+ . bytes ( )
292+ . await
293+ . map_err ( |err| err. with_url ( url. clone ( ) ) ) ?;
237294 let e = serde_json:: from_slice :: < E > ( & text) . map_err ( |e| {
238295 Error :: new (
239296 ErrorKind :: Unexpected ,
240297 "Failed to parse response from rest catalog server!" ,
241298 )
242- . with_context ( "json" , String :: from_utf8_lossy ( & text) )
243299 . with_context ( "code" , code. to_string ( ) )
300+ . with_context ( "method" , method. to_string ( ) )
301+ . with_context ( "url" , url. to_string ( ) )
302+ . with_context ( "json" , String :: from_utf8_lossy ( & text) )
244303 . with_source ( e)
245304 } ) ?;
246305 Err ( e. into ( ) )
@@ -255,19 +314,27 @@ impl HttpClient {
255314 ) -> Result < R > {
256315 self . authenticate ( & mut request) . await ?;
257316
317+ let method = request. method ( ) . clone ( ) ;
318+ let url = request. url ( ) . clone ( ) ;
319+
258320 let resp = self . client . execute ( request) . await ?;
259321
260322 if let Some ( ret) = handler ( & resp) {
261323 Ok ( ret)
262324 } else {
263325 let code = resp. status ( ) ;
264- let text = resp. bytes ( ) . await ?;
326+ let text = resp
327+ . bytes ( )
328+ . await
329+ . map_err ( |err| err. with_url ( url. clone ( ) ) ) ?;
265330 let e = serde_json:: from_slice :: < E > ( & text) . map_err ( |e| {
266331 Error :: new (
267332 ErrorKind :: Unexpected ,
268333 "Failed to parse response from rest catalog server!" ,
269334 )
270335 . with_context ( "code" , code. to_string ( ) )
336+ . with_context ( "method" , method. to_string ( ) )
337+ . with_context ( "url" , url. to_string ( ) )
271338 . with_context ( "json" , String :: from_utf8_lossy ( & text) )
272339 . with_source ( e)
273340 } ) ?;
0 commit comments