Do You Do REST? Subbu Allamaraju Yahoo! Inc QCon San - - PowerPoint PPT Presentation

do you do rest
SMART_READER_LITE
LIVE PREVIEW

Do You Do REST? Subbu Allamaraju Yahoo! Inc QCon San - - PowerPoint PPT Presentation

Do You Do REST? Subbu Allamaraju Yahoo! Inc QCon San Francisco Nov 3-5, 2010 2 Take-away Interfaces for interoperability 3 Do you do


slide-1
SLIDE 1

Do ¡You ¡Do ¡REST? ¡

Subbu ¡Allamaraju ¡ Yahoo! ¡Inc ¡

QCon ¡San ¡Francisco ¡Nov ¡3-­‑5, ¡2010 ¡

slide-2
SLIDE 2

2 ¡

slide-3
SLIDE 3

Take-­‑away ¡

3 ¡

Interfaces ¡for ¡interoperability ¡

slide-4
SLIDE 4

Do ¡you ¡do ¡REST? ¡

4 ¡

… ¡we ¡don’t ¡do ¡pure ¡REST ¡

slide-5
SLIDE 5

But ¡where ¡did ¡REST ¡come ¡from? ¡

5 ¡

slide-6
SLIDE 6

Quality ¡AJributes ¡

  • Performance ¡
  • Scalability ¡
  • Simplicity ¡

– SeparaLon ¡of ¡concerns ¡

  • Modifiability ¡

– Evolvability, ¡extensibility, ¡customizability, ¡configurability, ¡ reusability ¡

  • Portability ¡

– By ¡making ¡a ¡constrained ¡set ¡of ¡choices ¡for ¡represenLng ¡data ¡and ¡ code ¡

  • Reliability ¡

– Avoiding ¡single ¡points ¡of ¡failure, ¡redundancy, ¡monitoring ¡

6 ¡

slide-7
SLIDE 7
  • IdenLficaLon ¡of ¡

resources ¡

  • ManipulaLon ¡via ¡

representaLons ¡

  • Self-­‑descripLve ¡

messages ¡

  • Hypermedia ¡as ¡the ¡

engine ¡of ¡applicaLon ¡ state ¡

  • Code ¡on ¡demand ¡

Constraints ¡

7 ¡

slide-8
SLIDE 8
  • URIs ¡as ¡resource ¡

idenLfiers ¡

  • HTTP ¡as ¡the ¡uniform ¡

interface ¡

  • MIME-­‑like ¡message ¡

format ¡

  • Hypertext ¡(controls ¡

like ¡forms ¡and ¡links) ¡

  • Code ¡on ¡demand ¡

ApplicaLon ¡

to ¡the ¡web ¡

8 ¡

slide-9
SLIDE 9

9 ¡

Scalable ¡and ¡pervasive ¡ Network-­‑effect ¡in ¡acLon ¡

Human-­‑web ¡

slide-10
SLIDE 10

Server ¡ Client ¡

ApplicaLon ¡and ¡impact ¡ with ¡mixed ¡results ¡

10 ¡

Machine-­‑web ¡

slide-11
SLIDE 11

11 ¡

Level ¡0: ¡ ¡One ¡URI, ¡one ¡HTTP ¡method ¡ Level ¡1: ¡ ¡Many ¡URIs, ¡one ¡HTTP ¡method ¡ Level ¡2: ¡ ¡Many ¡URIs, ¡many ¡HTTP ¡methods ¡ Level ¡3: ¡ ¡Hypermedia ¡ MarLn ¡Fowler’s ¡ “Glory ¡of ¡REST” ¡

Leonard ¡Richardson’s ¡Maturity ¡Model ¡

slide-12
SLIDE 12

12 ¡

  • Q. ¡What ¡is ¡the ¡contract? ¡ ¡
  • Q. ¡What ¡needs ¡to ¡be ¡governed? ¡
  • Q. ¡How ¡to ¡deal ¡with ¡versioning? ¡
  • Q. ¡How ¡to ¡make ¡services ¡extensible? ¡
slide-13
SLIDE 13

13 ¡

Server ¡ Client ¡ Server ¡ Client ¡ Server ¡ Client ¡ Server ¡ Client ¡

