rest: i don't think it means what you think it...

72
REST: I don't Think it Means What You Think it Does Stefan Tilkov | @stilkov GOTO Amsterdam, 20 June 2014 Friday 20 June 14

Upload: others

Post on 11-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

REST: I don't Think it Means What You Think it Does

Stefan Tilkov | @stilkovGOTO Amsterdam, 20 June 2014

Friday 20 June 14

Page 2: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

REST: An architectural style defined by the constraints Client-Server, Stateless

Communication, Caching, Uniform Interface, Layered System, and (optionally) Code-on-demand.

For details, see http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm

Thank you!

Friday 20 June 14

Page 3: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

REST Intro

Friday 20 June 14

Page 4: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

identificationof resources

Friday 20 June 14

Page 5: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

identificationof resources

URIsUniversal identity

Link targetsBookmarks

Entry points

Friday 20 June 14

Page 6: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

resource manipulationthrough representations

Friday 20 June 14

Page 7: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

resource manipulationthrough representations

Metadata

SoC data/identity/location

Multiple representations

Reduced dependencyProcessing model

Friday 20 June 14

Page 8: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

hypermedia as the engineof application state

Friday 20 June 14

Page 9: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

hypermedia as the engineof application state

ConnectionsLinks

Forms Flow

Status

Friday 20 June 14

Page 10: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

self-descriptive messages

Friday 20 June 14

Page 11: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

self-descriptive messages

Metadata

Standardization

Provider independence

IntermediariesCaching

Friday 20 June 14

Page 12: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

What’s in the Web?

Friday 20 June 14

Page 13: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

Lots of things with URLs …

Friday 20 June 14

Page 14: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

… connected with hypermedia …

Friday 20 June 14

Page 15: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

… governed by standard formats.

Friday 20 June 14

Page 16: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

Common Misconceptions

Friday 20 June 14

Page 17: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

REST is about nice URIs

IT’S NOT

Friday 20 June 14

Page 18: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

1. http://example.com/customers/format?drive=c

2. http://example.com/customers/getDetails?id=13

3. http://example.com/customers/delete?id=13

4. http://example.com/customers/13

5. http://example.com/customers/13/edit

What makes a URI “RESTful”?

Friday 20 June 14

Page 19: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

There is no such thing as a “RESTful URI”

example.com /customers/delete?id=13

Scheme PathHost Param

://http

Opaque ID

Friday 20 June 14

Page 20: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

Why you shouldn’t care about URIs

<customer> <...> <orders href=' '></customer>

http://example.com/customers/13/ordershttp://xyz.org/838892AEF28CDAEFD1234/3

Hypermedia context

Friday 20 June 14

Page 21: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

REST = URI patterns + GET, PUT,

POST, DELETE

CLOSE, BUT NO

Friday 20 June 14

Page 22: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

URI Method Meaning

http://ex.org/v1/customers POST create new customer

http://ex.org/v1/customers/{id} GET get customer details

http://ex.org/v1/customers/{id}/orders GETget list of customer’s details

... ... ...Versions in URIs cause change for no good reason

Documented URIs become APIs

Assumptions about server details become facts

Friday 20 June 14

Page 23: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

Detour: Versioning

Friday 20 June 14

Page 24: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

What are you “versioning”?DataDocumentationFormatsAPIs

✔✔

✔✘

Friday 20 June 14

Page 25: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

Versioning recommendations

Friday 20 June 14

Page 26: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

Use different media typesfor different things

Friday 20 June 14

Page 27: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

Use version numbers in URIs to version the resource itself

Friday 20 June 14

Page 28: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

Create new resourcesfor new aspects

Friday 20 June 14

Page 29: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

Reserve space for links

Friday 20 June 14

Page 30: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

Version your docs

Friday 20 June 14

Page 31: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

Wait, what?

Friday 20 June 14

Page 32: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

Yes, no versioning

Friday 20 June 14

Page 33: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

Postel’s law

http://tools.ietf.org/html/rfc761

