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

I Am Hypermedia (And So Can You)

I Am Hypermedia (And So Can You)

There’s no wrong way to build an API. There are, however, tools and techniques at our disposal for building stable APIs others can depend on without sacrificing our own ability to change. Call it REST, call it Hypermedia, call it anything you like. We’ll keep the examples practical and speak the common languages of HTML and HTTP. This talk is for you if you deploy code to the web even if you’ve never heard of Hypermedia or if you have a life-size poster of Roy Fielding hanging in your office.

Larry Marburger

October 27, 2012
Tweet

More Decks by Larry Marburger

Other Decks in Technology

Transcript

  1. > GET /books < 200 OK [{ id: 42, price:

    "$10.20", title: "The Hitchhiker's Guide..." }, { id: 43, price: "$11.20", title: "The Restaurant at the End..." }] List Books
  2. > GET /books?page=2 < 200 OK [{ id: 44, price:

    "$11.20", title: "So Long, and Thanks for All..." }] List Books
  3. View Book > GET /books/42 < 200 OK { id:

    42, price: "$10.20", title: "The Hitchhiker's Guide...", author: "Douglas Adams", owned: false }
  4. Purchase Book > POST /purchases { book_id: 42 } <

    201 Created { id: 42, price: "$10.20", title: "The Hitchhiker's Guide...", author: "Douglas Adams", owned: true }
  5. Read Book > GET /read/42 < 200 OK > GET

    /read/43 < 403 Forbidden
  6. List Purchases > GET /purchases < 200 OK [{ id:

    42, price: "$10.20", title: "The Hitchhiker's Guide...", owned: true }]
  7. Move Images > GET http://cdn.example.com/42.png < 200 OK < Content-Type:

    image/png > GET /covers/42 < 302 Found < Location: http://cdn.example.com/42.png
  8. Lend Book > POST /lendings { book_id: 42, borrower_id: 13

    } < 201 Created { id: 42, title: "The Hitchhiker's Guide...", ... }
  9. Lend Book > GET /books/42 < 200 OK { id:

    42, title: "The Hitchhiker's Guide...", owned: true, lender_id: 13, ... } > GET /read/42 < 200 OK
  10. Lend Book > GET /books/42 < 200 OK { id:

    42, title: "The Hitchhiker's Guide...", owned: false, borrower_id: 13, ... } > GET /read/42 < 403 Forbidden
  11. RPC

  12. “REST emphasizes scalability of component interactions, generality of interfaces, independent

    deployment of components, and intermediary components to reduce interaction latency, enforce security, and encapsulate legacy systems.” - Roy T. Fielding
  13. Templated Queries <form method="get" action="/search"> <input name="query" type="text" value="" />

    <input type="submit" value="Search" /> </form> GET /search?query=hitchhiker HTTP/1.1 Host: example.org
  14. Non-Idempotent Updates <form method="post" action="/tags"> <input name="tag" type="text" value="" />

    <input type="submit" value="Create" /> </form> POST /tags HTTP/1.1 Host: example.org Content-Type: application/x-www-form-urlencoded Content-Length: 29 tag=intergalactic+hitchhiking
  15. H Factors LE: Embedding links LO: Outbound links LT: Templated

    queries LN: Non-Idempotent updates LI: Idempotent updates CR: Read control data CU: Update control data CM: Request control data CL: Link control data
  16. > GET / < 200 OK <a rel="books" href="...">Books</a> <a

    rel="purchases" href="...">Purchases</a> List Books
  17. => books < 200 OK <ol id="books"> <li> <span class="price">$10.20</span>

    <a rel="self" class="name" href="..."> The Hitchhiker's Guide... </a> </li> <li> ... </li> </ol> <a rel="next" href="...">Next</a> List Books
  18. List Books => next < 200 OK <ol id="books"> <li>

    <span class="price">$11.20</span> <a rel="self" class="name" href="..."> So Long, and Thanks for All... </a> </li> </ol> <a rel="prev" href="...">Prev</a>
  19. View Book => self < 200 OK <img src="..."> <span

    class="price">$11.20</span> <span class="name">The Hitchhiker's...</span> <span class="author">Douglas Adams</span> <form method="post" action="..."> <input name="book_id" value="42"> <input type="submit" value="Buy"> </form>
  20. Purchase Book => submit form < 201 Created <img src="...">

    <span class="price">$11.20</span> <span class="name">The Hitchhiker's...</span> <span class="author">Douglas Adams</span> <a rel="read" href="...">Read</a> => read < 200 OK
  21. List Purchases > GET / < 200 OK <a rel="books"

    href="...">Books</a> <a rel="purchases" href="...">Purchases</a> => purchases < 200 OK <ol id="books"> <li> ... </li> </ol>
  22. Move Images => self < 200 OK <img src="/covers/42"> =>

    self < 200 OK <img src="http://cdn.example.com/42.png">
  23. Lend Book => self < 200 OK <img src="..."> <span

    class="price">$11.20</span> <span class="name">The Hitchhiker's...</span> <span class="author">Douglas Adams</span> <a rel="read" href="...">Read</a> <form method="post" action="..."> <input name="book_id" value="42"> <input name="borrower_id" value=""> <input type="submit" value="Lend"> </form>
  24. Lend Book => submit form < 201 Created <img src="...">

    <span class="price">$11.20</span> <span class="name">The Hitchhiker's...</span> <span class="author">Douglas Adams</span>
  25. Lend Book => self < 200 OK <img src="..."> <span

    class="price">$11.20</span> <span class="name">The Hitchhiker's...</span> <span class="author">Douglas Adams</span> <a rel="read" href="...">Read</a> <form method="post" action="..."> <input name="book_id" value="42"> <input type="submit" value="Buy"> </form>