CorePost 0.0.7 - all about content types

Jacek Furmankiewicz <[email protected]>
Newsgroups gmane.comp.python.twisted.web
Message-ID <CAGDztiHiRKVjmA=-UQ=-3TXLAbev0dXzTZS9ORTmda0jW__Sig@mail.gmail.com>
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

_______________________________________________
Twisted-web mailing list
[email protected]
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.