Upgrade to Pro — share decks privately, control downloads, hide ads and more …

David Hume

Tara Simpson
November 11, 2013

David Hume

David Hume's presentation from recent 'Developer Bash' event (http://instil.co/2013/09/18/shiny-bash/).

REST is dead. Long live(d) Hypermedia APIs! Buzzwords aside, though, a perennial problem when attempting to design such APIs is the choice of media type. Atom? Siren? Mint your own? This talk will explore the less obvious choice of HTML5 as the media type for an API along with the challenges faced and the benefits reaped by a real world implementation.

Tara Simpson

November 11, 2013
Tweet

More Decks by Tara Simpson

Other Decks in Programming

Transcript

  1. “If the API is not being driven by hypertext, then

    it cannot be RESTful and cannot be a REST API. Period. Is there some broken manual somewhere that needs to be fixed?” - Roy Fielding
  2. “Hypermedia Types are MIME media types that contain native hyper-linking

    semantics that induce application flow.” - Mike Amundsen
  3. Data Types and Structures • Lists (<li/>) and Bags (<ul/>)

    • Maps (<dl/>) • Dates/Times (<time/>) • Machine + Human Readable (<data/>) • Multimedia (<img/>, <audio/>, <video/>)
  4. Provides elements with semantics • Adds meaning to structure •

    Like the _type property common in JSON • Can use existing “standard” types ◦ http://schema.org • Or use custom types if required
  5. <div> <div>Latitude: <data value="40.75"> 40 deg 44 min 54.36 sec

    N </data> </div>, <div>Longitude: <data value="73.98"> 73 deg 59 min 8.5 dec W </data> </div> </div>
  6. <div itemscope itemtype="http://schema.org/GeoCoordinates"> <div>Latitude: <data value="40.75"> 40 deg 44 min

    54.36 sec N </data> </div>, <div>Longitude: <data value="73.98"> 73 deg 59 min 8.5 dec W </data> </div> </div>
  7. <div itemscope itemtype="http://schema.org/GeoCoordinates"> <div>Latitude: <data itemprop="latitude" value="40.75"> 40 deg 44

    min 54.36 sec N </data> </div>, <div>Longitude: <data itemprop="longitude" value="73.98"> 73 deg 59 min 8.5 dec W </data> </div> </div>
  8. <div itemscope itemtype="http://schema.org/GeoCoordinates"> <div>Latitude: <data itemprop="latitude" value="40.75"> 40 deg 44

    min 54.36 sec N </data> </div>, <div>Longitude: <data itemprop="longitude" value="73.98"> 73 deg 59 min 8.5 dec W </data> </div> </div>
  9. <article> <h1>System Down</h1> <ul> <li>type 1</li> <li>type 2</li> <li>type 3</li>

    </ul> <div>Broken</div> <a href="assign">Reassign</a> <a href="reject">Reject</a> </article>
  10. <article itemscope itemtype="http://example.com/Issue"> <h1>System Down</h1> <ul> <li>type 1</li> <li>type 2</li>

    <li>type 3</li> </ul> <div>Broken</div> <a href="assign">Reassign</a> <a href="reject">Reject</a> </article>
  11. <article itemscope itemtype="http://example.com/Issue"> <h1 itemprop="name">System Down</h1> <ul> <li>type 1</li> <li>type

    2</li> <li>type 3</li> </ul> <div itemprop="description">Broken</div> <a href="assign">Reassign</a> <a href="reject">Reject</a> </article>
  12. <article itemscope itemtype="http://example.com/Issue"> <h1 itemprop="name">System Down</h1> <ul> <li itemprop="category">type 1</li>

    <li itemprop="category">type 2</li> <li itemprop="category">type 3</li> </ul> <div itemprop="description">Broken</div> <a href="assign">Reassign</a> <a href="reject">Reject</a> </article>
  13. <article itemscope itemtype="http://example.com/Issue"> <h1 itemprop="name">System Down</h1> <ul> <li itemprop="category">type 1</li>

    <li itemprop="category">type 2</li> <li itemprop="category">type 3</li> </ul> <div itemprop="description">Broken</div> <a href="assign">Reassign</a> <a href="reject">Reject</a> </article>
  14. Links provide flow For example: • Related resources • List

    items • Resources describing available actions
  15. <article itemscope itemtype="http://example.com/Issue"> <h1 itemprop="name">System Down</h1> <ul> <li itemprop="category">type 1</li>

    <li itemprop="category">type 2</li> <li itemprop="category">type 3</li> </ul> <div itemprop="description">Broken</div> <a href="assign">Reassign</a> <a href="reject">Reject</a> </article>
  16. Link Relations == Link Semantics • @rel attribute can be

    used on: ◦ <link/> ◦ <a/> • Expresses the relationship type of the link • Can be used by clients to distinguish links
  17. <article itemscope itemtype="http://example.com/Issue"> <h1 itemprop="name">System Down</h1> <ul> <li itemprop="category">type 1</li>

    <li itemprop="category">type 2</li> <li itemprop="category">type 3</li> </ul> <div itemprop="description">Broken</div> <a rel="http://example.com/AssignAction" href="assign">Reassign</a> <a rel="http://example.com/RejectAction" href="reject">Reject</a> </article>
  18. <form action="search" method="GET" enctype="application/x-www-form-urlencoded"> <label for="q">Query</label> <input id="q" name="q" type="text"

    value="red wedding" /> <input type="submit" value="Search" /> </form> Describe possible requests
  19. <form action="search" method="GET" enctype="application/x-www-form-urlencoded"> <label for="q">Query</label> <input id="q" name="q" type="text"

    value="red wedding" /> <input type="submit" value="Search" /> </form> Where the request can be sent
  20. <form action="search" method="GET" enctype="application/x-www-form-urlencoded"> <label for="q">Query</label> <input id="q" name="q" type="text"

    value="red wedding" /> <input type="submit" value="Search" /> </form> How the request can be made
  21. <form action="search" method="GET" enctype="application/x-www-form-urlencoded"> <label for="q">Query</label> <input id="q" name="q" type="text"

    value="red wedding" /> <input type="submit" value="Search" /> </form> What (media) type can be sent
  22. <form action="search" method="GET" enctype="application/x-www-form-urlencoded"> <label for="q">Query</label> <input id="q" name="q" type="text"

    value="red wedding" /> <input type="submit" value="Search" /> </form> What data can be sent...
  23. <form action="search" method="GET" enctype="application/x-www-form-urlencoded"> <label for="q">Query</label> <input id="q" name="q" type="text"

    value="red wedding" /> <input type="submit" value="Search" /> </form> ...and what defaults can it have
  24. <form action="search" method="GET" enctype="application/x-www-form-urlencoded"> <label for="q">Query</label> <input id="q" name="q" type="text"

    value="red wedding" /> <input type="submit" value="Search" /> </form> What submissions can be made
  25. <form action="search" method="GET" enctype="application/x-www-form-urlencoded" data-rel="search"> <label for="q">Query</label> <input id="q" name="q"

    type="text" value="red wedding" /> <input type="submit" value="Search" /> </form> Extend with relation semantics
  26. Working with HTML APIs • CSS Selectors (jQuery, JSoup, Jerry

    etc.) • XML (if you must: DOM/SAX etc.)
  27. The Good • Explore / Debug / Test - Use

    a browser ◦ No firebug needed! (though still useful) • Decent support for most languages • Specs for HTML itself ◦ Still need to document @rel and @itemtype use
  28. The Bad • Easy to forget target audience (machines) •

    Spec weaknesses: ◦ No PUT or DELETE support (yet) ◦ Use of @data-rel on <form/>s not standard ◦ Value of <input/>s with @itemprop not standard • Arguably verbose
  29. Application-as-API • Users want their UI on top of your

    services • And they want everything your app can do • Make the app the API • Works well with ROCA