Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

The REST And Then Some

100 views

Published on

This is a session given by Asbjørn Ulsberg at Nordic APIs 2016 Platform Summit on October 25th, in Stockholm Sweden.

Description:

This talk is for developers dabbling in REST, but who aren’t yet invited to Roy Fielding’s thanksgiving dinner. It will go through the history of REST and how it relates to SOAP and RPC-style APIs before delving down into the concepts of hypertext and hypermedia, how to implement them in your API and last, but not least; Why!

If you’ve started exploring the world of REST and miss the comfort of safe typing and service discovery of SOAP and WSDL, this talk is for you. You will learn why this is missing in the world of REST, what alternatives exist and how your API can embrace them and become much more self-descriptive and user friendly than RPC-style APIs could ever dream of!

Published in: Technology
  • Be the first to like this

The REST And Then Some

  1. 1. GET /presentation HTTP/1.1 HTTP/1.1 200 OK Content-Type: application/json { "what": "The REST And Then Some", "where": "Nordic APIs 2016 Platform Summit", "when": "2016-10-25T14:40+02:00" }
  2. 2. Asbjørn Ulsberg Business Architect Web, API, Interfaces
  3. 3. 1941 The Garden of Forking Paths Jorge Luis Borges
  4. 4. “Hypertext” “Hypermedia” 1963
  5. 5. 1968 “The Mother Of All Demos” oN-Line System
  6. 6. 1987
  7. 7. 1989 “WorldWideWeb”
  8. 8. 2000 Architectural Styles and the Design of Network-based Software Architectures By “Representational State Transfer”“REST” Roy Thomas Fielding
  9. 9. Defining REST
  10. 10. POST Does ”something” with the resource GET Retrieves the resource PUT Replaces or creates the resource. PATCH Given right state in client, apply a diff operation DELETE Delete the resource … Defining REST: CRUD
  11. 11. Defining REST: URIs http://api.com/harddrives/format?drive=c http://api.com/orders/getOrderById?id=13 http://api.com/orders/13/edit-form http://api.com/orders/13 http://api.com/731bef1e-8ffa-11e6-96e8
  12. 12. Defining REST: URIs URI Method Description http://api.com/v1/customers POST Creates a new customer http://api.com/v1/customers/{id} GET Retrieves customer http://api.com/v1/customers/{id} PUT Updates customer http://api.com/v1/customers/{id} DELETE Deletes customer http://api.com/v1/customers/{id}/orders GET Retrieves customer’s orders http://api.com/v1/customers/{id}/notes GET Retrieves customer’s notes
  13. 13. Defining REST: URIs
  14. 14. Defining REST: URIs
  15. 15. Defining REST: URIs URI Method Description http://api.com/v1/customers POST Creates a new customer http://api.com/v1/customers/{id} GET Retrieves customer http://api.com/v1/customers/{id} PUT Updates customer http://api.com/v1/customers/{id} DELETE Deletes customer http://api.com/v1/customers/{id}/orders GET Retrieves customer’s orders http://api.com/v1/customers/{id}/notes GET Retrieves customer’s notes
  16. 16. Defining REST: URIs URI Method Description http://api.com/v1/customers POST Creates a new customer http://api.com/v1/customers/{id} GET Retrieves customer http://api.com/v1/customers/{id} PUT Updates customer http://api.com/v1/customers/{id} DELETE Deletes customer http://orders.com/orders-for-customer/{id} GET Retrieves customer’s orders http://api.com/v1/customers/{id}/notes GET Retrieves customer’s notes
  17. 17. Defining REST: URIs URI Method Description http://api.com/v1/customers POST Creates a new customer http://api.com/v1/customers/{id} GET Retrieves customer http://api.com/v1/customers/{id} PUT Updates customer http://api.com/v1/customers/{id} DELETE Deletes customer http://orders.com/orders-for-customer/{id} GET Retrieves customer’s orders http://api.com/v2/customers/{id}/notes GET Retrieves customer’s notes
  18. 18. Defining REST: URIs URI Method Description http://api.com/v2/customers POST Creates a new customer http://api.com/v2/customers/{id} GET Retrieves customer http://api.com/v2/customers/{id} PUT Updates customer http://api.com/v2/customers/{id} DELETE Deletes customer http://orders.com/orders-for-customer/{id} GET Retrieves customer’s orders http://api.com/v2/customers/{id}/notes GET Retrieves customer’s notes
  19. 19. What is the best practice for versioning a REST API?
  20. 20. REST
  21. 21. I feel warmer and more relaxed in Spanish
  22. 22. German makes me reason more carefully
  23. 23. REST: Constraints 1. Client-Server 2. Stateless 3. Cacheable 4. Layered 5. Code on demand (optional) 6. Uniform Interface 1. Identification of resources 2. Manipulation of resources 3. Self-descriptive messages 4. Hypermedia as the engine of application state (HATEOAS)
  24. 24. REST: Constraints 1. Client-Server 2. Stateless 3. Cacheable 4. Layered 5. Code on demand (optional) 6. Uniform Interface 1. Identification of resources 2. Manipulation of resources 3. Self-descriptive messages 4. Hypermedia as the engine of application state (HATEOAS)
  25. 25. Hypermedia as the engine of application state is a REST constraint. Not an option. Not an ideal. Hypermedia is a constraint. As in, you either do it or you aren’t doing REST.
  26. 26. Hypermedia
  27. 27. Hypermedia: Affordances
  28. 28. Hypermedia: Affordances Do people know what to do based on what they see?
  29. 29. Hypermedia: Affordances Do machines know what to do based on what they see?
  30. 30. Hypermedia <a href="/toaster">Toaster</a> <img src="toaster.jpeg" alt="Toaster"> <video src="toaster.mp4">Toaster</video> <form action="/toaster" method="post" rel="on"> <button type="submit" name="state" value="on">Turn on</button> </form>
  31. 31. Hypermedia <a href="/toaster">Toaster</a> <img src="toaster.jpeg" alt="Toaster"> <video src="toaster.mp4">Toaster</video> <form action="/toaster" method="post" rel="on"> <button type="submit" name="state" value="on">Turn on</button> </form>
  32. 32. <a href="/toaster">Toaster</a> <img href="toaster.jpeg" alt="Toaster"> <video href="toaster.mp4">Toaster</video> <form href="/toaster" method="post" rel="on"> <button type="submit" name="state" value="on">Turn on</button> </form>
  33. 33. <id href="/toaster" title="Toaster"> <image href="toaster.jpeg" title="Toaster"> <video href="toaster.mp4" title="Toaster"> <operation href="/toaster" method="put" rel="on"> <expects name="state" value="on" /> </operation>
  34. 34. { "id": "/toaster", "title": "Toaster" } { "image": "toaster.jpeg", "title": "Toaster" } { "video": "toaster.mp4", "title": "Toaster" } { "operation": "/toaster", "method": "PUT", "rel": "on", "expects": { "state": "on" } }
  35. 35. { "id": "/toaster", "title": "Toaster" } { "image": "toaster.jpeg", "title": "Toaster" }, { "video": "toaster.mp4", "title": "Toaster" } "operation": { "rel": "on", "method": "PUT", "href": "/toaster", "expects": { "state": "on" } }
  36. 36. { "id": "/toaster", "links": [ { "image": "toaster.jpeg", "title": "Toaster" }, { "video": "toaster.mp4", "title": "Toaster" } ], "operations": [{ "rel": "on", "method": "PUT", "href": "/toaster", "expects": { "state": "on" } }] }
  37. 37. Hypermedia: State Machine
  38. 38. OnOff Shutting down Shut Down Off Shut Down Upper Limit Lower Limit Heating Idle Do/Turn Off Heater Turn On
  39. 39. Off GET /toaster HTTP/1.1
  40. 40. Off HTTP/1.1 200 OK { "id": "/toaster", "state": "off", "operations": [{ "rel": "on", "method": "PUT", "href": "/toaster", "expects": { "state": "on" } }] }
  41. 41. Off HTTP/1.1 200 OK { "id": "/toaster", "state": "off", "operations": [{ "rel": "on", "method": "PUT", "href": "/toaster", "expects": { "state": "on" } }] }
  42. 42. Off PUT /toaster { "state": "on" }
  43. 43. Off PUT /toaster { "state": "on" }
  44. 44. Off PUT /toaster { "state": "on" }
  45. 45. Off Turn On On PUT /toaster HTTP/1.1 { "state": "on" } HTTP/1.1 200 OK { "id": "/toaster", "state": "on", "strength": 0, "operations": [{ "rel": "off", "method": "PUT", "href": "/toaster", "expects": { "state": "off" } }, { "rel": "strength", "method": "PUT", "href": "/fcef93ba988011e6833f", "expects": { "strength": [1, 2, 3, 4, 5, 6] } }] }
  46. 46. On PUT /fcef93ba988011e6833f { "strength": [1, 2, 3, 4, 5, 6] }
  47. 47. On PUT /fcef93ba988011e6833f { "strength": [1, 2, 3, 4, 5, 6] }
  48. 48. On Heating HTTP/1.1 200 OK { "id": "/toaster", "state": "heating", "strength": 3, "operations": [{ "rel": "off", "method": "PUT", "href": "/toaster", "expects": { "state": "off" } }, { "rel": "strength", "method": "PUT", "href": "/fcef93ba988011e6833f", "expects": { "strength": [1, 2, 3, 4, 5, 6] } }] } PUT /fcef93ba988011e6833f HTTP/1.1 { "strength": 3 }
  49. 49. Heating /toaster
  50. 50. Upper Limit Idle Do/Turn Off Heater Heating HTTP/1.1 200 OK { "id": "/toaster", "state": "idle", "operations": [{ "rel": "off", "method": "PUT", "href": "/toaster", "expects": { "state": "off" } }, { "rel": "strength", "method": "PUT", "href": "/fcef93ba988011e6833f", "expects": { "strength": [1, 2, 3, 4, 5, 6] } }] } GET /toaster HTTP/1.1
  51. 51. Shut Down Shutting down Idle Do/Turn Off Heater GET /toaster HTTP/1.1HTTP/1.1 200 OK { "id": "/toaster", "state": "shutting-down", "operations": [] }
  52. 52. Shutting down GET /toaster HTTP/1.1 Off HTTP/1.1 200 OK { "id": "/toaster", "state": "off", "operations": [{ "rel": "on", "method": "PUT", "href": "/toaster", "expects": { "state": "on" } }] }
  53. 53. { "id": "/toaster", "state": "off", "slot": "/toaster/slot", "operations": [{ "rel": "on", "method": "PUT", "href": "/toaster", "expects": { "state": "on" } }] }
  54. 54. Questions?
  55. 55. Thank You! - @asbjornu - asbjorn.ulsberg@payex.com - slack.httpapis.com Asbjørn Ulsberg

×