<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://devlicio.us/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>.NET &amp; Funky Fresh : REST</title><link>http://devlicio.us/blogs/rob_eisenberg/archive/tags/REST/default.aspx</link><description>Tags: REST</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>Alt.Tekpub REST</title><link>http://devlicio.us/blogs/rob_eisenberg/archive/2012/03/05/alt-tekpub-rest.aspx</link><pubDate>Mon, 05 Mar 2012 06:43:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:69571</guid><dc:creator>Rob Eisenberg</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/rob_eisenberg/rsscomments.aspx?PostID=69571</wfw:commentRss><comments>http://devlicio.us/blogs/rob_eisenberg/archive/2012/03/05/alt-tekpub-rest.aspx#comments</comments><description>&lt;p&gt;Mr. Conery has been working through building a RESTful API &lt;a target="_blank" href="http://wekeroad.com/2012/03/03/moving-the-philosophy-into-machinery/"&gt;over on his blog&lt;/a&gt;. He&amp;rsquo;s tired of theory and has called on the community to submit some concrete examples in order to shed some light on the situation. I&amp;rsquo;m no expert in this area (I usually stick to UI stuff), but I have some experience, so I thought I&amp;rsquo;d make a contribution. Here&amp;rsquo;s some thoughts.&lt;/p&gt;
&lt;p&gt;Issuing a GET to http://api.alttekpub.com returns:&lt;/p&gt;
&lt;pre name="code" class="xml:nogutter:nocontrols"&gt;&amp;lt;alttekpub&amp;gt;
  &amp;lt;links&amp;gt;
    &amp;lt;link rel=&amp;quot;self&amp;quot; href=&amp;quot;...&amp;quot; /&amp;gt;
    &amp;lt;link rel=&amp;quot;rels/productions&amp;quot; href=&amp;quot;...&amp;quot; /&amp;gt;
    &amp;lt;link rel=&amp;quot;rels/new-episodes&amp;quot; href=&amp;quot;...&amp;quot; /&amp;gt;
    &amp;lt;link rel=&amp;quot;rels/productions-by-category&amp;quot; href=&amp;quot;...?category={category_name}&amp;quot; /&amp;gt;
  &amp;lt;/links&amp;gt;
  &amp;lt;categories&amp;gt;
    &amp;lt;category name=&amp;quot;Microsoft&amp;quot; /&amp;gt;
    &amp;lt;category name=&amp;quot;Ruby&amp;quot; /&amp;gt;
    &amp;lt;category name=&amp;quot;JavaScript&amp;quot; /&amp;gt;
    &amp;lt;category name=&amp;quot;Mobile&amp;quot; /&amp;gt;
  &amp;lt;/categories&amp;gt;
&amp;lt;/alttekpub&amp;gt;&lt;/pre&gt;
&lt;p&gt;One of my goals here is that clients only need to know a single uri: http://api.alttekpub.com After that, they should be able to follow links inside the document in order to navigate all the data and exercise all the features of the API. Above, you can see that the alttekpub api exposes links to several other documents: productions, new episodes and productions by category. These are identified by a rel attribute which is used to specify the link&amp;rsquo;s relationship to the enclosing document. Every document has a link that points to itself at a minimum. The last link in this example is parameterized. The api provides the various options for parameters in its document below. Here&amp;rsquo;s an alternative way you could represent the same concept, by borrowing some ideas from html:&lt;/p&gt;
&lt;pre name="code" class="xml:nogutter:nocontrols"&gt;&amp;lt;alttekpub&amp;gt;
  &amp;lt;links&amp;gt;
    &amp;lt;link rel=&amp;quot;self&amp;quot; href=&amp;quot;...&amp;quot; /&amp;gt;
    &amp;lt;link rel=&amp;quot;rels/productions&amp;quot; href=&amp;quot;...&amp;quot; /&amp;gt;
    &amp;lt;link rel=&amp;quot;rels/new-episodes&amp;quot; href=&amp;quot;...&amp;quot; /&amp;gt;
  &amp;lt;/links&amp;gt;
  &amp;lt;form method=&amp;quot;GET&amp;quot; action=&amp;quot;...&amp;quot;&amp;gt;
    &amp;lt;select name=&amp;quot;category&amp;quot;&amp;gt;
      &amp;lt;option value=&amp;quot;Microsoft&amp;quot;&amp;gt;Microsoft&amp;lt;/option&amp;gt;
      &amp;lt;option value=&amp;quot;Ruby&amp;quot;&amp;gt;Ruby&amp;lt;/option&amp;gt;
      &amp;lt;option value=&amp;quot;Javascript&amp;quot;&amp;gt;Javascript&amp;lt;/option&amp;gt;
      &amp;lt;option value=&amp;quot;Mobile&amp;quot;&amp;gt;Mobile&amp;lt;/option&amp;gt;
    &amp;lt;/select&amp;gt;
  &amp;lt;/form&amp;gt;
