mimerenderPython module for RESTful resource variant rendering using MIME Media-Types | |
Download |
mimerender Ranking & Summary
Advertisement
- License:
- MIT/X Consortium Lic...
- Price:
- FREE
- Publisher Name:
- Martin Blech
- Publisher web site:
- http://code.google.com/u/martinblech/
mimerender Tags
mimerender Description
Python module for RESTful resource variant rendering using MIME Media-Types mimerender is a Python module that allows, with the use of python decorators, to transparently select a render function for an HTTP request handler's result. It uses mimeparse to parse the HTTP Accept header and select the best available representation.Currently it only supports (web.py), but other web frameworks can be considered.Code example:import webimport jsonfrom mimerender import mimerenderrender_xml = lambda message: '%s'%messagerender_json = lambda **args: json.dumps(args)render_html = lambda message: '%s'%messagerender_txt = lambda message: messageurls = ( '/(.*)', 'greet')app = web.application(urls, globals())class greet: @mimerender( default = 'html', html = render_html, xml = render_xml, json = render_json, txt = render_txt ) def GET(self, name): if not name: name = 'world' return {'message': 'Hello, ' + name + '!'}if __name__ == "__main__": app.run()Then you can do:$ curl -H "Accept: application/html" localhost:8080/x< html >< body >Hello, x!< /body >< /html >$ curl -H "Accept: application/xml" localhost:8080/x< message >Hello, x!< /message >$ curl -H "Accept: application/json" localhost:8080/x{'message':'Hello, x!'}$ curl -H "Accept: text/plain" localhost:8080/xHello, x! Requirements: · Python
mimerender Related Software