Rest and the hypermedia constraint

5,799 views

Published on

A RESTful API is only truly RESTful if it uses hypermedia to tell us about all the actions that can be performed on the curent resource, allowing us to traverse the API from a single entry point.

His session looks at REST and HATEOAS (Hypermedia As The Engine Of Application State) to illustrate good service structure. Ben will use the RESTful file sharing service fdrop.it to illustrate the various examples of how this can be used.

This session is recommended for architects and senior developers alike and will give a good grounding in writing excellent, self-explanatory RESTful services.

Published in: Technology, News & Politics
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,799
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
104
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • Developer for 20 years\nProfessional developer since 2002\nStarted on C, C++ and Perl\nFocussed on Perl and moved into web app development where I picked up PHP\nmobile web, standards and software architectures\n
  • Technical Team Lead since the start of 2010\nOne of the Sheffield office founders\n
  • Sole developer of fdrop.it\nCreated to solve my problem of ‘why is it so difficult to send a file to someone online?’\n
  • \n
  • \n
  • Doctoral Dissertation\n‘Architectural Styles and the Design of Network-based Software Architectures’\n\none of the principle author of HTTP specification (RFC 1945/2616)\nIn 1999 he was named my MIT Technology Review as one of the top 100 innovators in the world under 35\n\n\n
  • \n
  • Constraints help to guide the creative project\n\nSacrifice is usually made retroactively\n
  • Can be applied to ANY distributed system\nWeb application or API design\n
  • \n
  • Clients not concerned about data storage\nServers not concerned about the user interface or user state\ncomponents evolve independently if interface remains constant\n
  • Each request from the client must contain ALL of the information required\nCannot rely on stored context on the server\nSession state on the client\nVisibility (Server can be observed at any time), Reliability (network failure), Scalability (easy to add servers)\n
  • Responses must implicitly or explicitly define themselves as cacheable or not\nFurther improves scalability and performance\n\n
  • Client connected to the end server or to an intermediary along the way.\nLoad Balancers\nSecurity Policies at a Firewall\nReverse Proxy Cache\n\n
  • Service can temporarily extend client side functionality by providing code it can execute \nie, Javascript or java applets\n\nTHE OPTIONAL CONSTRAINT!\n
  • Four guiding principles to simplify architecture\n
  • On the web we use URI’s\n\nResources themselves are usually different to what is returned to the client\nUsually, a database resource will be represented in JSON or XML\n
  • When a client holds a representation, it is enough to be able to modify or delete the resource assuming the user has permission to do so\n
  • Each message describes itself. This means using an explicit mime type, and explicitly if the resource can be cached.\n
  • Clients make state transitions ONLY through links returned within the resource (ie, anchors in html)\n\nOnly exception is the entry point\n
  • HATEOAS is a horrible acronym\n
  • Using hypermedia (anchor, xlink, etc)\n
  • and it’s why it’s been so successful over the last 20 years. The web is 20 years old. That’s amazing - and because of standards and hypermedia, I can still reference things from 20 years ago.\n
  • A web page contains all the information within it to allow the user to move from the current state, to the next.\n
  • All types of HTML\nXHTML\n\nVersions within them are identified by the content (DOCTYPE, html tag or xml header)\nXML documents not processed as XML (no checks for well formedness)\n \n
  • HTML 5 won over XHTML for who gains supremacy over the browsers - HTML contains some really good stuff about describing text, images, user input etc and what HTTP function to use on them (ie, GET image, POST a form).\n
  • This is how you declare an html 5 document. It’s great for the web as browsers only need to understand GET and POST - this is fine and it’s still RESTful, but what if we need a representation of a resource that can support other actions?\n
  • \n
  • HTML is a hypermedia type\nXML is not\n
  • Accept header in HTTP\n
  • HTTP headers and Hypermedia!\n
  • \nHTTP 1.1 defines these 9 methods. HTML lets us use 2 of them. \n
  • That’s all you get in a browser\n\n
  • \n
  • GET to retrive, POST to create, PUT to update, DELETE\nPOST to a job queue to update and delete\nThese are HTTP, not REST (REST just defines the uniform interface)\n
  • API for free?\nDownsides\nSome actions need to be different over an API due to different UI\nAJAX\n
  • Focus on the UI breaks the API\n
  • Craft my own XML doc\nNeed to define my media type first\nToo much like work...\n
  • There was merit to this - which i’ll come back to\n
  • Remove the markup from the XHTML that was only there to layout the UI\nServe HTML5 to browsers on text/html!\nClose - but webkit on iOS and Android devices prefer XML\n
  • \n
  • \n
  • XHTML still in the media type so still self describing\nWont conflict with browsers\n\n
  • \n\n
  • form, input, img, a\nBrowsers already understand XHTML\nEasy for people to consume using any XML Reader\nDEMO\n
  • XHTML only supports GET and POST\nWhat if we need more?\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Rest and the hypermedia constraint

    1. 1. REST and HATEOASBen Longden, PHPNW 2011
    2. 2. REST and theHypermedia ConstraintBen Longden, PHPNW 2011
    3. 3. Who are you?
    4. 4. Who are you? @blongden
    5. 5. Who are you?
    6. 6. Who are you? @ibuildings
    7. 7. Who are you?
    8. 8. Who are you? @fdrop
    9. 9. RESTREpresentational State Transfer
    10. 10. REST Series of constraints for distributedhypermedia systems - Roy Fielding, 2000
    11. 11. Constraints are good
    12. 12. Constraints are goodThey (help) stop you from doing crazy things
    13. 13. REST is an Architecture It is not a protocol
    14. 14. GoalsScalabilityGenerality of InterfacesIndependent deployment of componentsIntermediaries to reduce latency,enforce security, and encapsulatelegacy systems.
    15. 15. Client server
    16. 16. Stateless
    17. 17. Cache
    18. 18. Layered system
    19. 19. Code on demand
    20. 20. Uniform interface
    21. 21. Uniform interface Identification of resources
    22. 22. Uniform interfaceManipulations of resources through representations
    23. 23. Uniform interface Self descriptive messages
    24. 24. Uniform interfaceHypertext as the engine of application state
    25. 25. The hypermedia constraint?
    26. 26. The hypermedia constraintA URI request returns all valid state changes
    27. 27. The hypermedia constraint It’s how the web works
    28. 28. The hypermedia constraintYou already understand it!
    29. 29. HTTP
    30. 30. HTTPYour browser speaks the language of text/html
    31. 31. <!DOCTYPE html>
    32. 32. HTMLUsing hypermedia on the web, you can link to different types of data
    33. 33. HTMLUsing hypermedia on the web, you can link to different types of data text/css image/png audio/mpeg
    34. 34. Hypermedia Types “Hypermedia Types are MIME media types that contain native hyper-linkingsemantics that induce application flow.” - Mike Amundsen (2010)
    35. 35. The hypermedia constraintThe client tells the server what language it speaks
    36. 36. The hypermedia constraintThe server tells the client what to do
    37. 37. HTTP/1.1
    38. 38. HTTP/1.1HEAD GET POST PUT DELETE TRACE OPTIONS CONNECT PATCH
    39. 39. GET and POST is OK
    40. 40. GET and POST is OKREST doesn’t care what methods you use
    41. 41. GET and POST is OKHTTP cares that the methods are used correctly
    42. 42. Life in XHTMLThe website is also the API
    43. 43. Life in XHTMLInconvenient and difficult to maintain
    44. 44. False start #1Attempt a custom hypermedia type
    45. 45. False start #2 application/atom+xml
    46. 46. False start #3 application/xhtml+xml
    47. 47. Custom hypermedia types Consider registered types first
    48. 48. Custom hypermedia types More likely to be understood
    49. 49. A solutionapplication/vnd.fdrop.xhtml+xml
    50. 50. A solutionXHTML has hypermedia controls built in
    51. 51. A solutionContains most of what I need
    52. 52. XML based media types Namespaces
    53. 53. Let’s see it then! @RESTConsole
    54. 54. What about JSON?application/vnd.collection+json application/hal+json
    55. 55. Versioning http://www.w3.org/History/19921103-hypertext/hypertext/WWW/TheProject.html
    56. 56. VersioningThe server makes no assumptions on the capabilities of the client
    57. 57. Versioning If the client does not supportsomething, it simply ignores it
    58. 58. VersioningThe web is not versioned
    59. 59. VersioningCorrect use of hypermedia means you canchange things without affecting clients
    60. 60. VersioningBut if you must...
    61. 61. VersioningAccept: application/vnd.fdrop.v2.xhtml+xml
    62. 62. http://tools.ietf.org/html/rfc2616http://tools.ietf.org/html/rfc4287http://amundsen.com/hypermedia/http://amundsen.com/media-types/collection/http://www.flickr.com/photos/bjornmeansbear/4727475559/
    63. 63. http://joind.in/talk/view/3589 @blongden

    ×