+ All Categories
Home > Technology > HATEOAS: The Confusing Bit from REST

HATEOAS: The Confusing Bit from REST

Date post: 05-Dec-2014
Category:
Upload: elliando-dias
View: 8,168 times
Download: 0 times
Share this document with a friend
Description:
 
45
HATEOAS: The Confusing Bit from REST Dr. Jim Webber http://jim.webber.name
Transcript
Page 1: HATEOAS: The Confusing Bit from REST

HATEOAS: The Confusing Bit from REST Dr. Jim Webber

http://jim.webber.name

Page 2: HATEOAS: The Confusing Bit from REST

whoami

•  PhD in parallel computing –  Speciality in programming language design(!)

•  {developer, architect, director} with ThoughtWorks

•  Author of “Developing Enterprise Web Services” –  And currently engaged in writing a book on

Web-as-middleware

Page 3: HATEOAS: The Confusing Bit from REST

Roadmap

•  The Richardson Maturity Model • Hypermedia Formats • HATEOAS – Hypermedia As The Engine Of

Application State •  Semantics •  A Simple RESTful Service •  Cheap sales-pitch for our book

Page 4: HATEOAS: The Confusing Bit from REST

The Richardson Maturity Model

•  Level 0 –  SOAP, XML RPC, POX –  Single URI

•  Level 1 –  URI Tunnelling –  Many URIs, Single verb

•  Level 2 –  Many URIs, many verbs –  CRUD services (e.g.

Amazon S3) •  Level 3

–  Level 2 + Hypermedia –  RESTful Services

Hypermedia

HTTP

URI

Page 5: HATEOAS: The Confusing Bit from REST

Why the Web? Why be RESTful?

•  Scalable •  Fault-tolerant •  Recoverable •  Secure •  Loosely coupled

•  Precisely the same characteristics we want in business software systems!

Page 6: HATEOAS: The Confusing Bit from REST

Media Types Rule!

•  The Web’s contracts are expressed in terms of media types –  If you know the type, you can process the

content

•  Some types are special because they work in harmony with the Web –  We call these “hypermedia formats”

Page 7: HATEOAS: The Confusing Bit from REST

Other Resource Representations

•  Remember, XML is not the only way a resource can be serialised –  Remember the Web is based on REpresentational State

Transfer •  The choice of representation is left to the implementer

–  Can be a standard registered media type –  Or something else

•  But there is a division on the Web between two families –  Hypermedia formats

•  Formats which host URIs and links –  Regular formats

•  Which don’t

Page 8: HATEOAS: The Confusing Bit from REST

Plain Old XML is not Hypermedia Friendly

HTTP/1.1 200 OK Content-Length: 227 Content-Type: application/xml Date: Wed, 19 Nov 2008 21:48:10 GMT

<order xmlns="http://schemas.restbucks.com/order"> <location>takeAway</location> <items> <item>

<name>latte</name> <quantity>1</quantity> <milk>whole</milk> <size>small</size> </item> </items> <status>pending</pending> </order>

Where are the links? Where’s the protocol?

Page 9: HATEOAS: The Confusing Bit from REST

So what?

• How do you know the next thing to do? • How do you know the resources you’re

meant to interact with next? •  In short, how do you know the service’s

protocol? –  Turn to WADL? Yuck! –  Read the documentation? Come on! –  URI Templates? Tight Coupling!

Page 10: HATEOAS: The Confusing Bit from REST

URI Templates are NOT a Hypermedia Substitute

•  Often URI templates are used to advertise all resources a service hosts –  Do we really need to advertise them all?

•  This is verbose •  This is out-of-band communication •  This encourages tight-coupling to resources through their URI

template •  This has the opportunity to cause trouble!

–  Knowledge of “deep” URIs is baked into consuming programs –  Services encapsulation is weak and consumers will program to

it –  Service will change its implementation and break consumers

Page 11: HATEOAS: The Confusing Bit from REST

application/xml is not the media type you’re looking for

•  Remember that HTTP is an application protocol –  Headers and representations are intertwined –  Headers set processing context for representations –  Unlike SOAP which can safely ignore HTTP headers

•  It has its own header model

•  Remember that application/xml has a particular processing model –  Which doesn’t include understanding the semantics of links