“TCP implementations should follow a general principle of robustness: Be conservative in what you do, be liberal in what you accept from others.”

Friday 20 June 14

Page 34: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

Be kind to each other

Friday 20 June 14

Page 35: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

Postel’s Law

Liberal, Loose, Tolerant

Strict, Critical, Draconian

Liberal, Loose, Tolerant

Friday 20 June 14

Page 36: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

Don’t break URI structure unnecessarily

Evolve via additionalresources

Support older formats

Server rulesDon’t depend onURI structure

Support unknownlinks

Ignore unknown content

Client rules

Friday 20 June 14

Page 37: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

Friday 20 June 14

Page 38: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

How to hypermedia-enableyour service interfaces

Friday 20 June 14

Page 39: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

Step 1: Service Documents

Friday 20 June 14

Page 40: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

<?xml version="1.0" encoding="UTF-8"?><serviceDescription xml:base="http://om.example.com"> <link rel="all" href="/orders/" /> <link rel="received" href="/orders/received/" /> <link rel="accepted" href="/orders/accepted/" /> <link rel="rejected" href="/orders/rejected/" /> <link rel="cancelled" href="/orders/cancelled/" /> <link rel="fulfilled" href="/orders/fulfilled/" /> <link rel="cancellations" href="/cancellations/" /> <link rel="reports" href="/reports/" /></serviceDescription>

<link rel="fulfilled" href="http://om.archive.com/orders/" />

{ "serviceDescription" : {  "base": "http://om.example.com",  "links": [    { "rel": "all", "href": "/orders/" },    { "rel": "all", "href": "/orders/" },    { "rel": "received", "href": "/orders/received/" },    { "rel": "accepted", "href": "/orders/accepted/" },    { "rel": "rejected", "href": "/orders/rejected/" },    { "rel": "cancelled", "href": "/orders/cancelled/" },    { "rel": "fulfilled", "href": "/orders/fulfilled/" },    { "rel": "cancellations", "href": "/cancellations/" },    { "rel": "reports", "href": "/reports/" }  ]}}

Friday 20 June 14

Page 41: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

JSON Home{     "resources": {       "http://example.org/rel/widgets": {         "href": "/widgets/"       },       "http://example.org/rel/widget": {         "href-template": "/widgets/{widget_id}",         "href-vars": {           "widget_id": "http://example.org/param/widget"         },         "hints": {           "allow": ["GET", "PUT", "DELETE", "PATCH"],           "representations": ["application/json"],           "accept-patch": ["application/json-patch"],           "accept-post": ["application/xml"],           "accept-ranges": ["bytes"]         }       }     }   }

http://tools.ietf.org/html/draft-nottingham-json-home-03

Friday 20 June 14

Page 42: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

Step 2: Resource Links

Friday 20 June 14

Page 43: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

GET /order/123

{ "order": {  "date": "2013-07-02",    "total": 1240.02, "..." : "...", "links" : [ {"rel" : "self", "href" : "http://example.com/orders/123"}, {"rel" : "contacts", "href" : "http://example.org/A3BEF1"}, ... ]

}}

Friday 20 June 14

Page 44: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

GET /order/123

{ "order": {  "date": "2013-07-02",    "total": 1240.02, "..." : "...", "links" : { "self": "http://example.com/orders/123", "contacts": "http://example.org/A3BEF1", ... }

}}

Your future extensions

Friday 20 June 14

Page 45: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

Step 3: State Transition Links

Friday 20 June 14

Page 46: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

{ "order": { "state" : "received", "..." : "...", "links" : [ {"rel" : "cancel", "href" : "http://..."}, {"rel" : "accept", "href" : "http://..."}, {"rel" : "reject", "href" : "http://..."}, ... ]

}}

Friday 20 June 14

Page 47: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

Hypermedia APIs = Responses with Links

NOT ONLY

Friday 20 June 14

Page 48: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

Rule #1: Don’t have clients build URIs using string concatenation

