[Twisted-Python] How to design REST API with Twisted?

Burak Arslan burak.arslan at arskom.com.tr
Sun Oct 25 06:13:58 MDT 2015


Hey!

On 10/25/15 13:04, Wang Yan wrote:
> Hi,
>
> I'm confused about how to design REST APIs with Twisted, especially
> when my app have to interact with MySQL. 
>
> Is there any basic design patterns for this kind of situation?

I do Spyne, and I think it's _fa-bu-lous_ for building any web API on
top of twisted.

It's a bit different from what you to with stock Twisted though. More
specifically, there's no NOT_DONE_YET. The client hangs as long as you
keep returning `Deferred`s. Once you return a non-deferred (and
hopefully something that's compatible with your designated return type)
that object is serialized using the `out_protocol` you pass to your
`Application` and the resulting byte stream is written to the outgoing
stream of your transport of choice. In case of a HTTP-based api, it's
passed to the `transport.write()` function of a `twisted.web.Resource`
instance, along with the outgoing headers that you set.

As for the MySQL part, I use SQLAlchemy exclusively from inside a
function called by deferToThread. Spyne integrates with SQLAlchemy as
well, so you can return directly what SQLA returns, be it objects or rows.

Here's a simple example:
https://github.com/arskom/spyne/blob/master/examples/twisted/resource.py

There's a boilerplate generator here: http://spyne.io/

I hope you find it useful. Any questions, people at spyne.io is also at
your service.

Best,
Burak
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20151025/384ca18c/attachment-0002.html>


More information about the Twisted-Python mailing list