Skip to content

schuyler/dream

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dream

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.

Features

Abstracted away from WSGI

You only have to deal with WebOb’s Request objects, and your functions return Response objects.

Nothing you don’t need

Very lightweight. No ORM. No admin page. Just URL routing and request/response objects.

Per-method endpoints

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.

Conveniently namespaced

Common classes from WebOb & Decoroute are imported into the dream namespace — no need for anything other than import dream.

JSONResponse class

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.

Exception handling

Unhanded exceptions are caught and returned as 500 responses, with the exception reason in a JSON payload.

Endpoint introspection

Easily see the endpoints you’ve defined:

Crash course

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

Endpoint introspection

If you’d like to add an endpoint which returns known endpoints, add the following line:

dream.endpoints(App, '/endpoints.json')

About

Hyperminimal WSGI framework

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%