rpc to rest (hypermedia)
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-hypermediaTRANSCRIPT
RPC to ����REST HypermediaConcrete case on Payments API
@andreineculau
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 }
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 }
RPC to REST
RPC to REST
RPC to REST
Look around you. It’s not just you.
Perspectives. Hypermedia vs REST
Klarna
Journey of an API
I envisionI specI mockI serveI consume
Serving uniform HTTP
serve: Webmachine
serve: JeSSE
schema
Journey of an API
I envisionI specI mockI serveI consume
envision: RESTgraph
spec: RESTpi for machines
spec: RESTpi by humans for humans
mock: Apiary.io
consume: RESTit
I 1 URI as the only runtime requirementI Centered around resourcesI Simplify representationsI Transfer Protocol LayerI Hypermedia LayerI HooksI Sugar
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()
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()
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()
toolssimpler, safer & more fun
KITTKISS
RPC to ����REST HypermediaConcrete case on Payments API
@andreineculau
December 3-4, 2012, APIdays.io
Credits
I Scott Brinker, Marketing Technology Landscape SupergraphicI Adam DuVander/Programmable Web, 5000 APIs: Facebook, Google, Twitter are changing the
webI Google Trends: Architecture stylesI Simon Dongle, Good Morning 2012I Alan Dean, HTTP/1.1 activity diagram
I Philipp Meier, LiberatorI Data-Driven DocumentsI sigma.js