28

RPC to REST (Hypermedia)

Embed Size (px)

DESCRIPTION

A keynote from APIdays, Dec 3-4 2012, Paris, France that covers an analytical & practical approach towards designing APIs in general, and hypermedia in specific, as outlined by an experience at Klarna, Sweden. Questions? http://twitter.com/andreineculau Higher quality slides at: https://speakerdeck.com/andreineculau/rpc-to-rest-hypermedia

Citation preview

Page 1: RPC to REST (Hypermedia)
Page 2: RPC to REST (Hypermedia)

RPC to ����REST HypermediaConcrete case on Payments API

@andreineculau

Page 3: RPC to REST (Hypermedia)

RPC - Making a Remote Procedure Call

1 client imagines:2 server.translate ’Hello ’, ’sv’345 client sends:6 POST /rpc7 > Content -Type: application/json8 {9 "method ": "translate",

10 "args": [" Hello", "sv"]11 }

1 server expects:2 app.post ’/rpc ’, (req , res) ->3 fun = req.body.method # translate4 args = req.body.args # "Hello", "sv"5 call fun , args # "Hej"678 server sends:9 < 200 OK

10 < Content -Type: application/json11 {12 "result ": "Hej"13 }

Page 4: RPC to REST (Hypermedia)

REST - Making a HTTP request

1 client imagines:2 server.translate ’Hello ’, ’sv’345 client sends:6 POST /translate7 > Content -Type: application/json8 {9 "text": "Hello",

10 "language ": "sv"11 }

1 server expects:2 app.post ’/translate ’, (req , res) ->3 fun = ’translate ’ # translate4 args = req.body # "Hello", "sv"5 call fun , args # "Hej"678 server sends:9 < 200 OK

10 < Content -Type: application/json11 {12 "result ": "Hej"13 }

Page 5: RPC to REST (Hypermedia)

RPC to REST

Page 6: RPC to REST (Hypermedia)

RPC to REST

Page 7: RPC to REST (Hypermedia)

RPC to REST

Page 8: RPC to REST (Hypermedia)

Look around you. It’s not just you.

Page 9: RPC to REST (Hypermedia)

Perspectives. Hypermedia vs REST

Page 10: RPC to REST (Hypermedia)

Klarna

Page 11: RPC to REST (Hypermedia)

Journey of an API

I envisionI specI mockI serveI consume

Page 12: RPC to REST (Hypermedia)

Serving uniform HTTP

Page 13: RPC to REST (Hypermedia)

serve: Webmachine

Page 14: RPC to REST (Hypermedia)

serve: JeSSE

schema

Page 15: RPC to REST (Hypermedia)
Page 16: RPC to REST (Hypermedia)

Journey of an API

I envisionI specI mockI serveI consume

Page 17: RPC to REST (Hypermedia)

envision: RESTgraph

Page 18: RPC to REST (Hypermedia)

spec: RESTpi for machines

Page 19: RPC to REST (Hypermedia)

spec: RESTpi by humans for humans

Page 20: RPC to REST (Hypermedia)

mock: Apiary.io

Page 21: RPC to REST (Hypermedia)

consume: RESTit

I 1 URI as the only runtime requirementI Centered around resourcesI Simplify representationsI Transfer Protocol LayerI Hypermedia LayerI HooksI Sugar

Page 22: RPC to REST (Hypermedia)

consume: RESTit

1 it ’should enter the store and look around ’, (done) ->2 re$ = RESTit ().re$3 re$(’http :// starbucks.apiary.io ’)4 .read()5 .callback (err , resp) ->6 return done err if err7 resp [0]. statusCode.should.equal 2008 done()

1 it ’should see what can be done with orders ’, (done) ->2 re$ = RESTit ().re$34 re$(’http :// starbucks.apiary.io ’)5 .read()6 .follow(’/rels/orders ’)7 .options ()8 .callback (err , resp) ->9 return done err if err

10 resp [0]. statusCode.should.equal 20011 resp [0]. canPOST.should.equal true12 should.not.equal resp [0]. canDELETE , true # undefined13 done()

Page 23: RPC to REST (Hypermedia)

consume: RESTit

1 it ’should place an order and then update it’, (done) ->2 re$ = RESTit ().re$34 re$(’http :// starbucks.apiary.io ’)5 .read()6 .follow(’/rels/orders ’)7 .create ({ representation :{drink:’espresso ’}})8 .followLocation ()9 .read()

10 .callback (err , resp) ->11 return done err if err12 resp [0]. statusCode.should.equal 20013 @[0]. bookmarkAs ’myOrder ’14 changeMyMind ()1516 changeMyMind = () ->17 re$(’myOrder ’)18 .update ({ representation :{ ammend_drink:’with cream ’}})19 .callback (err , resp) ->20 return done err if err21 return done()22 resp [0]. statusCode.should.equal 20023 resp [0]. representation.get(’.drink ’). should.equal ’espresso con panna ’24 done()

Page 24: RPC to REST (Hypermedia)

consume: RESTit

1 it ’should smuggle an order ’, (done) ->2 re$ = RESTit ().re$34 re$(’myOrder ’)5 .go(’smuggle ’, {representation :{drink:’latte ’}})6 .callback (err , resp) ->7 return done err if err8 resp [0]. statusCode.should.equal 2009 resp [0]. representation.get(’.drink ’). should.equal ’latte ’

10 done()

Page 25: RPC to REST (Hypermedia)

toolssimpler, safer & more fun

Page 26: RPC to REST (Hypermedia)

KITTKISS

Page 27: RPC to REST (Hypermedia)

RPC to ����REST HypermediaConcrete case on Payments API

@andreineculau

December 3-4, 2012, APIdays.io