SlideShare ist ein Scribd-Unternehmen logo
1 von 56
Downloaden Sie, um offline zu lesen
HATEOAS:
The Confusing Part of REST   Dr. Jim Webber
                              Web: http://jim.webber.name
                                  Twitter: @jimwebber
Roadmap
• The Richardson Maturity Model
• Hypermedia Formats
• HATEOAS – Hypermedia As The Engine Of
  Application State
• RESTful business protocols
The Richardson Maturity Model
• Level 0
   – SOAP, XML RPC, POX
   – Single URI
                              Hypermedia
• Level 1
   – URI Tunnelling
   – Many URIs, Single verb
• Level 2                       HTTP
   – Many URIs, many verbs
   – CRUD services (e.g.
     Amazon S3)
• Level 3                        URI
   – Level 2 + Hypermedia
   – RESTful Services
Why the Web? Why be RESTful?
•   Scalable
•   Fault-tolerant
•   Recoverable
•   Secure
•   Loosely coupled

• Precisely the same characteristics we
  want in business software systems!
Media Types Rule!
• The Web’s contracts are expressed in
  terms of media types and link relations
  – 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”
(Hyper) media types                        General
Standardised media type


Processing model
                          Compose application-
                          specific behaviours on
   Hypermedia controls
    (links and forms)     top of the handling of
                          standardised media
   Supported operations   types
  (methods, headers and
      status codes)


 Representation formats
 (may include schemas)


                                            Specific
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>
                                   Where are the links?
                                  Where’s the protocol?
      <quantity>1</quantity>
      <milk>whole</milk>
      <size>small</size>
    </item>
  </items>
  <status>pending</status>
</order>
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!
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
Bad Ideas with URI Templates
• Imagine we’re created an order, what next?
• We could share this URI template:
  – http://restbucks.com/payment/{order_id}
• The order_id field should match the order ID
  that came from the restbucks service
  – Sounds great!
• But what if Restbucks outsources payment?
  – Change the URI for payments, break the template,
    break consumers!
  – D’oh!
• Be careful what you share!
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…
What’s the big deal with XHTML?
 • It does two interesting things:
     1. It gives us document structure
     2. It gives us links
 •    So?
     1. We can understand the format of those resources
     2. We can discover other resources!
 •    How?
     1. Follow the links!
     2. Encode the resource representation as “normal” XML in
        your XHTML documents
 •    Contrast this with the Atom and APP
      approach...similar!
XHTML in Action
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
 Business class="order">
   <div
   data <p class="location">takeAway</p>
           <ul class="items">
             <li class="item">
               <p class="name">latte</p>
               <p class="quantity">1</p>     “Hypermedia
               <p class="milk">whole</p>       Control”
               <p class="size">small</p>
             </li>
           </ul>
           <a href="http://restbucks.com/payment/1234"
             rel="payment">payment</a>
        </div>
  </body>
</html>
Atom Syndication Format
• The                     HTTP/1.1 200 OK
                          Content-Length: 342
  application/atom+xm     Content-Type: application/atom+xml
                          Date: Sun, 22 Mar 2009 17:04:10 GMT
  l media type is
  hypermedia aware        <entry
                             xmlns="http://www.w3.org/2005/Ato
                             m">
• You should expect         <title>Order 1234</title>

  links when processing     <link rel="payment"
                             href="http://restbucks.com/paymen
  such representations       t/1234"/>
                            <link rel="special-offer"

• And be prepared to
                             href="http://restbucks.com/offers
                             /freeCookie"/>

  do things with them!       <id>http://restbucks.com/order/12
                             34</id>
                            <updated>2009-03-
                             22T16:57:02Z</updated>
                            <summary>1x Cafe Latte</summary>
                          </entry>
application/atom+xml
• The rel carries semantics
• Atom is a hypermedia format
  – Both feeds and entries contains hypermedia
    controls that can describe protocols
• And it has reach
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
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
<drink>
           Anatomy of a link
   <name>latte</name>
   <milk>whole</milk>
   <size>small</size>
   <link
     href="http://restbucks.com/payment/1234"
     rel="http://relations.restbucks.com/payment"
     type="application/vnd.restbucks+xml"/>
 </drink>


Address           Information processing
WHERE does the       WHAT form does the
linked resource      linked resource take?
reside?                                      Semantic context
                                             WHY access the
                                             linked resource?
Projecting Contracts with Links
• The value of the Web is its “linked-ness”
  – Links on a Web page constitute a contractfor
    page traversals
• Use Links to describe state transitions in
  programmatic Web services
• Hypermedia support links
  – Allow us to describe higher-order protocols
    which sit comfortably atop HTTP
  – Hence application/vnd.restbucks+xml
Links advertise State Transitions
Richardson Model Level 3
• Lots of URIs that
  address resources
• Embraces HTTP as an
  application protocol
• Hypermedia-friendly
  resource
  representations
Workflow and MOM
• With Web Services we
  exchange messages
  with the service          Order Drink
• Resource state is




                                               Starbuck’s Service
  hidden from view        Add Specialities
• Conversation state is
  all we know             Order Confirmation
  – Advertise it with
    SSDL, BPEL                   Pay
• Uniform interface,
  roles defined by SOAP          Coffee!
  – No “operations”
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 transitions
 • Don’t need a static contract description
   – No WSDL, no WADL
 • This is HATEOAS!
 • So let’s see how we order a coffee at
   Restbucks.com
Static Interface and State Transitions
Place an Order
• POST to a well-known URI
  – http://restbucks.com/order




                                 Restbucks Service
     Client
