Commits

fredrik  committed 1aef25d

Comitting via pyrest

  • Participants
  • Parent commits bd628fd

Comments (0)

Files changed (5)

File pyrest/__init__.py

-"""Helper library for creating restful or restful-ish Python APIs"""
+"""Helper library for creating restful or restful-ish Python APIs"""
+from . import pyrest

File pyrest/examples/hg/__init__.py

 """Example backend for pyRest"""
 
-import backend.changeset as changeset
+from . import changeset as changeset

File pyrest/examples/hg/changeset.py

 import hgapi.hgapi as hgapi
+from pyrest import pyrest
 
 def get(ref=None):
     rev = hgapi.Repo('.')[ref]
 
-    return {
+    return pyrest.OkResponse({
     'node': rev.node,
     'desc': rev.desc
-    }
+    })
+
+def post(message=None, user=None):
+    
+    if message is None:
+        return pyrest.MalformedResponse('Need to supply a commit message')
+
+    try:
+        repo = hgapi.Repo('.')
+        if not user is None:
+            repo.hg_commit(message, user)
+        else:
+            repo.hg_commit(message)
+    except Exception as e:
+        if 'Out: nothing changed' in str(e):
+            return pyrest.ConflictResponse('Nothing changed')
+        return pyrest.ErrorResponse(str(e))
+    return pyrest.CreatedResponse('Revision committed')

File pyrest/integration/cherry.py

 import cherrypy
 from pyrest.pyrest import get_handlers, requesthandler
-from pyrest.examples import hg
+
+API = None #This should be set to a PyRest backend API if used as WSGI application
 
 class PyRest(object):
-    def __init__(self):
-        self._handlers = get_handlers(hg)
+    def __init__(self, api):
+        self._handlers = get_handlers(api)
         
     def index(self):
         return "Awesome API..."
     index.exposed = True
     api.exposed = True
 
-CONF = {
+
+
+if __name__ == '__main__':
+    from pyrest.examples import hg
+    CONF = {
     'global': {
         'server.socket_host': '0.0.0.0',
         'server.socket_port': 8888,
-    }
-}
+    }}
 
+    ROOT = PyRest(hg)
 
-if __name__ == '__main__':
-    ROOT = PyRest()
     cherrypy.quickstart(ROOT, '/', CONF)
     
+#Not actually working right now
 def application(environ, start_response):
   cherrypy.tree.mount(PyRest(), '/', None)
   return cherrypy.tree(environ, start_response)

File pyrest/pyrest.py

 from collections import namedtuple
 import inspect
 import json
+
 Response = namedtuple('response', 'status content')
 
+OkResponse = lambda msg: Response('200 Ok', json.dumps(msg))
+CreatedResponse = lambda msg: Response('201 Created', json.dumps(msg))
+MalformedResponse = lambda msg: Response('400 Malformed Request', json.dumps(msg))
+UnauthorizedResponse = lambda msg: Response('401 Unauthorized',json.dumps( msg))
+NotFoundResponse = lambda msg: Response('404 Not found', json.dumps(msg))
+UnsupportedResponse = lambda msg: Response('405 Method Not Allowed', json.dumps(msg))
+ConflictResponse = lambda msg: Response('409 Conflict', json.dumps(msg))
+ErrorResponse = lambda msg: Response('500 Internal Server Error', json.dumps(msg))
+
+
 def get_handlers(package):
     handlers = {}
     for member_name, member in [module for module in inspect.getmembers(package) if inspect.ismodule(module[1])]:
     code to be present after this point"""
 
     if not resource in handlers:
-        return Response('404 Not Found', 'No such resource')
+        return NotFoundResponse('No such resource')
 
     if not  hasattr(handlers[resource], method):
-        return Response('405 Method Not Allowed', 'Unsupported method for resource')
+        return UnsupportedResponse('Unsupported method for resource') #Need to add supported method
 
-    return_data = getattr(handlers[resource], method)(*pathargs, **kwargs)
-    return Response('200 OK', json.dumps(return_data))
+    return getattr(handlers[resource], method)(*pathargs, **kwargs)
+