the rest+json protocol starts to work with basic return types
This commit is contained in:
@@ -4,6 +4,7 @@ __all__ = ['expose', 'validate', 'WSRoot']
|
||||
|
||||
registered_protocols = {}
|
||||
|
||||
|
||||
def scan_api(controller, path=[]):
|
||||
for name in dir(controller):
|
||||
if name.startswith('_'):
|
||||
@@ -29,7 +30,7 @@ class FunctionDefinition(object):
|
||||
self.name = name
|
||||
self.return_type = None
|
||||
self.arguments = []
|
||||
|
||||
|
||||
@classmethod
|
||||
def get(cls, func):
|
||||
fd = getattr(func, '_wsme_definition', None)
|
||||
@@ -69,7 +70,7 @@ class validate(object):
|
||||
mandatory = defaults is None or i <= len(defaults)
|
||||
default = None
|
||||
if not mandatory:
|
||||
default = defaults[i-(len(args)-len(defaults))]
|
||||
default = defaults[i - (len(args) - len(defaults))]
|
||||
print argname, datatype, mandatory, default
|
||||
fd.arguments.append(FunctionArgument(argname, datatype,
|
||||
mandatory, default))
|
||||
|
||||
19
wsme/rest.py
19
wsme/rest.py
@@ -1,3 +1,5 @@
|
||||
import webob
|
||||
|
||||
class RestProtocol(object):
|
||||
name = None
|
||||
dataformat = None
|
||||
@@ -7,14 +9,23 @@ class RestProtocol(object):
|
||||
if request.path.endswith('.' + self.dataformat):
|
||||
return True
|
||||
return request.headers.get('Content-Type') in self.content_types
|
||||
|
||||
|
||||
def handle(self, root, request):
|
||||
path = request.path.split('/')
|
||||
path = request.path.strip('/').split('/')
|
||||
a = root
|
||||
for name in path:
|
||||
a = getattr(a, name)
|
||||
if not hasattr(a, '_ews_description'):
|
||||
|
||||
if not hasattr(a, '_wsme_definition'):
|
||||
raise ValueError('Invalid path')
|
||||
fonc = a
|
||||
kw = self.get_args(req)
|
||||
|
||||
kw = self.get_args(request)
|
||||
|
||||
res = webob.Response()
|
||||
res.headers['Content-Type'] = 'application/json'
|
||||
res.status = "200 OK"
|
||||
|
||||
res.body = self.encode_response(a(**kw))
|
||||
|
||||
return res
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
from wsme.rest import RestProtocol
|
||||
from wsme.controller import register_protocol
|
||||
|
||||
try:
|
||||
import simplejson as json
|
||||
except ImportError:
|
||||
import json
|
||||
|
||||
class RestJsonProtocol(RestProtocol):
|
||||
name = 'REST+Json'
|
||||
dataformat = 'json'
|
||||
@@ -8,6 +13,9 @@ class RestJsonProtocol(RestProtocol):
|
||||
|
||||
def get_args(self, req):
|
||||
kw = json.loads(req.body)
|
||||
return kw
|
||||
|
||||
def encode_response(self, response):
|
||||
return json.dumps(response)
|
||||
|
||||
register_protocol(RestJsonProtocol)
|
||||
|
||||
@@ -1,10 +1,54 @@
|
||||
# coding=utf-8
|
||||
|
||||
import unittest
|
||||
import warnings
|
||||
import datetime
|
||||
import decimal
|
||||
|
||||
from webob.dec import wsgify
|
||||
from webtest import TestApp
|
||||
|
||||
from wsme import *
|
||||
|
||||
warnings.filterwarnings('ignore', module='webob.dec')
|
||||
|
||||
|
||||
class ReturnTypes(object):
|
||||
@expose(str)
|
||||
def getstr(self):
|
||||
return "astring"
|
||||
|
||||
@expose(unicode)
|
||||
def getunicode(self):
|
||||
return u"の"
|
||||
|
||||
@expose(int)
|
||||
def getint(self):
|
||||
return 2
|
||||
|
||||
@expose(float)
|
||||
def getfloat(self):
|
||||
return 3.14159265
|
||||
|
||||
@expose(decimal.Decimal)
|
||||
def getdecimal(self):
|
||||
return decimal.Decimal('3.14159265')
|
||||
|
||||
@expose(datetime.date)
|
||||
def getdate(self):
|
||||
return datetime.date(1994, 1, 26)
|
||||
|
||||
@expose(datetime.time)
|
||||
def getdate(self):
|
||||
return datetime.time(12, 0, 0)
|
||||
|
||||
@expose(datetime.datetime)
|
||||
def getdate(self):
|
||||
return datetime.datetime(1994, 1, 26, 12, 0, 0)
|
||||
|
||||
class WSTestRoot(WSRoot):
|
||||
returntypes = ReturnTypes()
|
||||
|
||||
def reset(self):
|
||||
self.touched = False
|
||||
|
||||
@@ -12,11 +56,13 @@ class WSTestRoot(WSRoot):
|
||||
def touch(self):
|
||||
self.touched = True
|
||||
|
||||
class TestProtocol(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.root = WSTestRoot([self.protocol])
|
||||
|
||||
self.app = TestApp(wsgify(self.root._handle_request))
|
||||
class ProtocolTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
if self.__class__.__name__ != 'ProtocolTestCase':
|
||||
self.root = WSTestRoot([self.protocol])
|
||||
|
||||
self.app = TestApp(wsgify(self.root._handle_request))
|
||||
|
||||
def _call(self, fpath, **kw):
|
||||
pass
|
||||
@@ -24,3 +70,19 @@ class TestProtocol(unittest.TestCase):
|
||||
def test_touch(self):
|
||||
assert self.call('touch') is None
|
||||
|
||||
def test_return_str(self):
|
||||
r = self.call('returntypes/getstr')
|
||||
assert r == 'astring', r
|
||||
|
||||
def test_return_unicode(self):
|
||||
r = self.call('returntypes/getunicode')
|
||||
assert r == u'の', r
|
||||
|
||||
def test_return_int(self):
|
||||
r = self.call('returntypes/getint')
|
||||
assert r == 2, r
|
||||
|
||||
def test_return_float(self):
|
||||
r = self.call('returntypes/getfloat')
|
||||
assert r == 3.14159265, r
|
||||
|
||||
|
||||
@@ -1,14 +1,20 @@
|
||||
from wsme.tests.protocol import TestProtocol
|
||||
import json
|
||||
import wsme.tests.protocol
|
||||
try:
|
||||
import simplejson as json
|
||||
except:
|
||||
import json
|
||||
|
||||
class TestRestJson(TestProtocol):
|
||||
|
||||
class TestRestJson(wsme.tests.protocol.ProtocolTestCase):
|
||||
protocol = 'REST+Json'
|
||||
|
||||
def call(self, fpath, **kw):
|
||||
content = json.dumps(kw)
|
||||
res = self.app.post(
|
||||
'/' + fpath,
|
||||
content,
|
||||
headers={
|
||||
'Content-Type': 'application/json'
|
||||
'Content-Type': 'application/json',
|
||||
})
|
||||
return json.loads(res.body)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user