Placing an Order
• Request
POST /order HTTP/1.1
Content-Type: application/vnd.restbucks+xml
Accept: application/vnd.restbucks+xml
Host: restbucks.com
Connection: keep-alive
Content-Length: 283

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:order xmlns:ns2="http://schemas.restbucks.com">
  <ns2:item>
    <ns2:milk>semi</ns2:milk>
    <ns2:size>large</ns2:size>
    <ns2:drink>latte</ns2:drink>
  </ns2:item>
  <ns2:location>takeaway</ns2:location>
</ns2:order>
Confirm the Order
• GET from the rel=“self” URI
  – http://restbucks.com/order/1234




                                      Restbucks Service
      Client
Confirm the Order
• Request
GET /order/1234 HTTP/1.1
Accept: application/vnd.restbucks+xml
Host: restbucks.com
Connection: keep-alive
Confirm the Order
• Response
HTTP/1.1 200 OK
Location: http://restbucks.com/order/1234
Content-Type: application/vnd.restbucks+xml
Content-Length: 911
Date: Wed, 03 Mar 2010 20:58:02 GMT

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:order xmlns:ns2="http://schemas.restbucks.com" xmlns:ns3="http://schemas.restbucks.com/dap">
  <ns3:link mediaType="application/vnd.restbucks+xml" uri="http://restbucks.com/order/1234"
     rel="http://relations.restbucks.com/cancel"/>
  <ns3:link mediaType="application/vnd.restbucks+xml" uri="http://restbucks.com/payment/1234"
     rel="http://relations.restbucks.com/payment"/>
  <ns3:link mediaType="application/vnd.restbucks+xml" uri="http://restbucks.com/order/1234"
     rel="http://relations.restbucks.com/update"/>
  <ns3:link mediaType="application/vnd.restbucks+xml" uri="http://restbucks.com/order/1234" rel="self"/>
  <ns2:item>
    <ns2:milk>semi</ns2:milk>
    <ns2:size>large</ns2:size>
    <ns2:drink>latte</ns2:drink>
  </ns2:item>
  <ns2:location>takeaway</ns2:location>
  <ns2:cost>2.0</ns2:cost>
  <ns2:status>unpaid</ns2:status>
</ns2:order>
Change the Order
• POST new order to service-generated
  URI
  – http://restbucks.com/order/1234




                                      Restbucks Service
      Client
POST? Not PUT?
• PUT expects the whole resource state to be
  presented in the request
  – But our clients aren’t responsible for generating
    cost or status elements
• PATCH would be better
  – It allows us to send diffs, but is new to the
    family of HTTP verbs and not widely supported
    yet
• So we use POST
  – Could use more fine-grained resources with PUT
Change the Order
• Request
POST order/1234 HTTP/1.1
Content-Type: application/vnd.restbucks+xml
Accept: application/vnd.restbucks+xml
Host: restbucks.com
Connection: keep-alive
Content-Length: 288

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:order xmlns:ns2="http://schemas.restbucks.com">
  <ns2:item>
    <ns2:milk>semi</ns2:milk>
                                                   No service-
    <ns2:size>large</ns2:size>
                                                   generated
    <ns2:drink>cappuccino</ns2:drink>
                                                    content
  </ns2:item>
  <ns2:location>takeaway</ns2:location>
</ns2:order>
Change the Order
• Response
HTTP/1.1 200 OK
Location: http://restbucks.com/order/1234
Content-Type: application/vnd.restbucks+xml
Content-Length: 916
Date: Sun, 06 Sep 2009 06:52:22 GMT

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:order xmlns:ns2="http://schemas.restbucks.com"
    xmlns:ns3="http://schemas.restbucks.com/dap">
  <ns3:link mediaType="application/vnd.restbucks+xml" uri="http://restbucks.com/order/1234"
    rel="http://relations.restbucks.com/cancel"/>
  <ns3:link mediaType="application/vnd.restbucks+xml" uri="http://restbucks.com/payment/1234"
    rel="http://relations.restbucks.com/payment"/>
  <ns3:link mediaType="application/vnd.restbucks+xml" uri="http://restbucks.com/order/1234"
    rel="http://relations.restbucks.com/update"/>
  <ns3:link mediaType="application/vnd.restbucks+xml" uri="http://restbucks.com/order/1234"
    rel="self"/>
  <ns2:item>
    <ns2:milk>semi</ns2:milk>
    <ns2:size>large</ns2:size>
    <ns2:drink>cappuccino</ns2:drink>
  </ns2:item>
  <ns2:location>takeaway</ns2:location>
  <ns2:cost>2.0</ns2:cost>
  <ns2:status>unpaid</ns2:status>
</ns2:order>
Statelessness
• Interactions with resources are stateless
  – The resource “forgets” about you while you’re not
    directly interacting with it
• 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 putting the resource into some
    inconsistent state
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
                              Response
PUT /order/1234 HTTP 1.1
                               409 Conflict
Host: restbucks.com
...                                   Too slow! Someone else has
                                     changed the state of my order
 Request                       Response
OPTIONS /order/1234 HTTP 1.1   Allow: GET
Host: restbucks.com
What if we want to cancel?
• DELETE order at rel=“self” URI
  – http://restbucks.com/order/1234




                                      Restbucks Service
      Client
What if we want to cancel?
• Request
DELETE /order/1234 HTTP/1.1
Host: restbucks.com
Connection: keep-alive



• Response
HTTP/1.1 200 OK
Content-Type: application/vnd.restbucks+xml
Date: Sun, 06 Sep 2009 06:53:22 GMT
Order Payment
• PUT to service-generated payment URI
  –   rel="http://relations.restbucks.com/payment"
  – http://restbucks.com/payment/1234




                                                 Restbucks Service
        Client
