-
1.
REST and HATEOAS
Ben Longden, PHPNW 2011
-
2.
REST and the
Hypermedia Constraint
Ben Longden, PHPNW 2011
-
3.
Who are you?
-
4.
Who are you?
@blongden
-
5.
Who are you?
-
6.
Who are you?
@ibuildings
-
7.
Who are you?
-
8.
Who are you?
@fdrop
-
9.
REST
REpresentational State Transfer
-
10.
REST
Series of constraints for distributed
hypermedia systems - Roy Fielding, 2000
-
11.
Constraints are good
-
12.
Constraints are good
They (help) stop you from doing crazy
things
-
13.
REST is an Architecture
It is not a protocol
-
14.
Goals
Scalability
Generality of Interfaces
Independent deployment of components
Intermediaries to reduce latency,
enforce security, and encapsulate
legacy systems.
-
15.
Client server
-
16.
Stateless
-
17.
Cache
-
18.
Layered system
-
19.
Code on demand
-
20.
Uniform interface
-
21.
Uniform interface
Identification of resources
-
22.
Uniform interface
Manipulations of resources through
representations
-
23.
Uniform interface
Self descriptive messages
-
24.
Uniform interface
Hypertext as the engine of application
state
-
25.
The hypermedia
constraint?
-
26.
The hypermedia
constraint
A URI request returns all valid state
changes
-
27.
The hypermedia
constraint
It’s how the web works
-
28.
The hypermedia
constraint
You already understand it!
-
29.
HTTP
-
30.
HTTP
Your browser speaks the language of
text/html
-
31.
<!DOCTYPE html>
-
32.
HTML
Using hypermedia on the web, you can
link to different types of data
-
33.
HTML
Using hypermedia on the web, you can
link to different types of data
text/css image/png audio/mpeg
-
34.
Hypermedia Types
“Hypermedia Types are MIME media types
that contain native hyper-linking
semantics that induce application flow.”
- Mike Amundsen (2010)
-
35.
The hypermedia
constraint
The client tells the server what
language it speaks
-
36.
The hypermedia
constraint
The server tells the client what to do
-
37.
HTTP/1.1
-
38.
HTTP/1.1
HEAD GET POST PUT DELETE TRACE OPTIONS
CONNECT PATCH
-
39.
GET and POST is OK
-
40.
GET and POST is OK
REST doesn’t care what methods you use
-
41.
GET and POST is OK
HTTP cares that the methods are used
correctly
-
42.
Life in XHTML
The website is also the API
-
43.
Life in XHTML
Inconvenient and difficult to maintain
-
44.
False start #1
Attempt a custom hypermedia type
-
45.
False start #2
application/atom+xml
-
46.
False start #3
application/xhtml+xml
-
47.
Custom hypermedia types
Consider registered types first
-
48.
Custom hypermedia types
More likely to be understood
-
49.
A solution
application/vnd.fdrop.xhtml+xml
-
50.
A solution
XHTML has hypermedia controls built in
-
51.
A solution
Contains most of what I need
-
52.
XML based media
types
Namespaces
-
53.
Let’s see it then!
@RESTConsole
-
54.
What about JSON?
application/vnd.collection+json
application/hal+json
-
55.
Versioning
http://www.w3.org/History/19921103-
hypertext/hypertext/WWW/TheProject.html
-
56.
Versioning
The server makes no assumptions on the
capabilities of the client
-
57.
Versioning
If the client does not support
something, it simply ignores it
-
58.
Versioning
The web is not versioned
-
59.
Versioning
Correct use of hypermedia means you can
change things without affecting clients
-
60.
Versioning
But if you must...
-
61.
Versioning
Accept: application/vnd.fdrop.v2.xhtml+xml
-
62.
http://tools.ietf.org/html/rfc2616
http://tools.ietf.org/html/rfc4287
http://amundsen.com/hypermedia/
http://amundsen.com/media-types/collection/
http://www.flickr.com/photos/bjornmeansbear/4727475559/
-
63.
http://joind.in/talk/view/3589
@blongden
\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 &#x2018;why is it so difficult to send a file to someone online?&#x2019;\n
\n
\n
Doctoral Dissertation\n&#x2018;Architectural Styles and the Design of Network-based Software Architectures&#x2019;\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&#x2019;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&#x2019;s why it&#x2019;s been so successful over the last 20 years. The web is 20 years old. That&#x2019;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&#x2019;s great for the web as browsers only need to understand GET and POST - this is fine and it&#x2019;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&#x2019;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&#x2019;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