Skip to content

pdh/diablo

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

Todo

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.

Example

myproject.tac:

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):
        internet.TCPServer.__init__(
            self, HTTP_PORT, Site(RESTApi(routes)))
MyService().setServiceParent(application)

myproject.py:

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()
		else:
			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.

About

Simple REST framework for Twisted

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%