Skip to content

Commit 6ac23ef

Browse files
committed
handle e.g. empty dict body data properly
1 parent 1ea3c61 commit 6ac23ef

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

lib/vsc/utils/rest.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
import base64
3939
import copy
4040
import json
41+
import logging
4142
from functools import partial
42-
from vsc.utils import fancylogger
4343
from urllib.parse import urlencode
4444
from 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

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
from vsc.install.shared_setup import ag, kh, jt, sdw
3838

3939
PACKAGE = {
40-
'version': '3.5.2',
40+
'version': '3.5.3',
4141
'author': [sdw, jt, ag, kh],
4242
'maintainer': [sdw, jt, ag, kh],
4343
'install_requires': [

0 commit comments

Comments
 (0)