•  Remember if a representation is declared in the Content-Type header, you must treat it that way –  HTTP is an application protocol – did you forget already?

•  We need real hypermedia formats!

Page 12: HATEOAS: The Confusing Bit from REST

Hypermedia Formats

•  Standard –  Wide “reach” –  Software agents already know how to process

them –  But sometimes need to be shoe-horned

•  Self-created –  Can craft specifically for domain –  Semantically rich –  But lack reach

Page 13: HATEOAS: The Confusing Bit from REST

Two Common Hypermedia Formats: XHTML and ATOM

•  Both are commonplace today •  Both are hypermedia formats

–  They contain links

•  Both have a processing model that explicitly supports links

• Which means both can describe protocols…

Page 14: HATEOAS: The Confusing Bit from REST

application/vnd.restbucks+xml

•  What a mouthful! •  The vnd namespace is for proprietary media

types –  As opposed to the IANA-registered ones

•  Restbucks own XML is a hybrid –  We use plain old XML to convey information –  And link elements to convey protocol

•  This is important, since it allows us to create RESTful, hypermedia aware services

Page 15: HATEOAS: The Confusing Bit from REST

Interlude: Microformats

•  Microformats are an example of little “s” semantics

•  Innovation at the edges of the Web –  Not by some central design authority (e.g. W3C)

•  Started by embedding machine-processable elements in Web pages –  E.g. Calendar information, contact information,

etc –  Using existing HTML features like class, rel,

etc

Page 16: HATEOAS: The Confusing Bit from REST

Semantic versus semantic

•  Semantic Web is top-down –  Driven by the W3C with extensive array of technology,

standards, committees, etc –  Has not currently proven as scalable as the visionaries hoped

•  RDF tripples have been harvested and processed in private databases

•  Microformats are bottom-up –  Little formal organisation, no guarantee of interoperability –  Popular formats tend to be adopted (e.g. hCard) –  Easy to use and extend for our systems –  Trivial to integrate into current and future programmatic Web

systems

Page 17: HATEOAS: The Confusing Bit from REST

Microformats and Resources

•  Use Microformats to structure resources where formats exist –  I.e. Use hCard for contacts, hCalendar for data

•  Create your own formats (sparingly) in other places –  Annotating links is a good start –  <link rel="withdraw.cash" .../> –  <link rel="service.post" type="application/atom+xml" href="{post-uri}" title="some title">

•  The rel attribute describes the semantics of the referred resource

Page 18: HATEOAS: The Confusing Bit from REST

Revisiting Resource Lifetime

•  On the Web, the lifecycle of a single resource is more than: –  Creation –  Updating –  Reading –  Deleting

•  Can also get metadata –  About the resource –  About its (subset of) the verbs it understands

•  And as we see, resources tell us about other resources we might want to interact with…

Page 19: HATEOAS: The Confusing Bit from REST

Links

•  Connectedness is good in Web-based systems

•  Resource representations can contain other URIs

•  Links act as state transitions •  Application (conversation) state is

captured in terms of these states

Page 20: HATEOAS: The Confusing Bit from REST

Describing Contracts with Links

•  The value of the Web is its “linked-ness” –  Links on a Web page constitute a contractfor page

traversals •  The same is true of the programmatic Web •  Use Links to describe state transitions in

programmatic Web services –  By navigating resources you change application state

•  Hypermedia formats support this –  Allow us to describe higher-order protocols which sit

comfortably atop HTTP –  Hence application/vnd.restbucks+xml

Page 21: HATEOAS: The Confusing Bit from REST

Links are State Transitions

Page 22: HATEOAS: The Confusing Bit from REST

Links as APIs <confirm xmlns="...">

<link rel="payment" href="https://pay"

type="application/xml"/> <link rel="postpone"

href="https://wishlist" type="application/xml"/>

</confirm>

•  Following a link causes an action to occur

•  This is the start of a state machine!

•  Links lead to other resources which also have links

•  Can make this stronger with semantics –  Microformats

Page 23: HATEOAS: The Confusing Bit from REST

We have a framework!

•  The Web gives us a processing and metadata model –  Verbs and status codes –  Headers

•  Gives us metadata contracts or Web “APIs” –  URI Templates –  Links

