Make the flask adapter working with flask.ext.restful

Change-Id: I71d875f6579799b2881f76859fa88226dfea9b2b
This commit is contained in:
Christophe de Vienne 2014-10-03 10:27:59 +02:00
parent c8337025e9
commit 292c556808
4 changed files with 47 additions and 0 deletions

View File

@ -1,5 +1,8 @@
# encoding=utf8
import unittest import unittest
from flask import Flask, json, abort from flask import Flask, json, abort
from flask.ext import restful
from wsmeext.flask import signature from wsmeext.flask import signature
from wsme.api import Response from wsme.api import Response
from wsme.types import Base, text from wsme.types import Base, text
@ -16,6 +19,7 @@ class Criterion(Base):
value = text value = text
test_app = Flask(__name__) test_app = Flask(__name__)
api = restful.Api(test_app)
@test_app.route('/multiply') @test_app.route('/multiply')
@ -78,6 +82,18 @@ def get_status_response():
return Response(1, status_code=201) return Response(1, status_code=201)
class RestFullApi(restful.Resource):
@signature(Model)
def get(self):
return Model(id=1, name=u"Gérard")
@signature(int, body=Model)
def post(self, model):
return model.id
api.add_resource(RestFullApi, '/restful')
class FlaskrTestCase(unittest.TestCase): class FlaskrTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
@ -170,5 +186,27 @@ class FlaskrTestCase(unittest.TestCase):
"faultstring": "integer division or modulo by zero"} "faultstring": "integer division or modulo by zero"}
) )
def test_restful_get(self):
r = self.app.get('/restful', headers={'Accept': 'application/json'})
self.assertEqual(r.status_code, 200)
data = json.loads(r.data)
self.assertEqual(data['id'], 1)
self.assertEqual(data['name'], u"Gérard")
def test_restful_post(self):
r = self.app.post(
'/restful',
data=json.dumps({'id': 5, 'name': u'Huguette'}),
headers={
'Accept': 'application/json',
'Content-Type': 'application/json'})
self.assertEqual(r.status_code, 200)
data = json.loads(r.data)
self.assertEqual(data, 5)
if __name__ == '__main__': if __name__ == '__main__':
test_app.run() test_app.run()

View File

@ -14,6 +14,7 @@ basedeps=
cloud_sptheme cloud_sptheme
Sphinx < 1.2.99 Sphinx < 1.2.99
Flask Flask
flask-restful
[axes] [axes]
python=py26,py27,py32,py33,pypy python=py26,py27,py32,py33,pypy

View File

@ -13,6 +13,7 @@ basedeps =
cloud_sptheme cloud_sptheme
Sphinx < 1.2.99 Sphinx < 1.2.99
Flask Flask
flask-restful
[testenv] [testenv]
setenv = setenv =

View File

@ -3,6 +3,7 @@ from __future__ import absolute_import
import functools import functools
import logging import logging
import sys import sys
import inspect
import wsme import wsme
import wsme.api import wsme.api
@ -43,12 +44,16 @@ def signature(*args, **kw):
sig = wsme.signature(*args, **kw) sig = wsme.signature(*args, **kw)
def decorator(f): def decorator(f):
args = inspect.getargspec(f)[0]
ismethod = args and args[0] == 'self'
sig(f) sig(f)
funcdef = wsme.api.FunctionDefinition.get(f) funcdef = wsme.api.FunctionDefinition.get(f)
funcdef.resolve_types(wsme.types.registry) funcdef.resolve_types(wsme.types.registry)
@functools.wraps(f) @functools.wraps(f)
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
if ismethod:
self, args = args[0], args[1:]
args, kwargs = wsme.rest.args.get_args( args, kwargs = wsme.rest.args.get_args(
funcdef, args, kwargs, funcdef, args, kwargs,
flask.request.args, flask.request.form, flask.request.args, flask.request.form,
@ -62,6 +67,8 @@ def signature(*args, **kw):
dataformat = get_dataformat() dataformat = get_dataformat()
try: try:
if ismethod:
args = [self] + list(args)
result = f(*args, **kwargs) result = f(*args, **kwargs)
# NOTE: Support setting of status_code with default 20 # NOTE: Support setting of status_code with default 20