Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ class ApiClient(object):
_request_timeout=_request_timeout)

self.last_response = response_data
{{^tornado}}

return_data = response_data
if _preload_content:
Expand All @@ -160,12 +159,20 @@ class ApiClient(object):
else:
return_data = None

{{^tornado}}
if _return_http_data_only:
return (return_data)
else:
return (return_data, response_data.status,
response_data.getheaders())
{{/tornado}}
{{#tornado}}
if _return_http_data_only:
raise tornado.gen.Return(return_data)
else:
raise tornado.gen.Return((return_data, response_data.status,
response_data.getheaders()))
{{/tornado}}

def sanitize_for_serialization(self, obj):
"""Builds a JSON POST object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import ssl

import certifi
# python 2 and python 3 compatibility library
import six
from six.moves.urllib.parse import urlencode
import tornado
import tornado.gen
Expand Down Expand Up @@ -50,7 +51,7 @@ class RESTClientObject(object):
# if not set certificate file, use Mozilla's root certificates.
ca_certs = certifi.where()

self.ssl_context = ssl.SSLContext()
self.ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
self.ssl_context.load_verify_locations(cafile=ca_certs)
if configuration.cert_file:
self.ssl_context.load_cert_chain(
Expand Down Expand Up @@ -121,7 +122,8 @@ class RESTClientObject(object):
elif headers['Content-Type'] == 'application/x-www-form-urlencoded': # noqa: E501
request.body = urlencode(post_params)
elif headers['Content-Type'] == 'multipart/form-data':
request.body = encode_multipart_formdata(post_params)
multipart = encode_multipart_formdata(post_params)
request.body, headers['Content-Type'] = multipart
# Pass a `bytes` parameter directly in the body to support
# other content types than Json when `body` argument is provided
# in serialized form
Expand All @@ -134,15 +136,24 @@ class RESTClientObject(object):
declared content type."""
raise ApiException(status=0, reason=msg)

r = yield self.pool_manager.fetch(request)
r = RESTResponse(r, r.body)
r = yield self.pool_manager.fetch(request, raise_error=False)

# log response body
logger.debug("response body: %s", r.data)
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')

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

if not 200 <= r.status <= 299:
raise ApiException(http_resp=r)

raise tornado.gen.Return(r)

@tornado.gen.coroutine
def GET(self, url, headers=None, query_params=None, _preload_content=True,
_request_timeout=None):
Expand Down
5 changes: 5 additions & 0 deletions samples/client/petstore/python-tornado/dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
nose
tox
coverage
randomize
flake8
14 changes: 14 additions & 0 deletions samples/client/petstore/python-tornado/petstore_api/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,20 @@ def __call_api(

self.last_response = response_data

return_data = response_data
if _preload_content:
# deserialize response data
if response_type:
return_data = self.deserialize(response_data, response_type)
else:
return_data = None

if _return_http_data_only:
raise tornado.gen.Return(return_data)
else:
raise tornado.gen.Return((return_data, response_data.status,
response_data.getheaders()))

def sanitize_for_serialization(self, obj):
"""Builds a JSON POST object.

Expand Down
23 changes: 17 additions & 6 deletions samples/client/petstore/python-tornado/petstore_api/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import certifi
# python 2 and python 3 compatibility library
import six
from six.moves.urllib.parse import urlencode
import tornado
import tornado.gen
Expand Down Expand Up @@ -59,7 +60,7 @@ def __init__(self, configuration, pools_size=4, maxsize=4):
# if not set certificate file, use Mozilla's root certificates.
ca_certs = certifi.where()

self.ssl_context = ssl.SSLContext()
self.ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
self.ssl_context.load_verify_locations(cafile=ca_certs)
if configuration.cert_file:
self.ssl_context.load_cert_chain(
Expand Down Expand Up @@ -130,7 +131,8 @@ def request(self, method, url, query_params=None, headers=None, body=None,
elif headers['Content-Type'] == 'application/x-www-form-urlencoded': # noqa: E501
request.body = urlencode(post_params)
elif headers['Content-Type'] == 'multipart/form-data':
request.body = encode_multipart_formdata(post_params)
multipart = encode_multipart_formdata(post_params)
request.body, headers['Content-Type'] = multipart
# Pass a `bytes` parameter directly in the body to support
# other content types than Json when `body` argument is provided
# in serialized form
Expand All @@ -143,15 +145,24 @@ def request(self, method, url, query_params=None, headers=None, body=None,
declared content type."""
raise ApiException(status=0, reason=msg)

r = yield self.pool_manager.fetch(request)
r = RESTResponse(r, r.body)
r = yield self.pool_manager.fetch(request, raise_error=False)

# log response body
logger.debug("response body: %s", r.data)
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')

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

if not 200 <= r.status <= 299:
raise ApiException(http_resp=r)

raise tornado.gen.Return(r)

@tornado.gen.coroutine
def GET(self, url, headers=None, query_params=None, _preload_content=True,
_request_timeout=None):
Expand Down
32 changes: 32 additions & 0 deletions samples/client/petstore/python-tornado/test_python2_and_3.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash

REQUIREMENTS_FILE=dev-requirements.txt
REQUIREMENTS_OUT=dev-requirements.txt.log
SETUP_OUT=*.egg-info
VENV=.venv
DEACTIVE=false

export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8

### set virtualenv
if [ -z "$VIRTUAL_ENV" ]; then
virtualenv $VENV --no-site-packages --always-copy
source $VENV/bin/activate
DEACTIVE=true
fi

### install dependencies
pip install -r $REQUIREMENTS_FILE | tee -a $REQUIREMENTS_OUT
python setup.py develop

### run tests
tox

### static analysis of code
flake8 --show-source petstore_api/

### deactivate virtualenv
#if [ $DEACTIVE == true ]; then
# deactivate
#fi
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
Loading