&amp;lt;/alttekpub&amp;gt;&lt;/pre&gt;
&lt;p&gt;I actually like this a little better, but I&amp;rsquo;ve seen both ideas used here and there. What is neat is that the api is self-documenting and there&amp;rsquo;s no need for the client to know how to construct uris. Everything is provided by the server. Let&amp;rsquo;s ay we follow the link for rels/productions. Here&amp;rsquo;s what we might get back:&lt;/p&gt;
&lt;pre name="code" class="xml:nogutter:nocontrols"&gt;&amp;lt;productions&amp;gt;
  &amp;lt;links&amp;gt;
    &amp;lt;link rel=&amp;quot;self&amp;quot; href=&amp;quot;...&amp;quot; /&amp;gt;
    &amp;lt;link rel=&amp;quot;next&amp;quot; href=&amp;quot;...&amp;quot; /&amp;gt;
  &amp;lt;/links&amp;gt;
  &amp;lt;production&amp;gt;
    &amp;lt;name&amp;gt;RavenDB&amp;lt;/name&amp;gt;
    &amp;lt;author&amp;gt;Oren Eini&amp;lt;/author&amp;gt;
    &amp;lt;links&amp;gt;
      &amp;lt;link rel=&amp;quot;self&amp;quot; href=&amp;quot;...&amp;quot; /&amp;gt;
      &amp;lt;link rel=&amp;quot;rels/episodes&amp;quot; href=&amp;quot;...&amp;quot; /&amp;gt;
    &amp;lt;/links&amp;gt;
  &amp;lt;/production&amp;gt;
  &amp;lt;production&amp;gt;
    &amp;lt;name&amp;gt;Art of Speaking&amp;lt;/name&amp;gt;
    &amp;lt;author&amp;gt;Scott Hanselman&amp;lt;/author&amp;gt;
    &amp;lt;links&amp;gt;
      &amp;lt;link rel=&amp;quot;self&amp;quot; href=&amp;quot;...&amp;quot; /&amp;gt;
      &amp;lt;link rel=&amp;quot;rels/episodes&amp;quot; href=&amp;quot;...&amp;quot; /&amp;gt;
    &amp;lt;/links&amp;gt;
  &amp;lt;/production&amp;gt;
  &amp;lt;!--other productions elided--&amp;gt;
