Dream is a hyper-minimal framework for creating WSGI apps, which leverages decoroute and WebOb to dispach and abstract requests. It’s particularly good for API type applications, as this is what it was originally written to support.
You only have to deal with WebOb’s Request objects, and your functions return Response objects.
Very lightweight. No ORM. No admin page. Just URL routing and request/response objects.
Decoroute has been extended to allow invoking different functions for the same URL based on the request method. This avoids ugly conditional code in your endpoint functions.
Common classes from WebOb & Decoroute are imported into the dream
namespace — no need for anything other than import dream
.
Since returning JSON is a common thing to do with Dream, there’s a
Response subclass which will serialize it out for you. If you want
a response intended for human consumption, there is also the
clunkily-named HumanReadableJSONResponse
.
Unhanded exceptions are caught and returned as 500 responses, with the exception reason in a JSON payload.
Easily see the endpoints you’ve defined:
Drop this into in my_app.py
:
import dream
App = dream.App()
@App.expose('/greeting.json')
def hello_json(request):
return dream.JSONResponse(body={'greeting': 'Hello, world!'})
@App.expose('/greeting.txt')
def hello_text(request):
return dream.Response(body='Hello, world!',
headers={'Content-Type': 'text/plain'})
@App.expose('/greet/<person:.*>')
def hello_user(request, person):
return dream.Response(body='Nice to meet you, %s' % person,
headers={'Content-Type': 'text/plain'})
Run it:
$ gunicorn my_app:App & $ curl -i http://localhost:8080/greeting.json $ curl -i http://localhost:8080/greeting.txt
If you’d like to add an endpoint which returns known endpoints, add the following line:
dream.endpoints(App, '/endpoints.json')