3838import base64
3939import copy
4040import json
41+ import logging
4142from functools import partial
42- from vsc .utils import fancylogger
4343from urllib .parse import urlencode
4444from urllib .request import Request , HTTPSHandler , build_opener
4545
@@ -172,18 +172,19 @@ def request(self, method, url, body, headers, content_type=None):
172172 secret_items = ['Authorization' , 'X-Auth-Token' ]
173173 headers_censored = self .censor_request (secret_items , headers )
174174
175- if body and not isinstance (body , str ):
176- # censor contents of body to avoid leaking passwords
177- secret_items = ['password' ]
178- body_censored = self .censor_request (secret_items , body )
179- # serialize body in all cases
180- body = json .dumps (body )
181- else :
182- # assume serialized bodies are already clear of secrets
183- fancylogger .getLogger ().debug ("Request with pre-serialized body, will not censor secrets" )
184- body_censored = body
175+ body_censored = body
176+ if body is not None :
177+ if isinstance (body , str ):
178+ # assume serialized bodies are already clear of secrets
179+ logging .debug ("Request with pre-serialized body, will not censor secrets" )
180+ else :
181+ # censor contents of body to avoid leaking passwords
182+ secret_items = ['password' ]
183+ body_censored = self .censor_request (secret_items , body )
184+ # serialize body in all cases
185+ body = json .dumps (body )
185186
186- fancylogger . getLogger () .debug ('cli request: %s, %s, %s, %s' , method , url , body_censored , headers_censored )
187+ logging .debug ('cli request: %s, %s, %s, %s' , method , url , body_censored , headers_censored )
187188
188189 # TODO: in recent python: Context manager
189190 conn = self .get_connection (method , url , body , headers )
@@ -197,7 +198,7 @@ def request(self, method, url, body, headers, content_type=None):
197198 pybody = json .loads (body )
198199 except ValueError :
199200 pybody = body
200- fancylogger . getLogger () .debug ('reponse len: %s ' , len (pybody ))
201+ logging .debug ('reponse len: %s ' , len (pybody ))
201202 conn .close ()
202203 return status , pybody
203204
@@ -241,13 +242,13 @@ def get_connection(self, method, url, body, headers):
241242 sep = '/'
242243 else :
243244 sep = ''
244- if body :
245+ if body is not None :
245246 body = body .encode ()
246247 request = Request (self .url + sep + url , data = body )
247248 for header , value in headers .items ():
248249 request .add_header (header , value )
249250 request .get_method = lambda : method
250- fancylogger . getLogger () .debug ('opening request: %s%s%s' , self .url , sep , url )
251+ logging .debug ('opening request: %s%s%s' , self .url , sep , url )
251252 connection = self .opener .open (request )
252253 return connection
253254
0 commit comments