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

REST and other important stuff

REST and other important stuff

Joshua Thijssen

January 01, 2012
Tweet

More Decks by Joshua Thijssen

Other Decks in Programming

Transcript

  1. Representational
    State
    Transfer
    (REST)
    (and some other important stuff)
    woensdag 25 april 12

    View Slide

  2. Warning before we start
    ‣ REST != MVC
    ‣ Do not think in controllers, id’s,
    actions, models, views, plugins,
    helpers etc...
    ‣ In fact, do not think about
    implementation AT ALL!!
    woensdag 25 april 12

    View Slide

  3. What is REST?
    REST is a coordinated set of architectural constraints that attempts to minimize
    latency and network communication while at the same time maximizing the
    independence and scalability of component implementations. This is achieved by
    placing constraints on connector semantics where other styles have focused on
    component semantics. REST enables the caching and reuse of interactions,
    dynamic substitutability of components, and processing of actions by
    intermediaries, thereby meeting the needs of an Internet-scale distributed
    hypermedia system.
    Roy Fielding said:
    woensdag 25 april 12

    View Slide

  4. What is REST?
    ‣ ADVANTAGES OF REST
    ‣ Cacheable
    ‣ Stateless
    ‣ Scalable
    ‣ Fault-tolerant
    ‣ Loosely coupled
    woensdag 25 april 12

    View Slide

  5. What is REST?
    ‣ URL identifies a resource
    ‣ URLs have an hierarchy
    ‣ Methods perform operations on resources
    ‣ Operation must be implicit
    ‣ Hypermedia format to represent data
    ‣ Link relations to navigate
    ‣ THE PRINCIPLES OF REST
    woensdag 25 april 12

    View Slide

  6. What is REST?
    ‣ THE FOUR MAIN PRINCIPLES
    ‣ Identification of resources
    ‣ Manipulation of resources
    ‣ Self-descriptive messages
    ‣ HATEOAS
    woensdag 25 april 12

    View Slide

  7. What is REST?
    ‣ THE FOUR MAIN PRINCIPLES
    ‣ Identification of resources
    ‣ Manipulation of resources
    ‣ Self-descriptive messages
    ‣ HATEOAS
    woensdag 25 april 12

    View Slide

  8. Identification of resources
    ‣ /index.php?action=getarticle&id=5
    ‣ /default/article/5/4/6/size
    ‣ You are doing it wrong... :(
    Cacheable? Scalable? Readable?
    woensdag 25 april 12

    View Slide

  9. Identification of resources
    ‣ /articles
    ‣ /articles/5/photos/4/comments/1
    ‣ /articles/5/photos/4/comments
    We want the first comment of the fourth photo for the fifth article
    We want all comments of the fourth photo for the fifth article
    We want all articles
    Cacheable! Scalable! Readable!
    ‣ Readable and maintainable!
    woensdag 25 april 12

    View Slide

  10. Identification of resources
    ✗ /photos/order/size/limit/5
    ✗ /photos/limit/5/order/size
    ✓ /photos?order=size&limit=5
    ✓ /photos?limit=5&order=size
    ‣ FILTERING THROUGH A QUERY STRING, NOT THE URI
    woensdag 25 april 12

    View Slide

  11. What is REST?
    ‣ THE FOUR MAIN PRINCIPLES
    ‣ Identification of resources
    ‣ Manipulation of resources
    ‣ Self-descriptive messages
    ‣ HATEOAS
    woensdag 25 april 12

    View Slide

  12. Manipulation of resources
    ‣ Create
    ‣ Retrieve
    ‣ Update
    ‣ Delete
    ‣ But please note that REST != CRUD
    woensdag 25 april 12

    View Slide

  13. Manipulation of resources
    ‣ Create = PUT
    ‣ Retrieve = GET
    ‣ Update = POST
    ‣ Delete = DELETE
    ‣ CRUD to HTTP verb mapping
    woensdag 25 april 12

    View Slide

  14. Manipulation of resources
    ‣ SAFE METHODS
    ‣ Any client should be able to make
    the request as many times as
    necessary.
    ‣ GET, OPTIONS, HEAD
    woensdag 25 april 12

    View Slide

  15. Manipulation of resources
    ‣ IDEMPOTENT METHODS
    ‣ Garantuees that the client can
    repeat the request when it’s not
    certain.
    ‣ $x++ vs $x=4
    ‣ ALL METHODS EXCEPT “POST”
    woensdag 25 april 12

    View Slide

  16. What is REST?
    ‣ THE FOUR MAIN PRINCIPLES
    ‣ Identification of resources
    ‣ Manipulation of resources
    ‣ Self-descriptive messages
    ‣ HATEOAS
    woensdag 25 april 12

    View Slide

  17. Self-descriptive messages
    ‣ Stateless!
    ‣ All information for processing is available:
    ‣ How? (method + content-type)
    ‣ What? (URI)
    ‣ When? (preconditions)
    ‣ Who? (authentication)
    woensdag 25 april 12

    View Slide

  18. Self-descriptive messages
    GET /article/1234 HTTP/1.1
    Host: www.enrise.com
    Accept: application/vnd.enrise.nl+xml ; version: 1.0
    Authorization: OAuth oauth_nonce=”123” ...
    If-None-Matched: absad12412414
    ‣ HOW (method)
    woensdag 25 april 12

    View Slide

  19. Self-descriptive messages
    GET /article/1234 HTTP/1.1
    Host: www.enrise.com
    Accept: application/vnd.enrise.nl+xml ; version: 1.0
    Authorization: OAuth oauth_nonce=”123” ...
    If-None-Matched: absad12412414
    ‣ HOW (content-type)
    woensdag 25 april 12

    View Slide

  20. Self-descriptive messages
    GET /article/1234 HTTP/1.1
    Host: www.enrise.com
    Accept: application/vnd.enrise.nl+xml ; version: 1.0
    Authorization: OAuth oauth_nonce=”123” ...
    If-None-Matched: absad12412414
    ‣ WHAT
    woensdag 25 april 12

    View Slide

  21. Self-descriptive messages
    GET /article/1234 HTTP/1.1
    Host: www.enrise.com
    Accept: application/vnd.enrise.nl+xml ; version: 1.0
    Authorization: OAuth oauth_nonce=”123” ...
    If-None-Matched: absad12412414
    ‣ WHEN
    woensdag 25 april 12

    View Slide

  22. Self-descriptive messages
    GET /article/1234 HTTP/1.1
    Host: www.enrise.com
    Accept: application/vnd.enrise.nl+xml ; version: 1.0
    Authorization: OAuth oauth_nonce=”123” ...
    If-None-Matched: absad12412414
    ‣ WHO
    woensdag 25 april 12

    View Slide

  23. What is REST?
    ‣ THE FOUR MAIN PRINCIPLES
    ‣ Identification of resources
    ‣ Manipulation of resources
    ‣ Self-descriptive messages
    ‣ HATEOAS
    woensdag 25 april 12

    View Slide

  24. HATEOAS
    HATEOAS
    =
    Hypermedia As The Engine Of Application State
    woensdag 25 april 12

    View Slide

  25. HATEOAS
    This is the hardest and
    of course, most important part of REST
    woensdag 25 april 12

    View Slide

  26. HATEOAS
    ‣ Use links to allow clients to discover
    locations and operations.
    ‣ Link relations are used to express
    options.
    ‣ Clients do not need to know URLs.
    ‣ This controls the state.
    woensdag 25 april 12

    View Slide

  27. State inside your REST API
    ‣ “Flight booking API”
    Select Confirm Pay
    Fetch
    E-Ticket
    woensdag 25 april 12

    View Slide

  28. State inside your REST API
    ‣ Search for specified flights
    POST /search?order=price&limit=5 HTTP/1.1
    Host: www.enrise.com
    Accept: application/vnd.enrise.nl+xml ; version: 1.0

    LPA
    24-may-2011
    firstclass

    woensdag 25 april 12

    View Slide

  29. State inside your REST API
    ‣ Returns a collection of flights
    HTTP/1.1 200 OK
    Content-type: application/vnd.enrise.nl+xml ; version: 1.0


    KL1234
    4:24




    HV123
    3:54




    woensdag 25 april 12

    View Slide

  30. State inside your REST API
    ‣ Confirm a specific flight
    POST /confirm/flight/15263 HTTP/1.1
    Host: www.enrise.com
    Accept: application/vnd.enrise.nl+xml ; version: 1.0

    2A
    vegetarian

    HTTP/1.1 401 Authentication required
    woensdag 25 april 12

    View Slide

  31. State inside your REST API
    ‣ Confirm a specific flight, with more info
    POST /confirm/flight/15263 HTTP/1.1
    Host: www.enrise.com
    Accept: application/vnd.enrise.nl+xml ; version: 1.0
    Authorization: OAuth ......

    2A
    vegetarian

    HTTP/1.1 200 OK
    Location: /booking/1616163





    woensdag 25 april 12

    View Slide

  32. State inside your REST API
    ‣ What can we do with our booking?
    OPTIONS /booking/1616163 HTTP/1.1
    Host: www.enrise.com
    Authorization: OAuth ......
    HTTP/1.1 200 OK
    Allow: GET, DELETE, PUT
    woensdag 25 april 12

    View Slide

  33. State inside your REST API
    ‣ Cancel our booking!
    DELETE /booking/1616163 HTTP/1.1
    Host: www.enrise.com
    Authorization: OAuth ......
    HTTP/1.1 204 No content
    woensdag 25 april 12

    View Slide

  34. State inside your REST API
    ‣ Still need to pay for the flight
    GET /booking/1616163 HTTP/1.1
    Host: www.enrise.com
    Accept: application/vnd.enrise.nl+xml ; version: 1.0
    Authorization: OAuth...
    HTTP/1.1 200 OK


    KL1234
    4:24



    Not paid




    woensdag 25 april 12

    View Slide

  35. State inside your REST API
    ‣ Pay through another resource
    PUT /payment/booking/1616163 HTTP/1.1
    Host: www.enrise.com
    Accept: application/vnd.enrise.nl+xml ; version: 1.0
    Authorization: OAuth ......

    4111-1111-1111-1111
    04/13
    Joshua Thijssen
    414.00

    HTTP/1.1 201 Created
    Location: /payment/booking/1616163
    woensdag 25 april 12

    View Slide

  36. State inside your REST API
    ‣ Can’t delete our booking since it’s paid
    OPTIONS /booking/1616163 HTTP/1.1
    Host: www.enrise.com
    Authorization: OAuth ......
    HTTP/1.1 200 OK
    Allow: GET
    woensdag 25 april 12

    View Slide

  37. State inside your REST API
    ‣ We can fetch our eticket now
    GET /booking/1616163 HTTP/1.1
    Host: www.enrise.com
    Accept: application/vnd.enrise.nl+xml ; version: 1.0
    Authorization: OAuth...
    HTTP/1.1 200 OK


    KL1234
    4:24



    Paid in full




    woensdag 25 april 12

    View Slide

  38. (Common) pitfalls of REST design
    ‣ Versioning
    ‣ Methods in uri
    ‣ One uri per resource
    ‣ Controller resources
    & non-CRUD
    woensdag 25 april 12

    View Slide

  39. (Common) pitfalls of REST design
    ‣ Versioning
    ‣ Methods in uri
    ‣ One uri per resource
    ‣ Controller resources & Non-CRUD
    woensdag 25 april 12

    View Slide

  40. (Common) pitfalls of REST design
    ‣ /api/v1.1/article/1234/photos
    ‣ /api/v1.2/article/1234/photos
    ‣ Different resources?
    ‣ Versioning
    woensdag 25 april 12

    View Slide

  41. (Common) pitfalls of REST design
    GET /api/article/1234/photos HTTP/1.1
    Host: www.enrise.com
    Accept: application/vnd.enrise.nl+xml ; version = 1.0
    GET /api/article/1234/photos HTTP/1.1
    Host: www.enrise.com
    Accept: application/vnd.enrise.nl+json ; version = 1.1
    ‣ Versioning
    GET /api/article/1234/photos HTTP/1.1
    Host: www.enrise.com
    Accept: application/vnd.enrise.nl+json ; version = 5.0.4a
    woensdag 25 april 12

    View Slide

  42. (Common) pitfalls of REST design
    ‣ Versioning
    ‣ Methods in uri
    ‣ One uri per resource
    ‣ Controller resources & Non-CRUD
    woensdag 25 april 12

    View Slide

  43. (Common) pitfalls of REST design
    ‣ /api/get/articles/1234/photos
    ‣ /api/articles/new
    ‣ /api/articles/list
    ‣ Methods in URL
    woensdag 25 april 12

    View Slide

  44. (Common) pitfalls of REST design
    ‣ Versioning
    ‣ Methods in uri
    ‣ One uri per resource
    ‣ Controller resources & Non-CRUD
    woensdag 25 april 12

    View Slide

  45. (Common) pitfalls of REST design
    ‣ /api/article/1234
    ‣ /api/article/red+teddybear
    ‣ Different resources
    ‣ One URI per resource
    woensdag 25 april 12

    View Slide

  46. (Common) pitfalls of REST design
    GET /api/article/red+teddybear HTTP/1.1
    Host: www.enrise.com
    Accept: application/vnd.enrise.nl+xml ; version = 1.0
    HTTP/1.1 303 See Other
    Location: /api/article/1234
    ‣ One URI per resource
    woensdag 25 april 12

    View Slide

  47. (Common) pitfalls of REST design
    ‣ Versioning
    ‣ Methods in uri
    ‣ One uri per resource
    ‣ Controller resources & Non-CRUD
    woensdag 25 april 12

    View Slide

  48. (Common) pitfalls of REST design
    ‣ Outside the CRUD?
    ‣ Multiple operations simultaneously?
    ‣ Controller resources & non-crud
    woensdag 25 april 12

    View Slide

  49. (Common) pitfalls of REST design
    POST /distance HTTP/1.1
    Host: www.enrise.com
    Accept: text/xml
    Content-type: text/xml;charset=UTF-8

    Amersfoort, NLD
    Apeldoorn, NLD

    HTTP/1.1 200 OK
    Content-length: 123
    Content-type: text/xml

    45
    27.96

    ‣ Controller resources & non-crud
    GET /distance?from=...&to=.. HTTP/1.1
    Host: www.enrise.com
    Accept: text/xml
    woensdag 25 april 12

    View Slide

  50. (Common) pitfalls of REST design
    POST /user/jthijssen/address_merge HTTP/1.1
    Host: www.enrise.com
    Accept: application/vnd.enrise.nl+xml ; version = 1.0
    Content-type: text/csv;charset=UTF-8
    John Doe, 1 Main Street, Seattle, WA
    Jane Doe, 100 North Street, Los Angeles, CA
    HTTP/1.1 303 See Other
    Location: /user/jthijssen/addressbook
    ‣ Controller resources & non-crud
    woensdag 25 april 12

    View Slide

  51. More important stuff
    ‣ HTTP Status codes
    ‣ ETags
    woensdag 25 april 12

    View Slide

  52. HTTP Status codes
    ‣ Status codes are important
    ‣ They represent the result of your
    actions
    woensdag 25 april 12

    View Slide

  53. HTTP Status codes
    ‣ 1xx
    ‣ 2xx
    ‣ 3xx
    ‣ 4xx
    ‣ 5xx
    Informational
    Success
    Redirection
    Client error
    Server error
    woensdag 25 april 12

    View Slide

  54. HTTP Status codes
    ‣ 200 OK
    ‣ 201 Created
    ‣ 204 No content
    ‣ IMPORTANT 2xx CODES
    Resource returned
    Resource created
    Resource deleted
    woensdag 25 april 12

    View Slide

  55. HTTP Status codes
    ‣ 304 Not modified
    ‣ IMPORTANT 3xx CODES
    Resource wasn’t changed
    woensdag 25 april 12

    View Slide

  56. HTTP Status codes
    ‣ 400 Bad request
    ‣ 401 Unauthorized
    ‣ 403 Forbidden
    ‣ 404 Not found
    ‣ IMPORTANT 4xx CODES
    Resource was not found
    Not authorized to operate
    Not authorized to operate
    Incorrect payload
    woensdag 25 april 12

    View Slide

  57. HTTP Status codes
    ‣ 405 Method not allowed
    ‣ 406 Not acceptable
    ‣ 412 Precondition failed
    ‣ IMPORTANT 4xx CODES
    Method incorrect
    “ETag mismatch”
    Cannot return in correct format
    woensdag 25 april 12

    View Slide

  58. HTTP Status codes
    ‣ 500 Internal server error
    ‣ 501 Not implemented
    ‣ IMPORTANT 5xx CODES
    “Something” happened
    Method is not implemented
    woensdag 25 april 12

    View Slide

  59. HTTP Status codes
    ‣ 501 Not implemented vs
    405 Method not allowed
    ‣ 409 Conflict vs
    412 Precondition failed
    ‣ de·bat·a·ble/diˈbātəbəl/Adjective
    woensdag 25 april 12

    View Slide

  60. ETags & Optimistic locking
    GET /blogpost/12345 HTTP/1.1
    Host: www.enrise.com
    HTTP/1.1 200 OK
    Content-length: 1234000
    Content-type: text/xml
    ETag: abcd-1234


    Joshua Thijssen
    ...
    ....


    woensdag 25 april 12

    View Slide

  61. ETags & Optimistic locking
    GET /blogpost/12345 HTTP/1.1
    Host: www.enrise.com
    If-None-Match: abcd-1234
    HTTP/1.1 304 Not modified
    Blogpost is cached and can be used!
    woensdag 25 april 12

    View Slide

  62. ETags & Optimistic locking
    POST /blogpost/12345 HTTP/1.1
    Host: www.enrise.com
    If-Match: abcd-1234

    Sjors de Valk

    HTTP/1.1 412 Precondition failed
    Blogpost is already modified by “someone”
    woensdag 25 april 12

    View Slide

  63. REST examples
    Other REST Examples
    woensdag 25 april 12

    View Slide

  64. REST examples
    ‣ Creating a resource
    PUT /articles HTTP/1.1
    Host: www.enrise.com
    Content-type: application/vnd.enrise.nl+xml ; version = 1.0

    Teddybear
    red
    15
    15,95
    19,95

    HTTP/1.1 201 Created
    Location: /articles/1234
    woensdag 25 april 12

    View Slide

  65. REST examples
    ‣ Getting a resource collection
    GET /articles HTTP/1.1
    Host: www.enrise.com
    Accept: application/vnd.enrise.nl+xml ; version = 1.0
    HTTP/1.1 200 OK
    Content-length: 12345
    Content-type: application/vnd.enrise.nl+xml
    Date: sun, 01 Nov 2010 12:34:56 GMT


    Teddybear



    Skippyball



    woensdag 25 april 12

    View Slide

  66. REST examples
    ‣ Getting a resource
    GET /articles/1234 HTTP/1.1
    Host: www.enrise.com
    Accept: application/vnd.enrise.nl+xml ; version = 1.0
    HTTP/1.1 200 OK
    Content-length: 12345
    Content-type: application/vnd.enrise.nl+xml
    Date: sun, 01 Nov 2010 12:34:56 GMT

    Teddybear
    ...

    HTTP/1.1 404 Not found
    Content-length: 0
    Date: sun, 01 Nov 2010 12:34:56 GMT
    woensdag 25 april 12

    View Slide

  67. REST examples
    ‣ Delete a resource
    DELETE /articles/1234 HTTP/1.1
    Host: www.enrise.com
    Accept: application/vnd.enrise.nl+xml ; version = 1.0
    HTTP/1.1 204 No content
    Content-length: 0
    Date: sun, 01 Nov 2010 12:34:56 GMT
    woensdag 25 april 12

    View Slide

  68. REST examples
    ‣ Updating a resource
    POST /articles/1234 HTTP/1.1
    Host: www.enrise.com
    Accept: application/vnd.enrise.nl+xml ; version = 1.0
    If-Match: 23709-12135125

    Teddybear
    red
    30
    15,95
    19,95

    HTTP/1.1 200 OK
    Content-length: 0
    Date: sun, 01 Nov 2010 12:34:56 GMT
    HTTP/1.1 412 Precondition failed
    Content-length: 0
    Date: sun, 01 Nov 2010 12:34:56 GMT
    Idempotent
    woensdag 25 april 12

    View Slide

  69. (Common) pitfalls of REST design
    POST /user/jthijssen/address_merge HTTP/1.1
    Host: www.enrise.com
    Accept: application/vnd.enrise.nl+xml ; version = 1.0
    Content-type: text/csv;charset=UTF-8
    John Doe, 1 Main Street, Seattle, WA
    Jane Doe, 100 North Street, Los Angeles, CA
    HTTP/1.1 303 See Other
    Location: /user/jthijssen/addressbook
    ‣ Controller resources
    woensdag 25 april 12

    View Slide

  70. More reading
    ‣ http://en.wikipedia.org/wiki/Representational_State_Transfer
    ‣ http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm
    ‣ http://oreilly.com/catalog/9780596801694
    ‣ http://www.slideshare.net/Wombert/designing-http-interfaces-and-
    restful-web-services-confoo11-20110310
    ‣ http://www.slideshare.net/adorepump/hateoas-the-confusing-bit-
    from-rest
    ‣ http://www.slideshare.net/guilhermecaelum/rest-in-practice
    woensdag 25 april 12

    View Slide

  71. Question & discussion
    http://farm1.static.flickr.com/73/163450213_18478d3aa6_d.jpg
    woensdag 25 april 12

    View Slide

  72. ‣ THANK YOU FOR YOUR ATTENTION
    woensdag 25 april 12

    View Slide