Inconsistent ¡URIs, ¡IDs, ¡HTTP ¡ usage, ¡security, ¡formats, ¡… ¡ ~ ¡ ~ ¡ ~ ¡ ~ ¡ ~ ¡ ~ ¡ ~ ¡ ~ ¡ ~ ¡ Each ¡with ¡its ¡own ¡set ¡of ¡barriers ¡

slide-14
SLIDE 14

14 ¡

http://foo.com/person/1234/address/6789 ¡ http://bar.com/user?id=1234&filter=address ¡ http://baz.com/user;id=1234/address;id=6789 ¡

slide-15
SLIDE 15

15 ¡

<comment ¡id=“1234” ¡posted_by=“user01”> ¡ ¡ ¡<text>…</text> ¡ ¡ ¡… ¡ </comment> ¡ { ¡ ¡ ¡ ¡“id” ¡: ¡“user01”, ¡ ¡ ¡ ¡“name” ¡: ¡“Joe”, ¡ ¡ ¡ ¡“locaLon” ¡: ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡“lat” ¡: ¡“…”, ¡ ¡ ¡ ¡ ¡ ¡ ¡“lng” ¡: ¡“…” ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡… ¡ } ¡

slide-16
SLIDE 16

“…the ¡ability ¡of ¡diverse ¡systems ¡and ¡

  • rganizaLons ¡to ¡work ¡together” ¡[Wikipedia] ¡

16 ¡

Challenge: ¡ Interoperability ¡

slide-17
SLIDE 17

What ¡is ¡the ¡interface ¡of ¡the ¡Web? ¡

17 ¡

slide-18
SLIDE 18

Object ¡

Iden%ty ¡ Behavior ¡ State ¡

  • Dist. ¡Obj. ¡

Remote-­‑as-­‑local ¡ OS ¡independence ¡ Naming ¡ … ¡

Service ¡ Resource ¡

Interface ¡ IDL ¡ WSDL ¡ Protocol ¡ ¡ independence ¡ Fielding’s ¡ ¡ ili%es ¡ ? ¡

18 ¡

>me ¡

Thinking ¡about ¡interfaces ¡

Consumers ¡

slide-19
SLIDE 19

C/S ¡ C/S ¡ C/S ¡ C/S ¡

Decentralized ¡

C/S ¡ C/S ¡

I ¡ I ¡ I ¡ I ¡ I ¡ I ¡ I ¡ I ¡ I ¡ I ¡ I ¡ I ¡

19 ¡

Agreements ¡among ¡disagreements ¡

slide-20
SLIDE 20

Most ¡given ¡+ ¡Some ¡applicaLon ¡defined ¡ Interoperability ¡= ¡f(interfaces) ¡ ¡

20 ¡

Single ¡interface ¡ ¡ Many interfaces

slide-21
SLIDE 21

Interface: ¡URIs ¡

21 ¡

slide-22
SLIDE 22
  • 1. ¡Syntax ¡

22 ¡

Interface: ¡URIs ¡

{scheme}://{authority}/{path}?{query}#{frag} ¡

  • 2. ¡ResoluLon ¡

¡ ¡ ¡ ¡Base: ¡http://www.example.org ¡ Relative: ¡/path?query ¡

  • 3. ¡NormalizaLon ¡and ¡Comparison ¡

Is ¡http://www.example.org/path?query ¡the ¡same ¡ as ¡http://example.org/path/?query ¡

[RFC ¡ ¡3986] ¡

slide-23
SLIDE 23

+ ¡Dereferenceable ¡

23 ¡

http://www.subbu.org/blog/tag/rest ¡ http://api.nytimes.com/svc/search/v1/ article?query=haiti&api-­‑key={api-­‑key} ¡ jar:http://mycorp.com/jars/foo.jar!/ mycorp/User.class ¡ ¡ jms:topic:Stocks.Prices ¡

slide-24
SLIDE 24

+ ¡IdenLty ¡

  • IdenLfier ¡in ¡some ¡local ¡or ¡global ¡scope ¡
  • Be ¡able ¡to ¡make ¡asserLons ¡

24 ¡

<! ¡A ¡review ¡of ¡RESTful ¡Web ¡Services ¡ ¡ ¡ ¡ ¡ ¡ ¡Cookbook ¡-­‑-­‑> ¡ <review> ¡ ¡ ¡<link ¡href=“http://amzn.com/0596801688” ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡rel=“review”/> ¡ ¡ ¡... ¡ </review> ¡

