Simple REST framework for Twisted
Switch branches/tags
Nothing to show
Pull request Compare This branch is 16 commits behind wuher:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

Simple REST framework for Twisted

Diablo aims to be simple to use REST framework for Twisted. Ultimately diablo should incorporate pretty much the same functionality as its big brother, the Devil.

Currently, only main features are Django style URL routing and appropriate method invocation based on the HTTP request's method.


Next big todo is content type negotiation. Currently, diablo only supports json. For incoming data diablo doesn't do anything, but for outgoing data, everything is encoded (by diablo) into json. Therefore resources must always either raise one of the exceptions derived from diablo.http.HTTPError or return a dictionary that can be encoded into json.



from twisted.application import service, internet
from twisted.web.server import Site
from diablo.api import RESTApi

HTTP_PORT = 8000

routes = [
    ('/user/(?P<user>.{3,20})?(/)?$', 'myproject.Users'),
    ('/order/(?P<order>\d{1,10})(/)?$', 'myproject.Orders'),

application = service.Application("MyApplication")

class MyService(internet.TCPServer):
    """ REST API """

    def __init__(self):
            self, HTTP_PORT, Site(RESTApi(routes)))

from diablo.resource import Resource
from diablo import http

class Users(Resource):
	def get(self, request, *args, **kw):
		user_id = kw['user']
		if something_bad:
			raise http.BadRequest()
			return {
				'name': 'Luke Skywalker',
				'age': 23,

If someone would send any other HTTP method besides GET to the Users resource, diablo would automatically return 405 Method Not Allowed.