Skip to content

Commit

Permalink
Fix python / tornado body handling (#7738)
Browse files Browse the repository at this point in the history
* Handle empty body

* Update petstore
  • Loading branch information
kippandrew authored and wing328 committed Feb 28, 2018
1 parent 91da145 commit 8e0a0eb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,19 @@ logger = logging.getLogger(__name__)

class RESTResponse(io.IOBase):

def __init__(self, resp, data):
def __init__(self, resp):
self.tornado_response = resp
self.status = resp.code
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):
"""Returns a CIMultiDictProxy of the response headers."""
Expand Down Expand Up @@ -83,7 +91,8 @@ class RESTClientObject(object):
"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.client_key = self.client_key
request.client_cert = self.client_cert
Expand Down Expand Up @@ -128,11 +137,7 @@ class RESTClientObject(object):

if _preload_content:

r = RESTResponse(r, r.body)
# 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')
r = RESTResponse(r)

# log response body
logger.debug("response body: %s", r.data)
Expand Down
21 changes: 13 additions & 8 deletions samples/client/petstore/python-tornado/petstore_api/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,19 @@

class RESTResponse(io.IOBase):

def __init__(self, resp, data):
def __init__(self, resp):
self.tornado_response = resp
self.status = resp.code
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):
"""Returns a CIMultiDictProxy of the response headers."""
Expand Down Expand Up @@ -92,7 +100,8 @@ def request(self, method, url, query_params=None, headers=None, body=None,
"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.client_key = self.client_key
request.client_cert = self.client_cert
Expand Down Expand Up @@ -137,11 +146,7 @@ def request(self, method, url, query_params=None, headers=None, body=None,

if _preload_content:

r = RESTResponse(r, r.body)
# 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')
r = RESTResponse(r)

# log response body
logger.debug("response body: %s", r.data)
Expand Down

0 comments on commit 8e0a0eb

Please sign in to comment.