<div style="line-height:1.7;color:#000000;font-size:14px;font-family:Arial"><p style="margin: 0px 0px 1em; padding: 0px; border: 0px; font-size: 15px; clear: both; color: rgb(34, 34, 34); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 19.5px;">I'm planning to design some REST APIs with Twisted in Python. For example, I want to use the HTTP method "GET" to fetch a single user's information:</p><pre style="margin-top: 0px; padding: 5px; border: 0px; font-size: 13px; overflow: auto; width: auto; max-height: 600px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; word-wrap: normal; color: rgb(34, 34, 34); background-color: rgb(238, 238, 238);"><code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit;">GET http://myhost:8000/api/v1.0/users/[user_id]
</code></pre><div style="margin: 0px 0px 1em; padding: 0px; border: 0px; font-size: 15px; clear: both; color: rgb(34, 34, 34); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 19.5px;">I know I should inherit the twisted.web.resource.Resource and implement "getChild" by myself. </div><div style="margin: 0px 0px 1em; padding: 0px; border: 0px; font-size: 15px; clear: both; color: rgb(34, 34, 34); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 19.5px;">The question is, should I implement a class for each segment of the URI? If so, I have to implement class API, class V1, class Users and Class User. In other words, if there're 10 segments in the URI, do I have to implement 10 classes to represent those resources?</div><br><br><br><br><div style="position:relative;zoom:1"></div><div id="divNeteaseMailCard"></div><br>At 2015-10-25 20:13:58, "Burak Arslan" <burak.arslan@arskom.com.tr> wrote:<br> <blockquote id="isReplyContent" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">
  
    
  
  
    Hey!<br>
    <br>
    <div class="moz-cite-prefix">On 10/25/15 13:04, Wang Yan wrote:<br>
    </div>
    <blockquote cite="mid:675af318.a4b6.1509eac2325.Coremail.snailcoder@163.com" type="cite">
      <div style="line-height:1.7;color:#000000;font-size:14px;font-family:Arial">
        <div>Hi,</div>
        <div><br>
        </div>
        <div>I'm confused about how to design REST APIs with Twisted,
          especially when my app have to interact with MySQL. </div>
        <div><br>
        </div>
        <div>Is there any basic design patterns for this kind of
          situation?</div>
      </div>
    </blockquote>
    <br>
    I do Spyne, and I think it's <u>fa-bu-lous</u> for building any web
    API on top of twisted.<br>
    <br>
    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.<br>
    <br>
    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.<br>
    <br>
    Here's a simple example:
    <a class="moz-txt-link-freetext" href="https://github.com/arskom/spyne/blob/master/examples/twisted/resource.py">https://github.com/arskom/spyne/blob/master/examples/twisted/resource.py</a><br>
    <br>
    There's a boilerplate generator here: <a class="moz-txt-link-freetext" href="http://spyne.io/">http://spyne.io/</a><br>
    <br>
    I hope you find it useful. Any questions, <a class="moz-txt-link-abbreviated" href="mailto:people@spyne.io">people@spyne.io</a> is also at
    your service.<br>
    <br>
    Best,<br>
    Burak<br>
  

</blockquote></div><br><br><span title="neteasefooter"><p> </p></span>