•  Strengthened with semantics –  Little “s”

Page 24: HATEOAS: The Confusing Bit from REST

Workflow

•  How does a typical enterprise workflow look when it’s implemented in a Web-friendly way?

•  Let’s take Starbuck’s as an example, the happy path is: –  Make selection

• Add any specialities –  Pay –  Wait for a while –  Collect drink

Page 25: HATEOAS: The Confusing Bit from REST

Workflow and MOM •  With Web Services we

exchange messages with the service

•  Resource state is hidden from view

•  Conversation state is all we know –  Advertise it with

SSDL, BPEL •  Uniform interface,

roles defined by SOAP –  No “operations”

Page 26: HATEOAS: The Confusing Bit from REST

Hypermedia Describes Protocols!

•  Links declare next valid steps •  Following links and interacting with

resources changes application state •  Media types with links define contracts

–  Media type defines processing model –  Links (with microformats) describe state

transistions •  Don’t need a static contract description

–  No WSDL, no WADL •  This is HATEOAS!

Page 27: HATEOAS: The Confusing Bit from REST

Web-friendly Workflow

•  What happens if workflow stages are modelled as resources? •  And state transitions are modelled as hyperlinks or URI

templates? •  And events modelled by traversing links and changing

resource states? •  Answer: we get Web-friendly workflow

–  With all the quality of service provided by the Web

•  So let’s see how we order a coffee at Restbucks.com… –  This is written up on the Web:

•  http://www.infoq.com/articles/webber-rest-workflow

Page 28: HATEOAS: The Confusing Bit from REST

Placing an Order

•  Place your order by POSTing it to a well-known URI –  http://example.restbucks.com/order

Client

Star

buck

’s S

ervi

ce

Page 29: HATEOAS: The Confusing Bit from REST

Placing an Order: On the Wire

•  Request POST /order HTTP 1.1 Host: restbucks.com Content-Length: ...

<order xmlns="urn:restbucks"> <drink>latte</drink> </order>

•  Response 201 Created Location: http://restbucks.com/

order/1234 Content-Type: application/

vnd.restbucks+xml Content-Length: ...

<order xmlns="urn:restbucks"> <drink>latte</drink> <link rel="payment"

href="https://restbucks.com/payment/order/1234"

type="application/xml"/> </order>

A link! Is this the start of an API?

If we have a (private) microformat, this can become a neat API!

Page 30: HATEOAS: The Confusing Bit from REST

Whoops! A mistake

•  I like my coffee to taste like coffee! •  I need another shot of espresso

–  What are my OPTIONS?

 Request OPTIONS /order/1234 HTTP 1.1

Host: restbucks.com

 Response 200 OK

Allow: GET, PUT Phew! I can update my

order, for now

Page 31: HATEOAS: The Confusing Bit from REST

Optional: Look Before You Leap

•  See if the resource has changed since you submitted your order –  If you’re fast your drink hasn’t been

prepared yet

 Request PUT /order/1234 HTTP 1.1

Host: restbucks.com

Expect: 100-Continue

 Response 100 Continue

I can still PUT this resource, for now. (417 Expectation Failed otherwise)

Page 32: HATEOAS: The Confusing Bit from REST

Amending an Order

•  Add specialities to you order via PUT –  Restbucks needs 2 shots!

Client

Star

buck

’s S

ervi

ce

Page 33: HATEOAS: The Confusing Bit from REST

Amending an Order: On the Wire

•  Request PUT /order/1234 HTTP 1.1 Host: restbucks.com Content-Type: application/

vnd.restbucks+xml Content-Length: ...

<order xmlns="urn:restbucks"> <drink>latte</drink> <additions>shot</additions> <link rel="payment"

href="https://restbucks.com/payment/order/1234"

type="application/xml"/> </order>

•  Response 200 OK Location: http://restbucks.com/

order/1234 Content-Type: application/

vnd.restbucks+xml Content-Length: ...

<order xmlns="urn:restbucks"> <drink>latte</drink> <additions>shot</additions> <link rel="payment"

href="https://restbucks.com/payment/order/1234"

type="application/xml"/> </order>

Page 34: HATEOAS: The Confusing Bit from REST

Statelessness

•  Remember interactions with resources are stateless •  The resource “forgets” about you while you’re not

