Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added --use-async option: generate async handlers (tornado) #142

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion swagger_py_codegen/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,11 @@ def print_version(ctx, param, value):
@click.option('--version', is_flag=True, callback=print_version,
expose_value=False, is_eager=True,
help='Show current version.')
@click.option('-a', '--use-async', default=False, is_flag=True,
help='Generate async request handlers (tornado)')
def generate(destination, swagger_doc, force=False, package=None,
template_dir=None, templates='flask',
specification=False, ui=False, validate=False):
specification=False, ui=False, validate=False, use_async=False):
package = package or destination.replace('-', '_')
data = spec_load(swagger_doc)
if validate:
Expand Down Expand Up @@ -198,6 +200,7 @@ def generate(destination, swagger_doc, force=False, package=None,
click.secho('%-12s%s' % (status, ui_dest))

for code in generator.generate():
code.data['use_async'] = use_async
source = template.render_code(code)
dest = join(destination, code.dest(env))
dest_exists = exists(dest)
Expand Down
19 changes: 17 additions & 2 deletions swagger_py_codegen/templates/tornado/validators.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ class ValidatorAdaptor(object):
def request_validate(obj):
def _request_validate(view):
@wraps(view)
{% if not use_async -%}
def wrapper(*args, **kwargs):
{%- else -%}
async def wrapper(*args, **kwargs):
{%- endif %}
request = obj.request
endpoint = obj.endpoint
user_info = obj.current_user
Expand All @@ -88,7 +92,8 @@ def request_validate(obj):
if location == 'json':
value = getattr(request, 'body', MultiDict())
elif location == 'args':
value = getattr(request, 'query_arguments', MultiDict())
value = {key: list(map(obj.decode_argument, value)) for key, value in
request.query_arguments.items()}
for k,v in six.iteritems(value):
if isinstance(v, list) and len(v) == 1:
value[k] = v[0]
Expand All @@ -101,16 +106,25 @@ def request_validate(obj):
raise tornado.web.HTTPError(422, message='Unprocessable Entity',
reason=json.dumps(reasons))
setattr(obj, location, result)
{% if not use_async -%}
return view(*args, **kwargs)
{%- else -%}
return await view(*args, **kwargs)
{%- endif %}
return wrapper
return _request_validate


def response_filter(obj):
def _response_filter(view):
@wraps(view)
{% if not use_async -%}
def wrapper(*args, **kwargs):
resp = view(*args, **kwargs)
{%- else -%}
async def wrapper(*args, **kwargs):
resp = await view(*args, **kwargs)
{%- endif %}
request = obj.request
endpoint = obj.endpoint
method = request.method
Expand Down Expand Up @@ -145,7 +159,8 @@ def response_filter(obj):
reason=json.dumps(errors))
obj.set_status(status)
obj.set_headers(headers)
obj.write(json.dumps(resp))
if resp:
obj.write(json.dumps(resp))
return
return wrapper
return _response_filter
Expand Down
4 changes: 4 additions & 0 deletions swagger_py_codegen/templates/tornado/view.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ class {{ name }}(ApiHandler):

{%- for method, ins in methods.items() %}

{% if use_async -%}
async def {{ method.lower() }}(self{{ params.__len__() and ', ' or '' }}{{ params | join(', ') }}):
{%- else -%}
def {{ method.lower() }}(self{{ params.__len__() and ', ' or '' }}{{ params | join(', ') }}):
{%- endif %}
{%- for request in ins.requests %}
print(self.{{ request }})
{%- endfor %}
Expand Down