[Twisted-web] CorePost 0.0.7 - all about content types

Jacek Furmankiewicz jacek99 at gmail.com
Thu Sep 29 17:49:29 EDT 2011


CorePost 0.0.7, the REST microframework built on top of twisted.web is out.

http://pypi.python.org/pypi/CorePost/0.0.7

This is a big release with lots of enhancements all around content types:

1) automatic parsing of incoming content based on type

@route("/post/json",(Http.POST,Http.PUT))
def test_json(self,request,**kwargs):
    return "%s" % json.dumps(request.json)

@route("/post/xml",(Http.POST,Http.PUT))
def test_xml(self,request,**kwargs):
    return "%s" % ElementTree.tostring(request.xml)

@route("/post/yaml",(Http.POST,Http.PUT))
def test_yaml(self,request,**kwargs):
    return "%s" % yaml.dump(request.yaml)


2) ability to route to different methods for the same URL by incoming
content

@route("/post/by/content",(Http.POST,Http.PUT),MediaType.APPLICATION_JSON)
def test_content_app_json(self,request,**kwargs):
    return request.received_headers[HttpHeader.CONTENT_TYPE]

@route("/post/by/content",(Http.POST,Http.PUT),(MediaType.TEXT_XML,MediaType.APPLICATION_XML))
def test_content_xml(self,request,**kwargs):
    return request.received_headers[HttpHeader.CONTENT_TYPE]

@route("/post/by/content",(Http.POST,Http.PUT),MediaType.TEXT_YAML)
def test_content_yaml(self,request,**kwargs):
    return request.received_headers[HttpHeader.CONTENT_TYPE]

@route("/post/by/content",(Http.POST,Http.PUT))
def test_content_catch_all(self,request,**kwargs):
    return MediaType.WILDCARD

3) ability to return dict/list response and have them automatically convert
to JSON or YAML, depending on what caller can Accept

@route("/return/by/accept")
def test_return_content_by_accepts(self,request,**kwargs):
    val = [{"test1":"Test1"},{"test2":"Test2"}]
    return val

Calling this URL with "Accept: application/json" will return:

[{"test1": "Test1"}, {"test2": "Test2"}]

Calling it with "Accept: text/yaml" will return:

- {test1: Test1}
- {test2: Test2}

4) proper support for *defer.returnValue()* *in @defer.inlineCallbacks
routers (which supports the auto-conversion shown above)

@route("/",Http.GET)
@defer.inlineCallbacks
def root(self,request,**kwargs):
    val1 = yield db.query("SELECT ....")
    val2 = yield db.query("SELECT ....")
    defer.returnValue(val1 + val2)


Cheers,
Jacek
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-web/attachments/20110929/a69b742c/attachment.htm 


More information about the Twisted-web mailing list