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.
36. HTML
Using hypermedia on the web, you can
link to different types of data
text/css image/png audio/mpeg
37. Hypermedia Types
“Hypermedia Types are MIME media types
that contain native hyper-linking
semantics that induce application flow.”
- Mike Amundsen (2010)
38. The hypermedia
constraint
The client tells the server what
language it speaks
39. The hypermedia
constraint
The server tells the client what to do
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