&amp;lt;/productions&amp;gt;&lt;/pre&gt;
&lt;p&gt;It&amp;rsquo;s just a simple list of productions. But, there&amp;rsquo;s a couple of things to note. First, notice the rel=&amp;rdquo;next&amp;rdquo; If there are a lot of productions, the server may choose to paginate them. If that is the case, and there&amp;rsquo;s a second page, we will have a link with rel=&amp;rdquo;next&amp;rdquo; as above. We can handle paging backwards in a similar fashion. In fact, there&amp;rsquo;s a host of standard rels which you can and should leverage when it make sense. You can find them here: &lt;a href="http://www.iana.org/assignments/link-relations/link-relations.xml"&gt;http://www.iana.org/assignments/link-relations/link-relations.xml&lt;/a&gt;&amp;nbsp; Another thing to note, is that we&amp;rsquo;ve represented the collection of episodes in a production with another link. Following the RavenDB production&amp;rsquo;s rels/episodes link yields:&lt;/p&gt;
&lt;pre name="code" class="xml:nogutter:nocontrols"&gt;&amp;lt;episodes&amp;gt;
  &amp;lt;links&amp;gt;
    &amp;lt;link rel=&amp;quot;self&amp;quot; href=&amp;quot;...&amp;quot; /&amp;gt;
  &amp;lt;/links&amp;gt;
  &amp;lt;episode&amp;gt;
    &amp;lt;links&amp;gt;
      &amp;lt;link rel=&amp;quot;self&amp;quot; href=&amp;quot;&amp;quot; /&amp;gt;
    &amp;lt;/links&amp;gt;
    &amp;lt;name&amp;gt;Why You Should Care About RavenDB&amp;lt;/name&amp;gt;
    &amp;lt;description&amp;gt;We&amp;#39;ve all heard of NoSQL...&amp;lt;/description&amp;gt;
    &amp;lt;videos&amp;gt;
      &amp;lt;video type=&amp;quot;video/mp4&amp;quot; href=&amp;quot;...&amp;quot; /&amp;gt;
      &amp;lt;video type=&amp;quot;video/ogg&amp;quot; href=&amp;quot;...&amp;quot; /&amp;gt;
    &amp;lt;/videos&amp;gt;
  &amp;lt;/episode&amp;gt;
  &amp;lt;episode&amp;gt;
    &amp;lt;links&amp;gt;
      &amp;lt;link rel=&amp;quot;self&amp;quot; href=&amp;quot;&amp;quot; /&amp;gt;
    &amp;lt;/links&amp;gt;
    &amp;lt;name&amp;gt;Understanding Document Names&amp;lt;/name&amp;gt;
    &amp;lt;description&amp;gt;In this short episode we explain how documents are named with RavenDB - and why.&amp;lt;/description&amp;gt;
    &amp;lt;videos&amp;gt;
      &amp;lt;video type=&amp;quot;video/mp4&amp;quot; href=&amp;quot;...&amp;quot; /&amp;gt;
      &amp;lt;video type=&amp;quot;video/ogg&amp;quot; href=&amp;quot;...&amp;quot; /&amp;gt;
    &amp;lt;/videos&amp;gt;
  &amp;lt;/episode&amp;gt;
&amp;lt;/episodes&amp;gt;&lt;/pre&gt;
&lt;p&gt;There&amp;rsquo;s not really anything new here. We just have basic xml with links. In the above examples, I&amp;rsquo;ve only shown a &amp;ldquo;readonly&amp;rdquo; api for a custom vendor-specific format. But, these concepts can all be expanded on to model all sorts of things through links, forms, etc. Here&amp;rsquo;s a couple of ideas you can implement to improve things further:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Use JSON instead of XML :)&lt;/li&gt;
&lt;li&gt;Add a &amp;ldquo;title&amp;rdquo; attribute to your links. This can be used by clients to display the UI. For example, a client might scan the links in a resource and generate a series of buttons or a toolbar for each link. It could use the title attribute to populate the button&amp;rsquo;s text. Clicking the button would just activate the link. It&amp;rsquo;s very similar to the way a browser&amp;rsquo;s &amp;lt;a /&amp;gt; tag works, and if the client is coded in this way, it allows a lot of flexibility for the service author. They can evolve and extend their service with less changes needed by api consumers.&lt;/li&gt;
&lt;li&gt;Embed resources. After recording usage data, you might discover that a request for productions is quickly followed by a request for episodes. Perhaps you can improve the api usage experience by embedding the episodes in the production documents, so only a single request is needed? There are multiple ways you can design your format to handle this situation. But you might want to consider using something that&amp;rsquo;s already been designed for this scenario, such as &lt;a target="_blank" href="http://stateless.co/hal_specification.html"&gt;HAL&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The takeaway here is that a key concept in REST is hypermedia controls, which I&amp;rsquo;ve represented mostly with links (and a form). These allow the API to document itself, alleviating the need for the client to be coded with specific knowledge of link construction. It also allows the service to evolve more easily by adding additional controls over time or by completely changing uri&amp;rsquo;s or even whether or not certain related documents are embedded.&lt;/p&gt;
&lt;p&gt;Oh..and just a reminder that I&amp;rsquo;m not a REST expert. But hopefully this example sheds some light, however dim :)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=69571" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/rob_eisenberg/archive/tags/Featured/default.aspx">Featured</category><category domain="http://devlicio.us/blogs/rob_eisenberg/archive/tags/REST/default.aspx">REST</category></item></channel></rss>