slide-25
SLIDE 25

+ ¡Hackable ¡and ¡Consistent ¡

25 ¡

http://api.twitter.com/version/statuses/ show/:id.format ¡ ¡ http://api.twitter.com/1/statuses/ user_timeline.format?user_id=:userid ¡ https://my.social.net/contacts/userEmail ¡ http://my.social.net/videos/username/ playlists ¡

slide-26
SLIDE 26

Interface: ¡Resources ¡

26 ¡

slide-27
SLIDE 27

None ¡

27 ¡

Interface: ¡Resources ¡

“(T)he ¡term ¡‘resource’ ¡is ¡used ¡in ¡a ¡ general ¡sense ¡for ¡whatever ¡might ¡be ¡ idenLfied ¡by ¡a ¡URI” ¡[RFC-­‑3986] ¡

slide-28
SLIDE 28

+ ¡Many ¡Resources ¡

  • Everything ¡that ¡a ¡client ¡needs ¡has ¡a ¡URI ¡
  • Consistent ¡formats ¡and ¡paJerns ¡for ¡

representaLons ¡

28 ¡

slide-29
SLIDE 29

29 ¡

<comment> ¡ ¡ ¡<id>1234</id> ¡ ¡ ¡<user> ¡ ¡ ¡ ¡ ¡<id>user01</id> ¡ ¡ ¡ ¡ ¡<link ¡rel=“user” ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡href=“...”/> ¡ ¡ ¡</user> ¡ ¡ ¡<text>…</text> ¡ ¡ ¡… ¡ </comment> ¡ <user> ¡ ¡ ¡<id>user01</id> ¡ ¡ ¡<name>Joe</name> ¡ ¡ ¡<locaLon> ¡ ¡ ¡ ¡ ¡<lat>…</lat> ¡ ¡ ¡ ¡ ¡<lng>…</lng> ¡ ¡ ¡</locaLon> ¡ </user> ¡

slide-30
SLIDE 30

Interface: ¡HTTP ¡

30 ¡

slide-31
SLIDE 31
  • 1. HTTP ¡methods ¡GET, ¡PUT, ¡POST, ¡DELETE, ¡HEAD ¡

and ¡OPTIONS ¡

31 ¡

Interface: ¡HTTP ¡

[RFC ¡ ¡2616] ¡

Fixed ¡set ¡of ¡methods ¡lets ¡a ¡lot ¡of ¡people ¡agree ¡ and ¡support ¡them ¡– ¡a ¡key ¡for ¡interoperability ¡

slide-32
SLIDE 32
  • 2. Message ¡syntax ¡

– Headers ¡+ ¡body ¡

  • 3. Method ¡safety ¡

– GET, ¡HEAD, ¡OPTIONS ¡

  • 4. Method ¡idempotency ¡

– GET, ¡HEAD, ¡OPTIONS, ¡PUT, ¡DELETE ¡

32 ¡

Interface: ¡HTTP ¡

[RFC ¡ ¡2616] ¡

See ¡Chapter ¡1 ¡in ¡RESTful ¡Web ¡Services ¡Cookbook ¡

slide-33
SLIDE 33

+ ¡Protocol ¡

  • 1. Content ¡negoLaLon ¡(?) ¡

– Accept-­‑* ¡headers ¡or ¡URIs ¡for ¡each ¡format ¡

  • 2. Caching ¡

– Last-­‑Modified, ¡Cache-­‑Control ¡

  • 3. CondiLonal ¡requests ¡

– If-­‑Modified-­‑Since, ¡If-­‑Unmodified-­‑ Since, ¡If-­‑Match, ¡If-­‑None-­‑Match ¡

  • 4. Custom ¡headers ¡(?) ¡ ¡

33 ¡

See ¡Chapters ¡7, ¡9 ¡and ¡10 ¡in ¡RESTful ¡Web ¡Services ¡Cookbook ¡

slide-34
SLIDE 34

Interface: ¡Security ¡Protocols ¡

34 ¡

slide-35
SLIDE 35
  • Support ¡for ¡WWW-­‑Authenticate ¡and ¡

Authorization ¡headers ¡

35 ¡

Interface: ¡Security ¡

