New flask adapter + test (run "tox -e flask" to test)
This commit is contained in:
parent
6a062fa5af
commit
737b794e07
28
tests/test_flask.py
Normal file
28
tests/test_flask.py
Normal file
@ -0,0 +1,28 @@
|
||||
import unittest
|
||||
from flask import Flask
|
||||
from wsmeext.flask import signature
|
||||
|
||||
test_app = Flask(__name__)
|
||||
|
||||
|
||||
@test_app.route('/multiply')
|
||||
@signature(int, int, int)
|
||||
def multiply(a, b):
|
||||
return a * b
|
||||
|
||||
|
||||
class FlaskrTestCase(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
test_app.config['TESTING'] = True
|
||||
self.app = test_app.test_client()
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
def test_multiply(self):
|
||||
r = self.app.get('/multiply?a=2&b=5')
|
||||
assert r.data == '10'
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_app.run()
|
14
tox-tmpl.ini
14
tox-tmpl.ini
@ -164,6 +164,20 @@ commands=
|
||||
{envbindir}/nosetests -w tests/pecantest test/tests/test_ws.py --with-xunit --xunit-file nosetests-{envname}.xml --verbose --with-coverage --cover-package wsme,wsmeext {posargs}
|
||||
{envbindir}/coverage xml -o coverage-{envname}.xml wsme/*.py wsme/rest/*.py wsmeext/*.py
|
||||
|
||||
[testenv:flask]
|
||||
basepython=python2.7
|
||||
deps=
|
||||
d2to1
|
||||
nose
|
||||
webtest
|
||||
coverage
|
||||
flask
|
||||
ipdb
|
||||
commands=
|
||||
{envbindir}/nosetests tests/test_flask.py --with-xunit --xunit-file nosetests-{envname}.xml --verbose --with-coverage --cover-package wsme {posargs}
|
||||
{envbindir}/coverage xml -o coverage-{envname}.xml wsme/*.py wsmeext/flask.py
|
||||
|
||||
|
||||
[testenv:coverage]
|
||||
basepython=python
|
||||
deps=
|
||||
|
13
tox.ini
13
tox.ini
@ -85,6 +85,19 @@ commands =
|
||||
{envbindir}/nosetests -w tests/pecantest test/tests/test_ws.py --with-xunit --xunit-file nosetests-{envname}.xml --verbose --with-coverage --cover-package wsme,wsmeext {posargs}
|
||||
{envbindir}/coverage xml -o coverage-{envname}.xml wsme/*.py wsme/rest/*.py wsmeext/*.py
|
||||
|
||||
[testenv:flask]
|
||||
basepython = python2.7
|
||||
deps =
|
||||
d2to1
|
||||
nose
|
||||
webtest
|
||||
coverage
|
||||
flask
|
||||
ipdb
|
||||
commands =
|
||||
{envbindir}/nosetests tests/test_flask.py --with-xunit --xunit-file nosetests-{envname}.xml --verbose --with-coverage --cover-package wsme {posargs}
|
||||
{envbindir}/coverage xml -o coverage-{envname}.xml wsme/*.py wsmeext/flask.py
|
||||
|
||||
[testenv:coverage]
|
||||
basepython = python
|
||||
deps =
|
||||
|
34
wsmeext/flask.py
Normal file
34
wsmeext/flask.py
Normal file
@ -0,0 +1,34 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
import functools
|
||||
import wsme
|
||||
import wsme.api
|
||||
from wsme.rest.args import (
|
||||
args_from_params, args_from_body, combine_args
|
||||
)
|
||||
|
||||
import flask
|
||||
|
||||
|
||||
def signature(*args, **kw):
|
||||
sig = wsme.signature(*args, **kw)
|
||||
|
||||
def decorator(f):
|
||||
sig(f)
|
||||
funcdef = wsme.api.FunctionDefinition.get(f)
|
||||
|
||||
@functools.wraps(f)
|
||||
def wrapper(*args, **kw):
|
||||
import ipdb
|
||||
ipdb.set_trace()
|
||||
args, kwargs = combine_args(
|
||||
funcdef,
|
||||
args_from_params(funcdef, flask.request.args),
|
||||
args_from_body(funcdef, flask.request.data, flask.request.content_type)
|
||||
)
|
||||
resp = make_response(f(*args, **kw))
|
||||
return resp
|
||||
|
||||
wrapper.wsme_func = f
|
||||
return wrapper
|
||||
return decorator
|
Loading…
x
Reference in New Issue
Block a user