Why PUT this time?
• It’s idempotent
  – Safe to repeat in the event of failures
• For all $ transactions, prefer idempotent
  verbs
Order Payment
• Request
PUT /payment/1234 HTTP/1.1
Content-Type: application/vnd.restbucks+xml
Accept: application/vnd.restbucks+xml
Host: restbucks.com
Connection: keep-alive
Content-Length: 287

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<payment xmlns="http://schemas.restbucks.com">
  <amount>2.0</amount>
  <cardholderName>Michael Farraday</cardholderName>
  <cardNumber>11223344</cardNumber>
  <expiryMonth>12</expiryMonth>
  <expiryYear>12</expiryYear>
</payment>
Order Payment
• Response
HTTP/1.1 201 Created
server: grizzly/1.8.1
Location: http://restbucks.com/payment/1234
Content-Type: application/vnd.restbucks+xml
Content-Length: 650
Date: Wed, 03 Mar 2010 20:58:03 GMT

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:payment xmlns="http://schemas.restbucks.com/dap"
   xmlns:ns2="http://schemas.restbucks.com">
  <link mediaType="application/vnd.restbucks+xml"
   uri="http://restbucks.com/order/1234"
   rel="http://relations.restbucks.com/order"/>
  <link mediaType="application/vnd.restbucks+xml"
   uri="http://restbucks.com/receipt/1234"
   rel="http://relations.restbucks.com/receipt"/>
  <ns2:amount>2.0</ns2:amount>
  <ns2:cardholderName>Michael Farraday</ns2:cardholderName>
  <ns2:cardNumber>11223344</ns2:cardNumber>
  <ns2:expiryMonth>12</ns2:expiryMonth>
  <ns2:expiryYear>12</ns2:expiryYear>
</ns2:payment>
What if we want to cancel now?
• Request
DELETE /order/1234 HTTP/1.1
Host: restbucks.com
Connection: keep-alive



• Response
HTTP/1.1 405 Method Not Allowed
Allow: GET
Content-Type: application/vnd.restbucks+xml
Content-Length: 0
Date: Sun, 06 Sep 2009 07:43:29 GMT
Grab a Receipt
• GET service-generated receipt URI
  – http://restbucks.com/receipt/1234




                                        Restbucks Service
      Client
Grab a Receipt
• Request
GET /receipt/1234 HTTP/1.1
Accept: application/vnd.restbucks+xml
Host: restbucks.com
Grab a Receipt
• Request
HTTP/1.1 200 OK
server: grizzly/1.8.1
Content-Type: application/vnd.restbucks+xml
Content-Length: 384
Date: Wed, 03 Mar 2010 20:58:03 GMT

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:receipt xmlns="http://schemas.restbucks.com/dap"
   xmlns:ns2="http://schemas.restbucks.com">
  <link mediaType="application/vnd.restbucks+xml"
   uri="http://restbucks.com/order/1234"
   rel="http://relations.restbucks.com/order"/>
  <ns2:amount>2.0</ns2:amount>
  <ns2:paid>2010-03-03T21:58:03.834+01:00</ns2:paid>
</ns2:receipt>
Poll until the order’s ready
• GET from the rel=“http://relations.restbucks.com/order” URI
   – http://restbucks.com/order/1234




                                           Restbucks Service
       Client
Polling for a ready order on the wire

 • Request
 GET /order/1234 HTTP/1.1
 Host: restbucks.com
 Connection: keep-alive
Order’s ready, take the receipt and
            walk away
• Response
HTTP/1.1 200 OK
server: grizzly/1.8.1
Content-Type: application/vnd.restbucks+xml
Content-Length: 534
Date: Wed, 03 Mar 2010 20:58:03 GMT

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:order xmlns:ns2="http://schemas.restbucks.com"
   xmlns:ns3="http://schemas.restbucks.com/dap">
  <ns3:link mediaType="application/vnd.restbucks+xml"
   uri="http://restbucks.com/receipt/1234"
   rel="http://relations.restbucks.com/reciept"/>
  <ns2:item>
    <ns2:milk>semi</ns2:milk>
    <ns2:size>large</ns2:size>
    <ns2:drink>cappuccino</ns2:drink>
  </ns2:item>
  <ns2:location>takeaway</ns2:location>
  <ns2:cost>2.0</ns2:cost>
  <ns2:status>ready</ns2:status>
</ns2:order>
Complete the Order
• DELETE order at rel=“http://relations.restbucks.com/receipt”
  URI
   – http://restbucks.com/receipt/1234




                                            Restbucks Service
        Client
Complete the Order
• Request
DELETE/receipt/1234 HTTP/1.1
Host: restbucks.com
Connection: keep-alive
Complete the Order
• Response
HTTP/1.1 200 OK
server: grizzly/1.8.1
Content-Type: application/xml
Content-Length: 393
Date: Wed, 03 Mar 2010 20:58:03 GMT

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:order xmlns:ns2="http://schemas.restbucks.com"
   xmlns:ns3="http://schemas.restbucks.com/dap">
  <ns2:item>
    <ns2:milk>semi</ns2:milk>
    <ns2:size>large</ns2:size>
    <ns2:drink>cappuccino</ns2:drink>
  </ns2:item>                                       No Hypermedia
  <ns2:location>takeaway</ns2:location>                 Controls
  <ns2:cost>2.0</ns2:cost>
  <ns2:status>taken</ns2:status>
