pyrest / pyrest / integration /

import cherrypy
from pyrest.pyrest import get_handlers, requesthandler

API = None #This should be set to a PyRest backend API if used as WSGI application

class PyRest(object):
    def __init__(self, api):
        self._api = api
        self._handlers = get_handlers(api)
    def index(self):
        return "HTTP access to %s available at <a href='/api'>/api</a><br/> %s" % (self._api.__name__, self._api.__doc__)

    def api(self, resource, *pathargs, **kwargs):
        method = cherrypy.request.method.lower()
        response = requesthandler(self._handlers, method, resource, *pathargs, **kwargs)
        cherrypy.response.status = response.status
        return response.content = True = True

def start(api, host, port):
    CONF = {
      'global': {
        'server.socket_host': host,
        'server.socket_port': port,

    ROOT = PyRest(api)

    cherrypy.quickstart(ROOT, '/', CONF)
#Not actually tested
def application(environ, start_response):
  cherrypy.tree.mount(PyRest(API), '/', None)
  return cherrypy.tree(environ, start_response)