Start working on adding protocols when used as a cornice complement

This commit is contained in:
Christophe de Vienne 2012-10-25 21:35:08 +02:00
parent e4b852292f
commit 21ea8a9b17
2 changed files with 28 additions and 2 deletions

View File

@ -14,6 +14,7 @@ And use it::
def get_hello(who=u'World'):
return Message(text='Hello %s' % who)
"""
from __future__ import absolute_import
import json
import xml.etree.ElementTree as et
@ -76,15 +77,38 @@ def wsexpose(*args, **kwargs):
request.override_renderer = 'wsmejson'
elif 'text/xml' in request.headers['Accept']:
request.override_renderer = 'wsmexml'
else:
request.override_renderer = 'wsmejson'
return {
'datatype': funcdef.return_type,
'result': f(*args, **kwargs)
}
callfunction.wsme_func = f
return callfunction
return decorate
def scan_api(root=None):
from cornice.service import get_services
for service in get_services():
for method, func, options in service.definitions:
wsme_func = getattr(func, 'wsme_func')
basepath = service.path.split('/')
if basepath and not basepath[0]:
del basepath[0]
if wsme_func:
yield (
basepath + [method.lower()],
wsme_func._wsme_definition
)
def includeme(config):
import pyramid.wsgi
wsroot = wsme.WSRoot(scan_api=scan_api, webpath='/ws')
wsroot.addprotocol('extdirect')
config.add_renderer('wsmejson', WSMEJsonRenderer)
config.add_renderer('wsmexml', WSMEXmlRenderer)
config.add_route('wsme', '/ws/*path')
config.add_view(pyramid.wsgi.wsgiapp(wsroot.wsgiapp()), route_name='wsme')

View File

@ -78,10 +78,12 @@ class WSRoot(object):
"""
__registry__ = wsme.types.registry
def __init__(self, protocols=[], webpath='', transaction=None):
def __init__(self, protocols=[], webpath='', transaction=None,
scan_api=scan_api):
self._debug = True
self._webpath = webpath
self.protocols = []
self.scan_api = scan_api
self._transaction = transaction
if self._transaction is True:
@ -123,7 +125,7 @@ class WSRoot(object):
:rtype: list of (path, :class:`FunctionDefinition`)
"""
if self._api is None:
self._api = [i for i in scan_api(self)]
self._api = [i for i in self.scan_api(self)]
for path, fdef in self._api:
fdef.resolve_types(self.__registry__)
return self._api