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
from flask import Flask, json, abort
from flask.ext import restful
from wsmeext.flask import signature
from wsme.api import Response
from wsme.types import Base, text
@ -16,6 +19,7 @@ class Criterion(Base):
value = text
test_app = Flask(__name__)
api = restful.Api(test_app)
@test_app.route('/multiply')
@ -78,6 +82,18 @@ def get_status_response():
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):
def setUp(self):
@ -170,5 +186,27 @@ class FlaskrTestCase(unittest.TestCase):
"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__':
test_app.run()

View File

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

View File

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

View File

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