Fix python / tornado body handling (#7738)

* Handle empty body

* Update petstore
This commit is contained in:
Andy Kipp 2018-02-28 00:08:17 -08:00 committed by William Cheng
parent 91da14577a
commit 8e0a0ebd62
2 changed files with 26 additions and 16 deletions

View File

@ -20,11 +20,19 @@ logger = logging.getLogger(__name__)
class RESTResponse(io.IOBase): class RESTResponse(io.IOBase):
def __init__(self, resp, data): def __init__(self, resp):
self.tornado_response = resp self.tornado_response = resp
self.status = resp.code self.status = resp.code
self.reason = resp.reason self.reason = resp.reason
self.data = data
if resp.body:
# In Python 3, the response body is utf-8 encoded bytes.
if six.PY3:
self.data = resp.body.decode('utf-8')
else:
self.data = resp.body
else:
self.data = None
def getheaders(self): def getheaders(self):
"""Returns a CIMultiDictProxy of the response headers.""" """Returns a CIMultiDictProxy of the response headers."""
@ -83,7 +91,8 @@ class RESTClientObject(object):
"body parameter cannot be used with post_params parameter." "body parameter cannot be used with post_params parameter."
) )
request = httpclient.HTTPRequest(url, allow_nonstandard_methods=True) request = httpclient.HTTPRequest(url)
request.allow_nonstandard_methods = True
request.ca_certs = self.ca_certs request.ca_certs = self.ca_certs
request.client_key = self.client_key request.client_key = self.client_key
request.client_cert = self.client_cert request.client_cert = self.client_cert
@ -128,11 +137,7 @@ class RESTClientObject(object):
if _preload_content: if _preload_content:
r = RESTResponse(r, r.body) r = RESTResponse(r)
# In the python 3, the response.data is bytes.
# we need to decode it to string.
if six.PY3:
r.data = r.data.decode('utf8')
# log response body # log response body
logger.debug("response body: %s", r.data) logger.debug("response body: %s", r.data)

View File

@ -29,11 +29,19 @@ logger = logging.getLogger(__name__)
class RESTResponse(io.IOBase): class RESTResponse(io.IOBase):
def __init__(self, resp, data): def __init__(self, resp):
self.tornado_response = resp self.tornado_response = resp
self.status = resp.code self.status = resp.code
self.reason = resp.reason self.reason = resp.reason
self.data = data
if resp.body:
# In Python 3, the response body is utf-8 encoded bytes.
if six.PY3:
self.data = resp.body.decode('utf-8')
else:
self.data = resp.body
else:
self.data = None
def getheaders(self): def getheaders(self):
"""Returns a CIMultiDictProxy of the response headers.""" """Returns a CIMultiDictProxy of the response headers."""
@ -92,7 +100,8 @@ class RESTClientObject(object):
"body parameter cannot be used with post_params parameter." "body parameter cannot be used with post_params parameter."
) )
request = httpclient.HTTPRequest(url, allow_nonstandard_methods=True) request = httpclient.HTTPRequest(url)
request.allow_nonstandard_methods = True
request.ca_certs = self.ca_certs request.ca_certs = self.ca_certs
request.client_key = self.client_key request.client_key = self.client_key
request.client_cert = self.client_cert request.client_cert = self.client_cert
@ -137,11 +146,7 @@ class RESTClientObject(object):
if _preload_content: if _preload_content:
r = RESTResponse(r, r.body) r = RESTResponse(r)
# In the python 3, the response.data is bytes.
# we need to decode it to string.
if six.PY3:
r.data = r.data.decode('utf8')
# log response body # log response body
logger.debug("response body: %s", r.data) logger.debug("response body: %s", r.data)