Prajwal Tuladhar’s Blog
 
programming, life and some random thoughts

Archive for March, 2008

Mar 31 2008

Key REST Principles

Published by admin under REST

The five key principles of REST are:

  • Give every “thing” and/or resource an “ID”
  • Link things together
  • Use standard methods
  • Resources with multiple representations
  • Communicate in a stateless manner

    Give every “thing” and/or resource an “ID”

    Everything that should be identifiable should obviously get an ID — on the Web, there is a unified concept for IDs: The URI. URIs make up a global namespace, and using URIs to identify our key resources means they get a unique, global ID.

    The main benefit of a consistent naming scheme for things is that you don’t have to come up with your own scheme — you can rely on one that has already been defined, works pretty well on global scale and is understood by practically anybody.

    Here are some examples of URIs you might come up with:

    To summarize the first principle: Use URIs to identify everything that merits being identifiable, specifically, all of the “high-level” resources that one’s application provides, whether they represent individual items, collections of items, virtual and physical objects, or computation results.

    <span style="color: blue">&lt;</span><span style="color: #a31515">order </span><span style="color: red">self</span><span style="color: blue">='http://example.com/customers/1234' &gt; 
       &lt;</span><span style="color: #a31515">amount</span><span style="color: blue">&gt;</span>23<span style="color: blue">&lt;/</span><span style="color: #a31515">amount</span><span style="color: blue">&gt; 
       &lt;</span><span style="color: #a31515">product </span><span style="color: red">ref</span><span style="color: blue">='http://example.com/products/4554' /&gt; 
       &lt;</span><span style="color: #a31515">customer </span><span style="color: red">ref</span><span style="color: blue">='http://example.com/customers/1234' /&gt; 
    &lt;/</span><span style="color: #a31515">order</span><span style="color: blue">&gt; </span>

    Link things together

    Links are something we’re all familiar with from HTML, but they are in no way restricted to human consumption.

    <span class="lnum">   1:  </span>&lt;order self=<span class="str">'http://example.com/customers/1234'</span> &gt;
    <span class="lnum">   2:  </span>     &lt;amount&gt;23&lt;/amount&gt; 
    <span class="lnum">   3:  </span>     &lt;product <span class="kwrd">ref</span>=<span class="str">'http://example.com/products/4554'</span> /&gt; 
    <span class="lnum">   4:  </span>     &lt;customer <span class="kwrd">ref</span>=<span class="str">'http://example.com/customers/1234'</span> /&gt; 
    <span class="lnum">   5:  </span>&lt;/order&gt;

    If we look at the product and customer links in this document, we can easily imagine how an application that has retrieved it can “follow” the links to retrieve more information. Of course, this would be the case if there were a simple “id” attribute adhering to some application-specific naming scheme, too — but only within the application’s context. The beauty of the link approach using URIs is that the links can point to resources that are provided by a different application, a different server, or even a different company on another continent — because the naming scheme is a global standard, all of the resources that make up the Web can be linked to each other.

    To summarize this principles: Use links to refer to identifiable things (resources) wherever possible. Hyperlinking is what makes the Web the Web.

     

    Use standard methods

    <span class="lnum">   1:  </span><span class="kwrd">class</span> Resource 
    <span class="lnum">   2:  </span>{    
    <span class="lnum">   3:  </span>    Resource(URI u);
    <span class="lnum">   4:  </span>    Response get();    
    <span class="lnum">   5:  </span>    Response post(Request r);     
    <span class="lnum">   6:  </span>    Response put(Request r);
    <span class="lnum">   7:  </span>    Response delete();
    <span class="lnum">   8:  </span>}

  • Because the same interface is used for every resource, one can rely on being able to retrieve a representation — i.e., some rendering of it — using GET. Because GET’s semantics are defined in the specification, one can be sure that one has no obligations when one calls it — this is why the method is called “safe”. GET supports very efficient and sophisticated caching, so in many cases, one doesn’t even have to send a request to the server. One can also be sure that a GET is idempotent — if one issues a GET request and don’t get a result, one might not know whether one’s request never reached its destination or the response got lost on its way back. The idempotence guarantee means one can simply issue the request again. Idempotence is also guaranteed for PUT (which basically means “update this resource with this data, or create it at this URI if it’s not there already”) and for DELETE (which one can simply try again and again until one gets a result — deleting something that’s not there is not a problem). POST, which usually means “create a new resource”, can also be used to invoke arbitrary processing and thus is neither safe nor idempotent.

     

    Resources with multiple representations

    The approach taken by HTTP is to allow for a separation of concerns between the handling of data and invoking operations. In other words, a client that knows how to handle a particular data format can interact with all resources that can provide a representation in this format. For example; using HTTP content negotiation, a client can ask for representation of a particular format:

    GET /customers/1234 HTTP/1.1
    Host: example.com 
    Accept: application/vnd.mycompany.customer+xml

    If a client “knows” both the HTTP application protocol and a set of data formats, it can interact with any RESTful HTTP application in the world in a very meaningful way.

     

    Communicate Statelessly

    A server should not have to retain some sort of communication state for any of the clients it communicates with beyond a single request. The most obvious reason for this is scalability — the number of clients interacting would seriously impact the server’s footprint if it had to keep client state.

    One response so far

    Mar 14 2008

    Ajax and XML : Ajax for tables

    Published by admin under Ajax, JavaScript, PrototypeJS

    IBM DeveloperWorks has published a very nice article illustrating the use of Ajax and XML for displaying and/or working with tabular data. Prototype JS framework has been used in the article.

    http://www.ibm.com/developerworks/xml/library/x-ajaxxml10/?S_TACT=105AGX54&S_CMP=B0314&ca=dnw-910

    2 responses so far

    RSS Feed

    Random Posts


    follow infynyxx at http://twitter.com