[RFC ¡ ¡2616] ¡

slide-36
SLIDE 36

36 ¡

Client ¡ Server ¡

GET ¡/foo ¡HTTP/1.1 ¡ HTTP/1.1 ¡401 ¡Unauthorized ¡ WWW-­‑Authenticate: ¡<scheme> ¡ GET ¡/foo ¡HTTP/1.1 ¡ Authorization: ¡<token> ¡ HTTP/1.1 ¡2OO ¡OK ¡

Ley ¡to ¡

  • ther ¡

protocols ¡

slide-37
SLIDE 37

Interoperability ¡Hurdles ¡

  • Early ¡protocols ¡

– RFC ¡2617 ¡ – Poor ¡adopLon ¡ – Basic ¡auth ¡was ¡weak ¡and ¡Digest ¡auth ¡was ¡complex ¡

  • Cookies ¡are ¡widely ¡interoperable, ¡but ¡poorly ¡

specified ¡

  • Security ¡policies ¡within ¡corporate ¡networks ¡

are ¡oyen ¡unique ¡to ¡each ¡organizaLon ¡

37 ¡

slide-38
SLIDE 38

+ ¡Consistency ¡

  • 1. Consistent ¡HTTP ¡based ¡protocols ¡

– Basic ¡ – Oauth ¡1.0 ¡and/or ¡OAuth ¡2.0 ¡ – Custom ¡but ¡consistent ¡

38 ¡

slide-39
SLIDE 39

Interface: ¡Hypertext ¡

39 ¡

slide-40
SLIDE 40

40 ¡

Interface: ¡Hypertext ¡

“Apparently ¡you ¡can ¡book ¡Lckets, ¡check ¡ email ¡and ¡do ¡all ¡sorts ¡of ¡ ¡stuff ¡online ¡ without ¡knowing ¡that ¡each ¡website ¡has ¡ a ¡disLnct ¡address ¡called ¡a ¡URL.” ¡

Compared ¡to ¡you, ¡most ¡people ¡seem ¡dumb ¡

slide-41
SLIDE 41

41 ¡

Human-­‑Web ¡

  • HTML ¡as ¡the ¡main ¡interface ¡
  • Well-­‑defined ¡hypermedia ¡controls ¡(forms ¡and ¡

hyperlinks) ¡

  • AutomaLc ¡composiLon ¡in ¡the ¡browser ¡(CSS, ¡

images, ¡video ¡etc.) ¡

  • A ¡user ¡leisurely ¡driving ¡the ¡client ¡
slide-42
SLIDE 42

42 ¡

Client ¡ Resource ¡ Resource ¡ Resource ¡ User ¡ Client ¡ Client ¡

slide-43
SLIDE 43

GET ¡/order/1234 ¡HTTP/1.1 ¡ Host: ¡orders.example.org ¡ HTTP/1.1 ¡200 ¡OK ¡ Content-­‑Type: ¡application/vnd.order+xml ¡ <order> ¡ ¡ ¡<id>1234</id> ¡ ¡ ¡<status>pending</status> ¡ ¡ ¡<link ¡rel=“http://www.example.org/rel/order.submit” ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡href=“http://sales.example.org/order/1234? ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡t=asd2342”/> ¡ ¡ ¡... ¡ </order> ¡

43 ¡

slide-44
SLIDE 44

44 ¡

application/vnd.order+xml ¡ Lorem ¡ipsum ¡dolor ¡sit ¡amet, ¡consectetur ¡adipiscing ¡

  • elit. ¡Duis ¡suscipit, ¡augue ¡ut ¡mazs ¡venenaLs, ¡purus ¡elit ¡

dapibus ¡nibh, ¡nec ¡tempus ¡nunc ¡lectus ¡viverra ¡nulla ¡… ¡ http://www.example.org/rel/order.submit ¡ Suspendisse ¡in ¡dignissim ¡erat. ¡Donec ¡ut ¡arcu ¡sed ¡libero ¡ semper ¡imperdiet ¡ornare ¡ac ¡eros. ¡Cras ¡hendrerit ¡ volutpat ¡nibh, ¡at ¡convallis ¡purus ¡porztor ¡in. ¡Nullam ¡ scelerisque ¡placerat ¡dui, ¡id ¡facilisis ¡diam ¡laoreet ¡non. ¡… ¡

