Move safe_body() into specific class

safe_body() is used in RestClient class only, so this patch
moves safe_body() into the class for the code cleanup.

Change-Id: Ice80d5ab19438162ba7a5705fa78c1ab91c1ccd5
This commit is contained in:
Ken'ichi Ohmichi 2014-12-10 05:31:16 +00:00
parent 53c963a916
commit e9140bfb08

View File

@ -38,19 +38,6 @@ MAX_RECURSION_DEPTH = 2
HTTP_SUCCESS = (200, 201, 202, 203, 204, 205, 206, 207) HTTP_SUCCESS = (200, 201, 202, 203, 204, 205, 206, 207)
# convert a structure into a string safely
def safe_body(body, maxlen=4096):
try:
text = six.text_type(body)
except UnicodeDecodeError:
# if this isn't actually text, return marker that
return "<BinaryData: removed>"
if len(text) > maxlen:
return text[:maxlen]
else:
return text
class ResponseBody(dict): class ResponseBody(dict):
"""Class that wraps an http response and dict body into a single value. """Class that wraps an http response and dict body into a single value.
@ -295,6 +282,18 @@ class RestClient(object):
return resp[i] return resp[i]
return "" return ""
def _safe_body(self, body, maxlen=4096):
# convert a structure into a string safely
try:
text = six.text_type(body)
except UnicodeDecodeError:
# if this isn't actually text, return marker that
return "<BinaryData: removed>"
if len(text) > maxlen:
return text[:maxlen]
else:
return text
def _log_request_start(self, method, req_url, req_headers=None, def _log_request_start(self, method, req_url, req_headers=None,
req_body=None): req_body=None):
if req_headers is None: if req_headers is None:
@ -325,9 +324,9 @@ class RestClient(object):
req_url, req_url,
secs, secs,
str(req_headers), str(req_headers),
safe_body(req_body), self._safe_body(req_body),
str(resp), str(resp),
safe_body(resp_body)), self._safe_body(resp_body)),
extra=extra) extra=extra)
def _log_request(self, method, req_url, resp, def _log_request(self, method, req_url, resp,