</ns2:order>
Source: http://images.businessweek.com/ss/06/07/top_brands/image/starbucks.jpg
What did we learn from Restbucks?
  • HTTP has a header/status combination
    for every occasion
  • APIs are expressed in terms of links
  • Hypermedia formats are important
  • State machines (defined by links) are
    important
    – Just as in Web Services…
Restbucks Written Up @ InfoQ




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

                  Blog:
        http://jim.webber.name
                Twitter:
              @jimwebber


             Now in Safari Rough Cuts

Weitere ähnliche Inhalte

Ähnlich wie Jimwebber rest

HATEOAS: The Confusing Bit from REST
HATEOAS: The Confusing Bit from RESTHATEOAS: The Confusing Bit from REST
HATEOAS: The Confusing Bit from RESTelliando dias
 
RESTful APIs
RESTful APIsRESTful APIs
RESTful APIsAdi Challa
 
Doing REST Right
Doing REST RightDoing REST Right
Doing REST RightKerry Buckley
 
REST Methodologies
REST MethodologiesREST Methodologies
REST Methodologiesjrodbx
 
REST and the Hypermedia Constraint
REST and the Hypermedia ConstraintREST and the Hypermedia Constraint
REST and the Hypermedia ConstraintInviqa
 
Microformats: Web Semantics & More
Microformats: Web Semantics & MoreMicroformats: Web Semantics & More
Microformats: Web Semantics & MoreEmily Lewis
 
An Overview of Web Services: SOAP and REST
An Overview of Web Services: SOAP and REST An Overview of Web Services: SOAP and REST
An Overview of Web Services: SOAP and REST Ram Awadh Prasad, PMP
 
Restful web-services
Restful web-servicesRestful web-services
Restful web-servicesrporwal
 
Moulding your enterprise with ROA
Moulding your enterprise with ROAMoulding your enterprise with ROA
Moulding your enterprise with ROAshirok
 
Web2 0 Incredibles
Web2 0 IncrediblesWeb2 0 Incredibles
Web2 0 Incrediblesanjeshdubey
 
Web-Services!.pptx
Web-Services!.pptxWeb-Services!.pptx
Web-Services!.pptxssuserae0316
 
Role of Rest vs. Web Services and EI
Role of Rest vs. Web Services and EIRole of Rest vs. Web Services and EI
Role of Rest vs. Web Services and EIWSO2
 
Web Services - A brief overview
Web Services -  A brief overviewWeb Services -  A brief overview
Web Services - A brief overviewRaveendra Bhat
 
REST and REST-fulness
REST and REST-fulnessREST and REST-fulness
REST and REST-fulnessDavid Waite
 
Restful webservice
Restful webserviceRestful webservice
Restful webserviceDong Ngoc
 
REST Introduction.ppt
REST Introduction.pptREST Introduction.ppt
REST Introduction.pptKGSCSEPSGCT
 

Ähnlich wie Jimwebber rest (20)

HATEOAS: The Confusing Bit from REST
HATEOAS: The Confusing Bit from RESTHATEOAS: The Confusing Bit from REST
HATEOAS: The Confusing Bit from REST
 
RESTful APIs
RESTful APIsRESTful APIs
RESTful APIs
 
Overview of java web services
Overview of java web servicesOverview of java web services
Overview of java web services
 
Mini-Training: Let's have a rest
Mini-Training: Let's have a restMini-Training: Let's have a rest
Mini-Training: Let's have a rest
 
Doing REST Right
Doing REST RightDoing REST Right
Doing REST Right
 
REST Methodologies
REST MethodologiesREST Methodologies
REST Methodologies
 
REST and the Hypermedia Constraint
REST and the Hypermedia ConstraintREST and the Hypermedia Constraint
REST and the Hypermedia Constraint
 
Microformats: Web Semantics & More
Microformats: Web Semantics & MoreMicroformats: Web Semantics & More
Microformats: Web Semantics & More
 
An Overview of Web Services: SOAP and REST
An Overview of Web Services: SOAP and REST An Overview of Web Services: SOAP and REST
An Overview of Web Services: SOAP and REST
 
Restful web-services
Restful web-servicesRestful web-services
Restful web-services
 
Moulding your enterprise with ROA
Moulding your enterprise with ROAMoulding your enterprise with ROA
Moulding your enterprise with ROA
 
Rest assured
Rest assuredRest assured
Rest assured
 
Web2 0 Incredibles
Web2 0 IncrediblesWeb2 0 Incredibles
Web2 0 Incredibles
 
Web-Services!.pptx
Web-Services!.pptxWeb-Services!.pptx
Web-Services!.pptx
 
Role of Rest vs. Web Services and EI
Role of Rest vs. Web Services and EIRole of Rest vs. Web Services and EI
Role of Rest vs. Web Services and EI
 
REST & RESTful Web Services
REST & RESTful Web ServicesREST & RESTful Web Services
REST & RESTful Web Services
 
Web Services - A brief overview
Web Services -  A brief overviewWeb Services -  A brief overview
Web Services - A brief overview
 
REST and REST-fulness
REST and REST-fulnessREST and REST-fulness
REST and REST-fulness
 
Restful webservice
Restful webserviceRestful webservice
Restful webservice
 
REST Introduction.ppt
REST Introduction.pptREST Introduction.ppt
REST Introduction.ppt
 

Mehr von d0nn9n

腾讯大讲堂:62 拇指下的精彩(手机qq交互设计经验分享)
腾讯大讲堂:62 拇指下的精彩(手机qq交互设计经验分享)腾讯大讲堂:62 拇指下的精彩(手机qq交互设计经验分享)
腾讯大讲堂:62 拇指下的精彩(手机qq交互设计经验分享)d0nn9n
 
