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

Hypermedia APIs & Self-Building Apps

vlucas
June 09, 2012

Hypermedia APIs & Self-Building Apps

What if your application had all of the information it needed from your REST API to build itself? This session will use an example RESTful Hypermedia API running locally together with a live running custom-built mobile application to demonstrate how to automate building parts of a mobile application UI from a RESTful API response alone. We will then explore techniques to customize and fine-tune the UI while still automating most of the work from the API.

vlucas

June 09, 2012
Tweet

More Decks by vlucas

Other Decks in Programming

Transcript

  1. Vance Lucas • Co-Founder, Brightbit
    http://brightb.it
    Hypermedia APIs &
    Self-Building Apps

    View Slide

  2. Who are You?
    • Vance Lucas
    • http://vancelucas.com
    • @vlucas (for heckling)
    • Brightbit
    • http://brightb.it
    • Design, Development & Consulting for
    web apps, mobile apps and APIs

    View Slide

  3. What is a REST API?
    Hypermedia API?

    View Slide

  4. Developed by Leonard Richardson
    http://martinfowler.com/articles/richardsonMaturityModel.html

    View Slide

  5. “Level 3” is a pre-
    condition of REST
    Roy T. Fielding
    http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven

    View Slide

  6. You must have
    hypermedia controls in
    your API to call it REST

    View Slide

  7. “A REST API should be entered with no prior
    knowledge beyond the initial URI (bookmark)
    and set of standardized media types that are
    appropriate for the intended audience (i.e.,
    expected to be understood by any client that
    might use the API). From that point on, all
    application state transitions must be driven by
    client selection of server-provided choices that
    are present in the received representations or
    implied by the user’s manipulation of those
    representations.”
    Roy T. Fielding
    http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven

    View Slide

  8. What are these
    “hypermedia controls”?

    View Slide

  9. Two Key Things

    View Slide

  10. Links

    View Slide

  11. Forms

    View Slide

  12. But you can’t make
    forms in an API!
    Whhaaaaa!

    View Slide

  13. You Can. Kind of...

    View Slide

  14. JSON Schema
    http://tools.ietf.org/html/draft-zyp-json-schema-03

    View Slide

  15. JSON Schema
    {
    "description":"A person",
    "type":"object",
    "properties":{
    "name":{"type":"string"},
    "age" :{
    "type":"integer",
    "maximum":125
    }
    }
    }

    View Slide

  16. URI Templates
    RFC 6570
    http://tools.ietf.org/html/rfc6570

    View Slide

  17. URI Templates
    http://uri.com/~{username}/
    #-> http://uri.com/~fred/
    http://uri.com/dictionary/{term:1}/{term}
    #-> http://uri.com/dictionary/c/cat
    http://uri.com/search{?q,lang}
    #-> http://uri.com/search?q=cat&lang=en

    View Slide

  18. HAL Spec (Unofficial)
    http://stateless.co/hal_specification.html

    View Slide

  19. HAL Specification
    {
    "_links": {
    "self": { "href": "/orders" },
    "next": { "href": "/orders?page=2" },
    "search": { "href": "/orders?id={order_id}" }
    },
    "_embedded": {
    "order": [
    {
    "_links": {
    "self": { "href": "/orders/123" },
    "customer": { "href": "/u/bob", "title": "Bob" }
    },
    "total": 30.00,
    "currency": "USD",
    "status": "shipped",
    "placed": "2011-01-16",
    }
    ]
    }
    }

    View Slide

  20. Link “rel” Types
    http://www.iana.org/assignments/link-relations/link-
    relations.xml

    View Slide

  21. No official spec for
    linking or describing
    resources for JSON or
    XML APIs

    View Slide

  22. Too Boring; Didn’t Listen:
    Link and describe
    resources however you
    want, but be consistent.

    View Slide

  23. DEMO TIME!
    App: https://github.com/vlucas/brightbudget-app
    API: https://github.com/vlucas/brightbudget-web

    View Slide

  24. Questions?
    Rate this talk: https://joind.in/6205

    View Slide