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

OSCON 2012: Designing Hypermedia APIs

OSCON 2012: Designing Hypermedia APIs

In this talk, Steve will explain how to design your APIs so that they truly embrace the web and HTTP. Just as there's an impedance mismatch between our databases, our ORMs, and our models, there's an equal mismatch between our applications, our APIs, and our clients. Pros and cons of this approach will be discussed, as well as why more people aren't building APIs this way yet.

Steve Klabnik

July 19, 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"}]}} Wednesday, July 18, 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”} Wednesday, July 18, 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"}]}} Wednesday, July 18, 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"}]}} Wednesday, July 18, 12
  5. "We reject kings, presidents and voting. We believe in rough

    consensus and running code." - IETF Credo Wednesday, July 18, 12
  6. Cons: 1. Latency 2. Reliance on caching 3. text formats

    & efficiency Wednesday, July 18, 12
  7. • 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 Wednesday, July 18, 12
  8. • 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 Wednesday, July 18, 12
  9. ... 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. Wednesday, July 18, 12
  10. Hypermedia way: “here’s where you’re at in the state machine

    and how to interpret it” Wednesday, July 18, 12
  11. 1. Implement the media type, not a specific server's responses.

    2. If possible, make two services and test your client against both. 3. Be flexible: don't rely on structure too much. 4. Implement local caching. Wednesday, July 18, 12