腾讯大讲堂:55 企业法律风险防范
腾讯大讲堂:55 企业法律风险防范腾讯大讲堂:55 企业法律风险防范
腾讯大讲堂:55 企业法律风险防范d0nn9n
 
腾讯大讲堂:56 qzone安全之路
腾讯大讲堂:56 qzone安全之路腾讯大讲堂:56 qzone安全之路
腾讯大讲堂:56 qzone安全之路d0nn9n
 
腾讯大讲堂:59 数据蕴含商机,挖掘决胜千里
腾讯大讲堂:59 数据蕴含商机,挖掘决胜千里腾讯大讲堂:59 数据蕴含商机,挖掘决胜千里
腾讯大讲堂:59 数据蕴含商机,挖掘决胜千里d0nn9n
 
腾讯大讲堂:57 超级qq的千万之路
腾讯大讲堂:57 超级qq的千万之路 腾讯大讲堂:57 超级qq的千万之路
腾讯大讲堂:57 超级qq的千万之路 d0nn9n
 
蔡学镛 Rebol漫谈
蔡学镛   Rebol漫谈蔡学镛   Rebol漫谈
蔡学镛 Rebol漫谈d0nn9n
 
赵泽欣 - 淘宝网前端应用与发展
赵泽欣 - 淘宝网前端应用与发展赵泽欣 - 淘宝网前端应用与发展
赵泽欣 - 淘宝网前端应用与发展d0nn9n
 
Yanggang wps
Yanggang wpsYanggang wps
Yanggang wpsd0nn9n
 
熊节 - 软件工厂的精益之路
熊节 - 软件工厂的精益之路熊节 - 软件工厂的精益之路
熊节 - 软件工厂的精益之路d0nn9n
 
谢恩伟 - 微软在云端
谢恩伟 - 微软在云端谢恩伟 - 微软在云端
谢恩伟 - 微软在云端d0nn9n
 
去哪儿平台技术
去哪儿平台技术去哪儿平台技术
去哪儿平台技术d0nn9n
 
吴磊 - Silverlight企业级RIA
吴磊 - Silverlight企业级RIA吴磊 - Silverlight企业级RIA
吴磊 - Silverlight企业级RIAd0nn9n
 
Tom - Scrum
Tom - ScrumTom - Scrum
Tom - Scrumd0nn9n
 
Tim - FSharp
Tim - FSharpTim - FSharp
Tim - FSharpd0nn9n
 
Tiger oracle
Tiger oracleTiger oracle
Tiger oracled0nn9n
 
Paulking groovy
Paulking groovyPaulking groovy
Paulking groovyd0nn9n
 
Paulking dlp
Paulking dlpPaulking dlp
Paulking dlpd0nn9n
 
Patrick jcp
Patrick jcpPatrick jcp
Patrick jcpd0nn9n
 
Nick twitter
Nick twitterNick twitter
Nick twitterd0nn9n
 
Marc facebook
Marc facebookMarc facebook
Marc facebookd0nn9n
 

Mehr von d0nn9n (20)

腾讯大讲堂:62 拇指下的精彩(手机qq交互设计经验分享)
腾讯大讲堂:62 拇指下的精彩(手机qq交互设计经验分享)腾讯大讲堂:62 拇指下的精彩(手机qq交互设计经验分享)
腾讯大讲堂:62 拇指下的精彩(手机qq交互设计经验分享)
 
腾讯大讲堂:55 企业法律风险防范
腾讯大讲堂:55 企业法律风险防范腾讯大讲堂:55 企业法律风险防范
腾讯大讲堂:55 企业法律风险防范
 
腾讯大讲堂:56 qzone安全之路
腾讯大讲堂:56 qzone安全之路腾讯大讲堂:56 qzone安全之路
腾讯大讲堂:56 qzone安全之路
 
腾讯大讲堂:59 数据蕴含商机,挖掘决胜千里
腾讯大讲堂:59 数据蕴含商机,挖掘决胜千里腾讯大讲堂:59 数据蕴含商机,挖掘决胜千里
腾讯大讲堂:59 数据蕴含商机,挖掘决胜千里
 
腾讯大讲堂:57 超级qq的千万之路
腾讯大讲堂:57 超级qq的千万之路 腾讯大讲堂:57 超级qq的千万之路
腾讯大讲堂:57 超级qq的千万之路
 
蔡学镛 Rebol漫谈
蔡学镛   Rebol漫谈蔡学镛   Rebol漫谈
蔡学镛 Rebol漫谈
 
赵泽欣 - 淘宝网前端应用与发展
赵泽欣 - 淘宝网前端应用与发展赵泽欣 - 淘宝网前端应用与发展
赵泽欣 - 淘宝网前端应用与发展
 
Yanggang wps
Yanggang wpsYanggang wps
Yanggang wps
 
熊节 - 软件工厂的精益之路
熊节 - 软件工厂的精益之路熊节 - 软件工厂的精益之路
熊节 - 软件工厂的精益之路
 
谢恩伟 - 微软在云端
谢恩伟 - 微软在云端谢恩伟 - 微软在云端
谢恩伟 - 微软在云端
 
去哪儿平台技术
去哪儿平台技术去哪儿平台技术
去哪儿平台技术
 
吴磊 - Silverlight企业级RIA
吴磊 - Silverlight企业级RIA吴磊 - Silverlight企业级RIA
吴磊 - Silverlight企业级RIA
 
