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

Designing Hypermedia APIs - RailsClub2012

Steve Klabnik
September 15, 2012

Designing Hypermedia APIs - RailsClub2012

This is my hypermedia talk I gave at RailsClub 2012

Steve Klabnik

September 15, 2012
Tweet

More Decks by Steve Klabnik

Other Decks in Programming

Transcript

  1. $ curl -i -H "Accept: application/json" \ http://localhost:3000 HTTP/1.1 200

    OK Link: <http://localhost:3000/profile>; rel="profile" {"job": {"number_one":null, "number_two":null, "links":[ {"href":"/jobs", "rel":"index"}]}} Saturday, September 15, 12
  2. $ curl -i -H "Accept: application/json" \ -X POST \

    -d "job[number_one]=1" \ -d "job[number_two]=2" \ http://localhost:3000/jobs HTTP/1.1 302 Found Location: http://localhost:3000/jobs/1 Link: <http://localhost:3000/profile>; rel="profile" {“message”: “You are being redirected”} Saturday, September 15, 12
  3. $ curl -i -H "Accept: application/json" http://localhost: 3000/jobs/1 HTTP/1.1 200

    OK Link: <http://localhost:3000/profile>; rel="profile" {"job": {"number_one":1, "number_two":2, "status":"in_progress", "links":[ {"href":"/jobs/1", "rel":"self"}, {"href":"/jobs", "rel":"index"}]}} Saturday, September 15, 12
  4. $ curl -i -H "Accept: application/json" http:// localhost:3000/jobs/1 HTTP/1.1 200

    OK Link: <http://localhost:3000/profile>; rel="profile" {"job": {"number_one":1, "number_two":2, "answer":3, "status":"finished", "links":[ {"href":"/jobs/1", "rel":"self"}, {"href":"/jobs", "rel":"index"}]}} Saturday, September 15, 12
  5. • Client-Server • Stateless • Caching • Uniform Interface •

    identification of resources • representations • self-descriptive messages • hypermedia as the engine of application state • Layered System • Code-on-demand Saturday, September 15, 12
  6. • Client-Server • Stateless • Caching • Uniform Interface •

    identification of resources • representations • self-descriptive messages • hypermedia as the engine of application state • Layered System • Code-on-demand Saturday, September 15, 12
  7. ... a profile can be described as additional semantics that

    can be used to process a resource representation, such as constraints, conventions, extensions, or any other aspects that do not alter the basic media type semantics. Saturday, September 15, 12
  8. "REST is software design on the scale of decades: every

    detail is intended to promote software longevity and independent evolution. Many of the constraints are directly opposed to short-term efficiency. Unfortunately, people are fairly good at short- term design, and usually awful at long-term design." - Fielding Saturday, September 15, 12
  9. Node.js is awesome. JS everywhere means sharing models between client

    and server! - J. Random Saturday, September 15, 12
  10. $ tree -d src /home/steveklabnik/src |-- my_rails_app | `-- app

    | `-- models | `-- foo.rb |-- my_api_gem `-- lib `-- my_api_gem `-- foo.rb Saturday, September 15, 12
  11. a representation is data that is structured in some way

    (like application/json) Saturday, September 15, 12
  12. metadata that allows them to interpret it is most importantly

    the media type (via Content-Type) Saturday, September 15, 12
  13. Client code should be processing a media type, not duplicating

    business objects. Saturday, September 15, 12
  14. Hypermedia way: “here’s where you’re at in the state machine

    and how to interpret it” Saturday, September 15, 12