2.7 KiB
2.7 KiB
Integrating with a Framework
WSGI Application
The wsme.WSRoot.wsgiapp
function of WSRoot returns a wsgi
application.
Example
from wsme import WSRoot, expose
class MyRoot(WSRoot):
@expose(unicode)
def helloworld(self):
return u"Hello World !"
= MyRoot(protocols=['restjson'])
root = root.wsgiapp() application
Bottle
import bottle
import wsme
class MyRoot(wsme.WSRoot):
@wsme.expose(unicode)
def helloworld(self):
return u"Hello World !"
= MyRoot(webpath='/ws', protocols=['restjson'])
root
'/ws', root.wsgiapp())
bottle.mount( bottle.run()
Pyramid
The WSRoot._handle_request method is a valid pyramid view:
from paste.httpserver import serve
from pyramid.config import Configurator
from wsme import *
class WSController(WSRoot):
@expose(int)
@validate(int, int)
def multiply(self, a, b):
return a * b
= WSRoot()
myroot 'restjson')
myroot.addprotocol('extdirect')
myroot.addprotocol(
if __name__ == '__main__':
= Configurator()
config 'ws', '')
config.add_route(='ws')
config.add_view(wsroot._handle_request, route_name= config.make_wsgi_app()
app ='0.0.0.0') serve(app, host
Turbogears 1.x
wsme.tg1
-- TG1 adapter
wsme.tg1
A TG1 Controller that publish a wsme.WSRoot
.
adapt
Returns a Controller
that publish a wsme.WSRoot
.
wsme.tg15
-- TG 1.5 adapter
wsme.tg15
A TG1 Controller that publish a wsme.WSRoot
.
adapt
Returns a Controller
that publish a wsme.WSRoot
.
Example
In a freshly quickstarted tg1 application (let's say, wsmedemo), the prefered way is the following :
Create a new file, "wsmedemo/ws.py" :
import wsme.tg1
from wsme import expose, validate, WSRoot
class WSController(WSRoot):
@expose(int)
@validate(int, int)
def multiply(self, a, b):
return a * b
Insert the ws controller in the controller tree, (file controllers.py):
# ...
from wsmedemo.ws import WSController
import wsme.tg1
class Root(controllers.RootController):
= wsme.tg1.adapt(
ws ='/ws', protocols=['restjson']))
WSController(webpath
# ...