Start working on a better tg 1.1 integration. Need to rework the rest implementation to make it easier (especially the body parsing)
This commit is contained in:
parent
3080528319
commit
4826c8b7ed
@ -1,5 +1,6 @@
|
|||||||
import wsme.tg1
|
import wsme.tg1
|
||||||
from wsme import expose, validate, WSRoot
|
from wsme import WSRoot
|
||||||
|
from wsme.tg1 import wsexpose, wsvalidate
|
||||||
|
|
||||||
from turbogears.controllers import RootController
|
from turbogears.controllers import RootController
|
||||||
|
|
||||||
@ -9,16 +10,18 @@ import simplejson
|
|||||||
|
|
||||||
|
|
||||||
class WSController(WSRoot):
|
class WSController(WSRoot):
|
||||||
@expose(int)
|
pass
|
||||||
@validate(int, int)
|
|
||||||
def multiply(self, a, b):
|
|
||||||
return a * b
|
|
||||||
|
|
||||||
|
|
||||||
class Root(RootController):
|
class Root(RootController):
|
||||||
ws = wsme.tg1.adapt(
|
ws = wsme.tg1.adapt(
|
||||||
WSController(webpath='/ws', protocols=['restjson']))
|
WSController(webpath='/ws', protocols=['restjson']))
|
||||||
|
|
||||||
|
@wsexpose(int)
|
||||||
|
@wsvalidate(int, int)
|
||||||
|
def multiply(self, a, b):
|
||||||
|
return a * b
|
||||||
|
|
||||||
|
|
||||||
import cherrypy
|
import cherrypy
|
||||||
|
|
||||||
@ -48,7 +51,7 @@ class TestController(unittest.TestCase):
|
|||||||
config.update({"server_started": False})
|
config.update({"server_started": False})
|
||||||
|
|
||||||
def test_simplecall(self):
|
def test_simplecall(self):
|
||||||
response = self.app.post("/ws/multiply",
|
response = self.app.post("/multiply",
|
||||||
simplejson.dumps({'a': 5, 'b': 10}),
|
simplejson.dumps({'a': 5, 'b': 10}),
|
||||||
{'Content-Type': 'application/json'})
|
{'Content-Type': 'application/json'})
|
||||||
print response
|
print response
|
||||||
|
79
wsme/tg1.py
79
wsme/tg1.py
@ -1,9 +1,88 @@
|
|||||||
|
try:
|
||||||
|
import json
|
||||||
|
except ImportError:
|
||||||
|
import simplejson as json # noqa
|
||||||
|
|
||||||
|
import functools
|
||||||
|
|
||||||
import cherrypy
|
import cherrypy
|
||||||
from cherrypy.filters.basefilter import BaseFilter
|
from cherrypy.filters.basefilter import BaseFilter
|
||||||
import webob
|
import webob
|
||||||
from turbogears import expose, config
|
from turbogears import expose, config
|
||||||
from turbogears.startup import call_on_startup, call_on_shutdown
|
from turbogears.startup import call_on_startup, call_on_shutdown
|
||||||
|
|
||||||
|
from wsme.rest import validate as wsvalidate
|
||||||
|
import wsme.api
|
||||||
|
import wsme.protocols.restjson
|
||||||
|
|
||||||
|
__all__ = ['adapt', 'wsexpose', 'wsvalidate']
|
||||||
|
|
||||||
|
|
||||||
|
def wsexpose(*args, **kwargs):
|
||||||
|
tg_json_expose = expose(
|
||||||
|
'wsmejson',
|
||||||
|
accept_format='application/json',
|
||||||
|
content_type='application/json',
|
||||||
|
tg_format='json'
|
||||||
|
)
|
||||||
|
tg_altjson_expose = expose(
|
||||||
|
'wsmejson',
|
||||||
|
accept_format='text/javascript',
|
||||||
|
content_type='application/json'
|
||||||
|
)
|
||||||
|
tg_xml_expose = expose(
|
||||||
|
'wsmxml',
|
||||||
|
accept_format='text/xml',
|
||||||
|
content_type='text/xml',
|
||||||
|
tg_format='xml'
|
||||||
|
)
|
||||||
|
sig = wsme.signature(*args, **kwargs)
|
||||||
|
|
||||||
|
def decorate(f):
|
||||||
|
sig(f)
|
||||||
|
funcdef = wsme.api.FunctionDefinition.get(f)
|
||||||
|
|
||||||
|
@functools.wraps(f)
|
||||||
|
def callfunction(self, *args, **kwargs):
|
||||||
|
print args, kwargs, cherrypy.request.body
|
||||||
|
args, kwargs = wsme.protocols.commons.get_args(
|
||||||
|
funcdef, args, kwargs,
|
||||||
|
cherrypy.request.body,
|
||||||
|
cherrypy.request.headers['Content-Type']
|
||||||
|
)
|
||||||
|
result = f(self, *args, **kwargs)
|
||||||
|
return dict(
|
||||||
|
datatype=funcdef.return_type,
|
||||||
|
result=result
|
||||||
|
)
|
||||||
|
|
||||||
|
callfunction = tg_json_expose(callfunction)
|
||||||
|
callfunction = tg_altjson_expose(callfunction)
|
||||||
|
callfunction = tg_xml_expose(callfunction)
|
||||||
|
return callfunction
|
||||||
|
|
||||||
|
return decorate
|
||||||
|
|
||||||
|
|
||||||
|
class AutoJSONTemplate(object):
|
||||||
|
def __init__(self, extra_vars_func=None, options=None):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def load_template(self, templatename):
|
||||||
|
"There are no actual templates with this engine"
|
||||||
|
pass
|
||||||
|
|
||||||
|
def render(self, info, format="json", fragment=False, template=None):
|
||||||
|
"Renders the template to a string using the provided info."
|
||||||
|
data = wsme.protocols.restjson.tojson(
|
||||||
|
info['datatype'],
|
||||||
|
info['result']
|
||||||
|
)
|
||||||
|
return json.dumps(data)
|
||||||
|
|
||||||
|
def get_content_type(self, user_agent):
|
||||||
|
return "application/json"
|
||||||
|
|
||||||
|
|
||||||
class WSMECherrypyFilter(BaseFilter):
|
class WSMECherrypyFilter(BaseFilter):
|
||||||
def __init__(self, controller):
|
def __init__(self, controller):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user