BitWorking

Joe Gregorio's writings (archives), projects and status updates.

Dispatch

Pass in the method and the media-range and the best matching function will be called. For example, if BaseHttpDispatch is constructed with a mime type map that maps 'text/xml' to 'xml', then if dispatch is called with 'POST' and 'text/xml' will first look for 'POST_xml' and then if that fails it will try to call 'POST'.

Each function so defined must return a tuple

(headers, body)

where 'headers' is a dictionary of headers for the response and 'body' is any object that simulates a file.

The module can also handle wildcards in the mime-types.

BaseHttpDispatch member functions:

Usage


>>> from dispatch import BaseHttpDispatch
>>> from StringIO import StringIO
>>> class MyDispatcher(BaseHttpDispatch):
        def __init__(self):
            BaseHttpDispatch.__init__(self, 
               {'text/plain':'text', 'application/html':'html'}
            )
        def GET_html(self):
            return ({"Status": "200 Ok", "Content-type": "application/html"}, 
                StringIO("<html><body><p>This is is some text.</p></body></html>")) 
        def GET_image(self):
            return ({"Status": "200 Ok", "Content-type": "text/plain"}, 
                StringIO("This is is some text")) 

>>> d = MyDispatcher()
>>> d.dispatch("GET", "text/plain")
{'Status': '200 Ok', 'Content-type': 'text/plain'}, <StringIO.StringIO 
  instance at 0x00B4C2D8>)

Requirements

Additional Information

Author
Joe Gregorio
License
MIT

Download

dispatch.py - The uncompressed source code of the single file that constitutes this module. You can run the file stand-alone to execute the unit tests.

Revision History

0.1
Initial Release

This page last updated on: 11 August 2005