slide-45
SLIDE 45

45 ¡

Broker ¡

Resource ¡ Resource ¡ Resource ¡

User ¡

State ¡changes ¡via ¡ hypertext ¡

Front-­‑end ¡

Search ¡ ¡ ProjecLons ¡ Filtering ¡ AggregaLon ¡

Search ¡

Low-­‑latency ¡

slide-46
SLIDE 46

HEAD ¡/user/1234 ¡HTTP/1.1 ¡ Host: ¡www.example.org ¡ HTTP/1.1 ¡204 ¡No ¡Content ¡ Link: ¡<http://www.example.org/comments?uid=1234>; ¡ ¡ ¡ ¡ ¡ ¡ ¡rel=“related ¡http://www.example.org/comments” ¡ Link: ¡<http://www.example.org/profie/1234>; ¡ ¡ ¡ ¡ ¡ ¡ ¡rel=“related ¡http://www.example.org/profile” ¡ Link: ¡<http://www.example.org/videos?uid=1234>; ¡ ¡ ¡ ¡ ¡ ¡ ¡rel=“related ¡http://www.example.org/media” ¡

46 ¡

slide-47
SLIDE 47

HEADE ¡/user/1234 ¡HTTP/1.1 ¡ Host: ¡www.example.org ¡ HTTP/1.1 ¡200 ¡OK ¡ Content-­‑Type: ¡application/xml;charset=UTF-­‑8 ¡ <user> ¡ ¡ ¡<id>1234</id> ¡ ¡ ¡<link ¡href=“http://www.example.org/comments?uid=1234” ¡ ¡ ¡ ¡ ¡ ¡ ¡rel=“related ¡http://www.example.org/comments”/> ¡ ¡ ¡<link ¡href=“http://www.example.org/profie/1234” ¡ ¡ ¡ ¡ ¡ ¡ ¡rel=“related ¡http://www.example.org/profile”/> ¡ ¡ ¡... ¡ </user> ¡

47 ¡

slide-48
SLIDE 48

Interface: ¡Hypertext ¡

  • 1. HTML ¡when ¡the ¡hypermedia ¡driver ¡is ¡a ¡user ¡

– UI ¡converging ¡to ¡HTML ¡

  • 2. Special ¡media ¡types ¡for ¡local ¡interoperability ¡
  • 3. Links ¡expressed ¡as ¡Link ¡headers ¡[RFC ¡5988] ¡or ¡

in ¡the ¡body ¡or ¡both ¡

  • 4. Link ¡relaLon ¡types ¡

48 ¡

slide-49
SLIDE 49

What ¡are ¡the ¡interfaces ¡of ¡the ¡ Machine-­‑Web? ¡

49 ¡

slide-50
SLIDE 50

50 ¡

URI ¡Syntax ¡ Dereferenceable ¡ Consistent ¡ Hackable ¡

URI ¡

Syntax ¡ Idempotency ¡ Caching ¡

  • Cond. ¡Req. ¡

HTTP ¡

Safety ¡ Conneg ¡(?) ¡

  • Cust. ¡Headers ¡(?) ¡

HTTP ¡Based ¡ OAuth ¡ Consistent ¡

Auth ¡

Link ¡Header ¡

Hypertext ¡

Cookies ¡ Links ¡in ¡body ¡ Link ¡Rel. ¡Reg. ¡

Resources ¡

PaJerns ¡ IdenLfiers ¡ Many ¡ ¡ Basic ¡

Non-­‑negoLable ¡ Watch-­‑out ¡

URI ¡Templates ¡

slide-51
SLIDE 51

Interfaces ¡of ¡the ¡Web ¡

  • The ¡architecture ¡of ¡the ¡web ¡relies ¡on ¡many ¡

loosely ¡related ¡interfaces ¡

  • Each ¡focusing ¡on ¡different ¡aspect ¡of ¡

interoperability ¡

  • Some ¡pre-­‑defined ¡– ¡others ¡applicaLon-­‑

defined ¡

  • Interoperability ¡is ¡a ¡funcLon ¡of ¡supporLng ¡

these ¡interfaces ¡

51 ¡

slide-52
SLIDE 52

52 ¡

Subbu ¡Allamaraju ¡ hJp://www.subbu.org ¡ @sallamar ¡