Friday 20 June 14

Page 49: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

…instead: provide recipes

Friday 20 June 14

Page 50: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

<form action='http://example.com/search' method='GET'>  Search for: <input type='text' name='query'> <input type='submit'></form>

{   "rel": "search",  "template": "http://example.com/search?q={query}" }

Friday 20 June 14

Page 51: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

<form action='http://example.com/add' method='POST'>  First: <input type='text' name='first'>  Last: <input type='text' name='last'>  Birthday: <input type='date' name='bdate'> <input type='submit'></form>

{"target": "http://example.com/add",  "rel": "add",  "template": {   "first": "...",   "last": "...",   "bdate": "..." }}

Friday 20 June 14

Page 52: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

REST = A different approach to service interfaces

TOO SIMPLE

Friday 20 June 14

Page 53: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

System B

Point-to-point integration

System A

Friday 20 June 14

Page 54: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

Client 1

Service interfaces

ServerClient 2

Client n

Friday 20 June 14

Page 55: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

Client 1

Hypermedia types

Client 2

Client n

Server A

Server B

Server n

…Format

Friday 20 June 14

Page 56: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

application/atom+xml

Friday 20 June 14

Page 57: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

application/opensearchdescription+xml

Friday 20 June 14

Page 58: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

application/vnd.collection+json

Friday 20 June 14

Page 59: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

application/hal+json

Friday 20 June 14

Page 60: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

application/vnd.siren+json

Friday 20 June 14

Page 61: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

text/html

Friday 20 June 14

Page 62: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

Detour: HTML

Friday 20 June 14

Page 63: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

<order> <shippingAddress>Paris, France</shippingAddress> <items> <item> <productDescription>iPad</productDescription> <quantity>1</quantity> <price>699</price> </item> </items> <link href="http://om.example.com/cancellations" rel="cancel" /> <link href="https://om.example.com/orders/123/payment" rel="payment" /> </order>

Friday 20 June 14

Page 64: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

<html xmlns="http://www.w3.org/1999/xhtml"> <body> <div class="order"> <p class="shippingAddress">Paris, France</p> <ul class="items"> <li class="item"> <p class="productDescription">iPad</p> <p class="quantity">1</p> <p class="price">699</p> </li> </ul> <a href="http://om.example.com/cancellations" rel="cancel">cancel</a> <a href="https://om.example.com/orders/123/payment" rel="payment">payment</a> </div> </body> </html>

Friday 20 June 14

Page 65: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

Benefits of Using HTML as Your Hypermedia Format

Ubiquity

Well-known, well supported HM controls

A pretty good standard client

Lots of programming tools

UIs as a side-effect

Friday 20 June 14

Page 66: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

Friday 20 June 14

Page 67: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

Summary

Friday 20 June 14

Page 68: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

Link context over pretty URIs

Friday 20 June 14

Page 69: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

Hypermedia over URI APIs

Friday 20 June 14

Page 70: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

Hypermedia flows over static links

Friday 20 June 14

Page 71: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

Generalized formats over services

Friday 20 June 14

Page 72: REST: I don't Think it Means What You Think it Doesgotocon.com/dl/goto-amsterdam-2014/slides/StefanT... · REST: An architectural style de!ned by the constraints Client-Server, Stateless

Q&AStefan Tilkov, @[email protected]: +49 170 471 2625

innoQ Deutschland GmbHKrischerstr. 10040789 Monheim am RheinGermanyPhone: +49 2173 3366-0

innoQ Schweiz GmbH

[email protected]

Gewerbestr. 11CH-6330 ChamSwitzerlandPhone: +41 41 743 0116

www.innoq.com

Ohlauer Straße 4310999 BerlinGermanyPhone: +49 2173 3366-0

Robert-Bosch-Straße 764293 DarmstadtGermanyPhone: +49 2173 3366-0

Radlkoferstraße 2D-81373 MünchenTelefon +49 (0) 89 741185-270

Friday 20 June 14