directly interacting with it •  Which means race conditions are possible •  Use If-Unmodified-Since on a timestamp to

make sure –  Or use If-Match and an ETag

•  You’ll get a 412 PreconditionFailed if you lost the race –  But you’ll avoid potentially putting the resource into

some inconsistent state

Page 35: HATEOAS: The Confusing Bit from REST

Warning: Don’t be Slow! •  Can only make changes until someone

actually makes your drink –  You’re safe if you use If-Unmodified-Since

or If-Match –  But resource state can change without you!

 Request PUT /order/1234 HTTP 1.1

Host: restbucks.com

...

 Response 409 Conflict

Too slow! Someone else has changed the state of my order

 Request OPTIONS /order/1234 HTTP 1.1

Host: restbucks.com

 Response Allow: GET

Page 36: HATEOAS: The Confusing Bit from REST

Order Confirmation

•  Check your order status by GETting it

Client

Star

buck

’s S

ervi

ce

Page 37: HATEOAS: The Confusing Bit from REST

Order Confirmation: On the Wire •  Request GET /order/1234 HTTP 1.1

Host: restbucks.com

•  Response 200 OK

Location: http://restbucks.com/order/1234

Content-Type: application/vnd.restbucks+xml

Content-Length: ...

<order xmlns="urn:restbucks"> <drink>latte</drink>

<additions>shot</additions>

<link rel="payment" href="https://restbucks.com/payment/order/1234"

type="application/xml"/>

</order>

Are they trying to tell me something with hypermedia?

Page 38: HATEOAS: The Confusing Bit from REST

Order Payment •  PUT your payment to the order resource

https://restbucks.com/payment/order/1234

Client

Star

buck

’s S

ervi

ce

New resource! https://restbucks.com/payment/order/1234

Page 39: HATEOAS: The Confusing Bit from REST

How did I know to PUT? •  The client knew the URI to PUT to from the link

–  PUT is also idempotent (can safely re-try) in case of failure •  Verified with OPTIONS

–  Just in case you were in any doubt

 Request OPTIONS /payment/order/1234 HTTP 1.1

Host: restbucks.com

 Response Allow: GET, PUT

Page 40: HATEOAS: The Confusing Bit from REST

Order Payment: On the Wire

•  Request PUT /payment/order/1234 HTTP 1.1 Host: restbucks.com Content-Type: application/xml Content-Length: ...

<payment xmlns="urn:restbucks"> <cardNo>123456789</cardNo> <expires>07/07</expires> <name>John Citizen</name> <amount>4.00</amount> </payment>

•  Response 201 Created Location: https://

restbucks.com/payment/order/1234

Content-Type: application/xml Content-Length: ...

<payment xmlns="urn:restbucks"> <cardNo>123456789</cardNo> <expires>07/07</expires> <name>John Citizen</name> <amount>4.00</amount> </payment>

Page 41: HATEOAS: The Confusing Bit from REST

Check that you’ve paid •  Request GET /order/1234 HTTP 1.1 Host: restbucks.com

•  Response 200 OK Content-Type: application/

vnd.restbucks+xml Content-Length: ...

<order xmlns="urn:restbucks"> <drink>latte</drink> <additions>shot</additions> </order>

My “API” has changed, because I’ve paid

enough now

Page 42: HATEOAS: The Confusing Bit from REST

Finally drink your coffee...

Source: http://images.businessweek.com/ss/06/07/top_brands/image/restbucks.jpg

Page 43: HATEOAS: The Confusing Bit from REST

Summary

•  Web-based services are about state machines, and business protocols –  The HATEOAS constraint from REST

•  If you don’t use hypermedia, you are NOT RESTful –  Which is OK! Good systems don’t always have to

be RESTful – e.g. Amazon S3 •  Use Web for massive scalability, fault

tolerance –  If you can tolerate higher latencies

Page 44: HATEOAS: The Confusing Bit from REST

Restbucks Written Up @ InfoQ

http://www.infoq.com/articles/webber-rest-workflow

Page 45: HATEOAS: The Confusing Bit from REST

Questions?

Blog: http://jim.webber.name

GET /Connected

Jim Webber Savas Parastatidis

Ian Robinson

Coming 2009…


Recommended