Tom - Scrum
Tom - ScrumTom - Scrum
Tom - Scrum
 
Tim - FSharp
Tim - FSharpTim - FSharp
Tim - FSharp
 
Tiger oracle
Tiger oracleTiger oracle
Tiger oracle
 
Paulking groovy
Paulking groovyPaulking groovy
Paulking groovy
 
Paulking dlp
Paulking dlpPaulking dlp
Paulking dlp
 
Patrick jcp
Patrick jcpPatrick jcp
Patrick jcp
 
Nick twitter
Nick twitterNick twitter
Nick twitter
 
Marc facebook
Marc facebookMarc facebook
Marc facebook
 

KĂźrzlich hochgeladen

Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 

KĂźrzlich hochgeladen (20)

Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 

Jimwebber rest

  • 1. HATEOAS: The Confusing Part of REST Dr. Jim Webber Web: http://jim.webber.name Twitter: @jimwebber
  • 2. Roadmap • The Richardson Maturity Model • Hypermedia Formats • HATEOAS – Hypermedia As The Engine Of Application State • RESTful business protocols
  • 3. The Richardson Maturity Model • Level 0 – SOAP, XML RPC, POX – Single URI Hypermedia • Level 1 – URI Tunnelling – Many URIs, Single verb • Level 2 HTTP – Many URIs, many verbs – CRUD services (e.g. Amazon S3) • Level 3 URI – Level 2 + Hypermedia – RESTful Services
  • 4. Why the Web? Why be RESTful? • Scalable • Fault-tolerant • Recoverable • Secure • Loosely coupled • Precisely the same characteristics we want in business software systems!
  • 5. Media Types Rule! • The Web’s contracts are expressed in terms of media types and link relations – 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”
  • 6. (Hyper) media types General Standardised media type Processing model Compose application- specific behaviours on Hypermedia controls (links and forms) top of the handling of standardised media Supported operations types (methods, headers and status codes) Representation formats (may include schemas) Specific
  • 7. 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> Where are the links? Where’s the protocol? <quantity>1</quantity> <milk>whole</milk> <size>small</size> </item> </items> <status>pending</status> </order>
  • 8. 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!
  • 9. 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
  • 10. Bad Ideas with URI Templates • Imagine we’re created an order, what next? • We could share this URI template: – http://restbucks.com/payment/{order_id} • The order_id field should match the order ID that came from the restbucks service – Sounds great! • But what if Restbucks outsources payment? – Change the URI for payments, break the template, break consumers! – D’oh! • Be careful what you share!
  • 11. 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…
  • 12. What’s the big deal with XHTML? • It does two interesting things: 1. It gives us document structure 2. It gives us links • So? 1. We can understand the format of those resources 2. We can discover other resources! • How? 1. Follow the links! 2. Encode the resource representation as “normal” XML in your XHTML documents • Contrast this with the Atom and APP approach...similar!
  • 13. XHTML in Action <html xmlns="http://www.w3.org/1999/xhtml"> <body> Business class="order"> <div data <p class="location">takeAway</p> <ul class="items"> <li class="item"> <p class="name">latte</p> <p class="quantity">1</p> “Hypermedia <p class="milk">whole</p> Control” <p class="size">small</p> </li> </ul> <a href="http://restbucks.com/payment/1234" rel="payment">payment</a> </div> </body> </html>
  • 14. Atom Syndication Format • The HTTP/1.1 200 OK Content-Length: 342 application/atom+xm Content-Type: application/atom+xml Date: Sun, 22 Mar 2009 17:04:10 GMT l media type is hypermedia aware <entry xmlns="http://www.w3.org/2005/Ato m"> • You should expect <title>Order 1234</title> links when processing <link rel="payment" href="http://restbucks.com/paymen such representations t/1234"/> <link rel="special-offer" • And be prepared to href="http://restbucks.com/offers /freeCookie"/> do things with them! <id>http://restbucks.com/order/12 34</id> <updated>2009-03- 22T16:57:02Z</updated> <summary>1x Cafe Latte</summary> </entry>
  • 15. application/atom+xml • The rel carries semantics • Atom is a hypermedia format – Both feeds and entries contains hypermedia controls that can describe protocols • And it has reach
  • 16. 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
  • 17. 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
  • 18. <drink> Anatomy of a link <name>latte</name> <milk>whole</milk> <size>small</size> <link href="http://restbucks.com/payment/1234" rel="http://relations.restbucks.com/payment" type="application/vnd.restbucks+xml"/> </drink> Address Information processing WHERE does the WHAT form does the linked resource linked resource take? reside? Semantic context WHY access the linked resource?
  • 19. Projecting Contracts with Links • The value of the Web is its “linked-ness” – Links on a Web page constitute a contractfor page traversals • Use Links to describe state transitions in programmatic Web services • Hypermedia support links – Allow us to describe higher-order protocols which sit comfortably atop HTTP – Hence application/vnd.restbucks+xml
  • 20. Links advertise State Transitions
  • 21.
  • 22. Richardson Model Level 3 • Lots of URIs that address resources • Embraces HTTP as an application protocol • Hypermedia-friendly resource representations
  • 23. Workflow and MOM • With Web Services we exchange messages with the service Order Drink • Resource state is Starbuck’s Service hidden from view Add Specialities • Conversation state is all we know Order Confirmation – Advertise it with SSDL, BPEL Pay • Uniform interface, roles defined by SOAP Coffee! – No “operations”
  • 24. 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 transitions • Don’t need a static contract description – No WSDL, no WADL • This is HATEOAS! • So let’s see how we order a coffee at Restbucks.com
  • 25. Static Interface and State Transitions
  • 26. Place an Order • POST to a well-known URI – http://restbucks.com/order Restbucks Service Client
  • 27. Placing an Order • Request POST /order HTTP/1.1 Content-Type: application/vnd.restbucks+xml Accept: application/vnd.restbucks+xml Host: restbucks.com Connection: keep-alive Content-Length: 283 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ns2:order xmlns:ns2="http://schemas.restbucks.com"> <ns2:item> <ns2:milk>semi</ns2:milk> <ns2:size>large</ns2:size> <ns2:drink>latte</ns2:drink> </ns2:item> <ns2:location>takeaway</ns2:location> </ns2:order>
  • 28. Confirm the Order • GET from the rel=“self” URI – http://restbucks.com/order/1234 Restbucks Service Client
  • 29. Confirm the Order • Request GET /order/1234 HTTP/1.1 Accept: application/vnd.restbucks+xml Host: restbucks.com Connection: keep-alive
  • 30. Confirm the Order • Response HTTP/1.1 200 OK Location: http://restbucks.com/order/1234 Content-Type: application/vnd.restbucks+xml Content-Length: 911 Date: Wed, 03 Mar 2010 20:58:02 GMT <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ns2:order xmlns:ns2="http://schemas.restbucks.com" xmlns:ns3="http://schemas.restbucks.com/dap"> <ns3:link mediaType="application/vnd.restbucks+xml" uri="http://restbucks.com/order/1234" rel="http://relations.restbucks.com/cancel"/> <ns3:link mediaType="application/vnd.restbucks+xml" uri="http://restbucks.com/payment/1234" rel="http://relations.restbucks.com/payment"/> <ns3:link mediaType="application/vnd.restbucks+xml" uri="http://restbucks.com/order/1234" rel="http://relations.restbucks.com/update"/> <ns3:link mediaType="application/vnd.restbucks+xml" uri="http://restbucks.com/order/1234" rel="self"/> <ns2:item> <ns2:milk>semi</ns2:milk> <ns2:size>large</ns2:size> <ns2:drink>latte</ns2:drink> </ns2:item> <ns2:location>takeaway</ns2:location> <ns2:cost>2.0</ns2:cost> <ns2:status>unpaid</ns2:status> </ns2:order>
  • 31. Change the Order • POST new order to service-generated URI – http://restbucks.com/order/1234 Restbucks Service Client
  • 32. POST? Not PUT? • PUT expects the whole resource state to be presented in the request – But our clients aren’t responsible for generating cost or status elements • PATCH would be better – It allows us to send diffs, but is new to the family of HTTP verbs and not widely supported yet • So we use POST – Could use more fine-grained resources with PUT
  • 33. Change the Order • Request POST order/1234 HTTP/1.1 Content-Type: application/vnd.restbucks+xml Accept: application/vnd.restbucks+xml Host: restbucks.com Connection: keep-alive Content-Length: 288 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ns2:order xmlns:ns2="http://schemas.restbucks.com"> <ns2:item> <ns2:milk>semi</ns2:milk> No service- <ns2:size>large</ns2:size> generated <ns2:drink>cappuccino</ns2:drink> content </ns2:item> <ns2:location>takeaway</ns2:location> </ns2:order>
  • 34. Change the Order • Response HTTP/1.1 200 OK Location: http://restbucks.com/order/1234 Content-Type: application/vnd.restbucks+xml Content-Length: 916 Date: Sun, 06 Sep 2009 06:52:22 GMT <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ns2:order xmlns:ns2="http://schemas.restbucks.com" xmlns:ns3="http://schemas.restbucks.com/dap"> <ns3:link mediaType="application/vnd.restbucks+xml" uri="http://restbucks.com/order/1234" rel="http://relations.restbucks.com/cancel"/> <ns3:link mediaType="application/vnd.restbucks+xml" uri="http://restbucks.com/payment/1234" rel="http://relations.restbucks.com/payment"/> <ns3:link mediaType="application/vnd.restbucks+xml" uri="http://restbucks.com/order/1234" rel="http://relations.restbucks.com/update"/> <ns3:link mediaType="application/vnd.restbucks+xml" uri="http://restbucks.com/order/1234" rel="self"/> <ns2:item> <ns2:milk>semi</ns2:milk> <ns2:size>large</ns2:size> <ns2:drink>cappuccino</ns2:drink> </ns2:item> <ns2:location>takeaway</ns2:location> <ns2:cost>2.0</ns2:cost> <ns2:status>unpaid</ns2:status> </ns2:order>
  • 35. Statelessness • Interactions with resources are stateless – The resource “forgets” about you while you’re not directly interacting with it • 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 putting the resource into some inconsistent state
  • 36. 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  Response PUT /order/1234 HTTP 1.1 409 Conflict Host: restbucks.com ... Too slow! Someone else has changed the state of my order  Request  Response OPTIONS /order/1234 HTTP 1.1 Allow: GET Host: restbucks.com
  • 37. What if we want to cancel? • DELETE order at rel=“self” URI – http://restbucks.com/order/1234 Restbucks Service Client
  • 38. What if we want to cancel? • Request DELETE /order/1234 HTTP/1.1 Host: restbucks.com Connection: keep-alive • Response HTTP/1.1 200 OK Content-Type: application/vnd.restbucks+xml Date: Sun, 06 Sep 2009 06:53:22 GMT
  • 39. Order Payment • PUT to service-generated payment URI – rel="http://relations.restbucks.com/payment" – http://restbucks.com/payment/1234 Restbucks Service Client
  • 40. Why PUT this time? • It’s idempotent – Safe to repeat in the event of failures • For all $ transactions, prefer idempotent verbs
  • 41. Order Payment • Request PUT /payment/1234 HTTP/1.1 Content-Type: application/vnd.restbucks+xml Accept: application/vnd.restbucks+xml Host: restbucks.com Connection: keep-alive Content-Length: 287 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <payment xmlns="http://schemas.restbucks.com"> <amount>2.0</amount> <cardholderName>Michael Farraday</cardholderName> <cardNumber>11223344</cardNumber> <expiryMonth>12</expiryMonth> <expiryYear>12</expiryYear> </payment>
  • 42. Order Payment • Response HTTP/1.1 201 Created server: grizzly/1.8.1 Location: http://restbucks.com/payment/1234 Content-Type: application/vnd.restbucks+xml Content-Length: 650 Date: Wed, 03 Mar 2010 20:58:03 GMT <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ns2:payment xmlns="http://schemas.restbucks.com/dap" xmlns:ns2="http://schemas.restbucks.com"> <link mediaType="application/vnd.restbucks+xml" uri="http://restbucks.com/order/1234" rel="http://relations.restbucks.com/order"/> <link mediaType="application/vnd.restbucks+xml" uri="http://restbucks.com/receipt/1234" rel="http://relations.restbucks.com/receipt"/> <ns2:amount>2.0</ns2:amount> <ns2:cardholderName>Michael Farraday</ns2:cardholderName> <ns2:cardNumber>11223344</ns2:cardNumber> <ns2:expiryMonth>12</ns2:expiryMonth> <ns2:expiryYear>12</ns2:expiryYear> </ns2:payment>
  • 43. What if we want to cancel now? • Request DELETE /order/1234 HTTP/1.1 Host: restbucks.com Connection: keep-alive • Response HTTP/1.1 405 Method Not Allowed Allow: GET Content-Type: application/vnd.restbucks+xml Content-Length: 0 Date: Sun, 06 Sep 2009 07:43:29 GMT
  • 44. Grab a Receipt • GET service-generated receipt URI – http://restbucks.com/receipt/1234 Restbucks Service Client
  • 45. Grab a Receipt • Request GET /receipt/1234 HTTP/1.1 Accept: application/vnd.restbucks+xml Host: restbucks.com
  • 46. Grab a Receipt • Request HTTP/1.1 200 OK server: grizzly/1.8.1 Content-Type: application/vnd.restbucks+xml Content-Length: 384 Date: Wed, 03 Mar 2010 20:58:03 GMT <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ns2:receipt xmlns="http://schemas.restbucks.com/dap" xmlns:ns2="http://schemas.restbucks.com"> <link mediaType="application/vnd.restbucks+xml" uri="http://restbucks.com/order/1234" rel="http://relations.restbucks.com/order"/> <ns2:amount>2.0</ns2:amount> <ns2:paid>2010-03-03T21:58:03.834+01:00</ns2:paid> </ns2:receipt>
  • 47. Poll until the order’s ready • GET from the rel=“http://relations.restbucks.com/order” URI – http://restbucks.com/order/1234 Restbucks Service Client
  • 48. Polling for a ready order on the wire • Request GET /order/1234 HTTP/1.1 Host: restbucks.com Connection: keep-alive
  • 49. Order’s ready, take the receipt and walk away • Response HTTP/1.1 200 OK server: grizzly/1.8.1 Content-Type: application/vnd.restbucks+xml Content-Length: 534 Date: Wed, 03 Mar 2010 20:58:03 GMT <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ns2:order xmlns:ns2="http://schemas.restbucks.com" xmlns:ns3="http://schemas.restbucks.com/dap"> <ns3:link mediaType="application/vnd.restbucks+xml" uri="http://restbucks.com/receipt/1234" rel="http://relations.restbucks.com/reciept"/> <ns2:item> <ns2:milk>semi</ns2:milk> <ns2:size>large</ns2:size> <ns2:drink>cappuccino</ns2:drink> </ns2:item> <ns2:location>takeaway</ns2:location> <ns2:cost>2.0</ns2:cost> <ns2:status>ready</ns2:status> </ns2:order>
  • 50. Complete the Order • DELETE order at rel=“http://relations.restbucks.com/receipt” URI – http://restbucks.com/receipt/1234 Restbucks Service Client
  • 51. Complete the Order • Request DELETE/receipt/1234 HTTP/1.1 Host: restbucks.com Connection: keep-alive
  • 52. Complete the Order • Response HTTP/1.1 200 OK server: grizzly/1.8.1 Content-Type: application/xml Content-Length: 393 Date: Wed, 03 Mar 2010 20:58:03 GMT <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ns2:order xmlns:ns2="http://schemas.restbucks.com" xmlns:ns3="http://schemas.restbucks.com/dap"> <ns2:item> <ns2:milk>semi</ns2:milk> <ns2:size>large</ns2:size> <ns2:drink>cappuccino</ns2:drink> </ns2:item> No Hypermedia <ns2:location>takeaway</ns2:location> Controls <ns2:cost>2.0</ns2:cost> <ns2:status>taken</ns2:status> </ns2:order>
  • 54. What did we learn from Restbucks? • HTTP has a header/status combination for every occasion • APIs are expressed in terms of links • Hypermedia formats are important • State machines (defined by links) are important – Just as in Web Services…
  • 55. Restbucks Written Up @ InfoQ http://www.infoq.com/articles/webber-rest-workflow
  • 56. Questions? Blog: http://jim.webber.name Twitter: @jimwebber Now in Safari Rough Cuts