INFO/CS  4302   Web  Informa6on  Systems   FT  2012   Week  7:  RESTful  Webservice  APIs     -­‐  Bernhard  Haslhofer  -­‐    

2  

3  

4  

Source:  hMp://www.blogperfume.com/new-­‐27-­‐circular-­‐social-­‐media-­‐icons-­‐in-­‐3-­‐sizes/    

5  

Plan  for  today...   •  Recap  -­‐  Web  Fundamentals   •  APIs,  Web  Services   •  Group  Brainstorming   •  RESTful  APIs  –  Architectural  principles   •  Ques6ons,  Housekeeping,  ...   6  

RECAP  –  WEB  FUNDAMENTALS  

7  

Web  Fundamentals       Internet  ≠  World  Wide  Web  

8  

Web  Fundamentals   •  Key  Architectural  Components   –  Iden6fica6on:  ???   –  Interac6on:  ???   –  Standardized  Document  Formats:  ???,  ???,  ???  

9  

Web  Fundamentals   •  Key  Architectural  Components   –  Iden6fica6on:  URI   –  Interac6on:  HTTP   –  Standardized  Document  Formats:  HTML,  XML,   JSON,  etc.  

10  

URI/  Iden6fica6on  

Principle  ‘Orthogonal  Specifica6ons’  

hMp/  Interac6on  

11  

URIs  /  Resources   •  URIs  iden6fy  interes6ng  things   –  documents  on  the  Web   –  relevant  aspects  of  a  data  set  

•  HTTP  URIs  name  and  address  resources  in   Web-­‐based  systems   –  a  URI  names  and  iden6fies  one  resource   –  a  resource  can  have  more  than  one  name   •  hMp://foo.com/sogware/latest   •  hMp://foo.com/sogware/v1.4   12  

Resource  Representa6on   •  A  resource  can  have   several  representa6ons   •  Representa6ons  can  be   in  any  format   –  HTML   –  XML   –  JSON   –  …  

URI

Representation Plain Text text/plain

Representation HTML text/html

http://example.com/someURI Representation JSON text/json

Resource

13  

Interac6ng  with  Resources   •  We  deal  with  resource  representa6ons  

–  not  the  resources  themselves  (pass  by  value)   –  representa6ons  can  be  in  any  format  (defined  by  media-­‐type)  

•  Each  resource  implements  a  standard  uniform  interface  (HTTP)   –  a  small  set  of  verbs  applied  to  a  large  set  of  nouns   –  verbs  are  universal  and  not  invented  on  a  per-­‐applica6on  basis   Resource Representations

Client

JSON

orm f i Un

rfa e t In

ce Server

Logical Resources

Physical Resources

14  

Document/Data  Formats  

HTML,   XHTML,   ...  

Display  data  

XML,   JSON,   ...  

Transport  and  store  data  

15  

APIS,  WEB  SERVICES  

16  

APIs   What  is  an  API?   and   Why  do  we  need  APIs?  

17  

(Web)  APIs   •  Applica6on  Programming  Interface   •  Specifies  how  sogware  components   communicate  with  each  other   –  e.g.,  Java  API,  3rd  party  library  APIs   –  usually  come  with  documenta6on,  howtos  

•  Web  API:  specify  how  applica6ons   communicate  with  other  over  the  Web  (HTTP,   URI,  XML,  etc.)   18  

Web  Services   API  

Web Application A

Application B

•  Example  opera6ons:   –  Publish  image  on  Flickr   –  Order  a  book  at  Amazon   –  Post  a  message  on  your  friend’s  Facebook  wall   –  Update  user  photo  on  foursquare   19  

Web  Services   •  “Web  Services”  ≅  “Web  APIs”   •  Build  on  the  design  principles  and  architectural   components  of  the  Web   •  Provide  certain  opera6ons   •  Exchange  structured  data  in  standard  formats   (JSON,  XML,  etc)   20  

GROUP  BRAINSTORMING  

21  

Instruc6ons   •  Form  groups  of  5   •  10  min:   –  discuss  known  or  possible  Web  API  opera6ons   (func6ons)   –  collect  opera6ons  in  the  form:   •  [verb][noun]  at  [service]  

–  one  person  per  group  should  write  them  down  at:   hMp://bit.ly/info4302-­‐api-­‐brainstorming  

22  

RESTFUL  APIS  –  ARCHITECTURAL   PRINCIPLES   23  

24  

RESTful  Webservices   •  REST  =  Representa6onal  State  Transfer   –  Based  on  Chapter  5  of  Roy  Fielding’s  2000  PhD   thesis  (it  is  in  your  reading  list!)  

•  An  architectural  style  for  building  loosely   coupled  systems   •  The  Web  itself  is  an  instance  of  that  style   •  Web  Services  can  be  built  on  top  of  it  

25  

The  Resource-­‐Oriented  Architecture   •  A  set  of  design  principles  for  building  RESTful   Web  Services   –  Addressability   –  Uniform  interface   –  Connectedness   –  Statelessness  

26  

Addressability   •  An  addressable  applica6on   –  exposes  the  interes6ng  aspects  of  its  dataset  as   resources   –  exposes  a  URI  for  every  piece  of  informa6on  it   might  serve   –  which  is  usually  an  infinite  number  of  URIs  

27  

Addressability   •  A  resource   –  is  anything  that  is  important  enough  to  be   referenced  as  a  thing  in  itself   –  usually  something   •  you  want  to  serve  informa6on  about   •  that  can  be  represented  as  a  stream  of  bits   –  actors   –  movies  

–  a  resource  must  have  at  least  one  name  (URI)   28  

Addressability   •  Resource  names  (URIs)   –  the  URI  is  the  name  and  address  of  a  resource   –  a  resource’s  URI  should  be  descrip6ve  

http://example.com/movies! ! instead of! ! http://example.com/overview.php?list=all,type=movie! !

29  

The  Resource-­‐Oriented  Architecture   •  A  set  of  design  principles  for  building  RESTful   Web  Services   –  Addressability   –  Uniform  interface   –  Connectedness   –  Statelessness  

30  

Uniform  Interface   •  The  same  set  of  opera6ons  applies  to   everything  (every  resource)   •  A  small  set  of  verbs  (methods)  applied  to  a   large  set  of  nouns  (resources)   –  verbs  are  universal  and  not  invented  on  a  per-­‐ applica6on  base  

•  Natural  language  works  in  the  same  way  (new   verbs  rarely  enter  language)   31  

Uniform  Interface   •  HTTP  defines  a  small  set  of  verbs  (methods)   for  ac6ng  on  URI-­‐iden6fied  resources   Which  methods  (verbs)  are  defined  in  HTTP?  

32  

Uniform  Interface   •  RESTful  Web  Services  use  HTTP  to  its  full   extent   –  Methods:  GET,  POST,  PUT,  DELETE,  (...)   –  Request  headers:  Authoriza6on,  Content-­‐Type,   Last-­‐Modified   –  Response  Codes:  200  OK,  304  Not  Modified,  401   Unauthorized,  500  Internal  Server  Error   –  Body:  an  envelope  for  data  to  be  transported   from  A  to  B   33  

Uniform  Interface   •  With  HTTP  we  have  all  methods  we  need  to   manipulate  Web  resources  (CRUD  interface)   –  Create  =  POST  (or  PUT)   –  Read  =  GET   –  Update  =  PUT   –  Delete  =  DELETE  

/234567839&:!*,& I/#)$'&

9oQ&

9)5J)5&

!"1/&:!*,& I/#)$'&

N::3&

*)+&

34  

Mapping  Web  Service  Opera6ons  to  CRUD   •  C(reate):     –  order  at  Etsy,  message  on  Facebook  wall,  ???  

•  R(read):   –  ???  

•  U(pdate):   –  user  account  on  Etsy,  ???  

•  D(elete):   –  order  at  Etsy,  ???   35  

Safe  and  Idempotent  Behavior   •  Safe  methods  can  be  ignored  or  repeated   without  side-­‐effects:  GET  and  HEAD   •  Idempotent  methods  can  be  repeated  without   side-­‐effects:  PUT  and  DELETE   •  Unsafe  and  non-­‐idempotent  methods  should   be  treated  with  care:  POST  

36  

Uniform  Interface   •  CREATE  a  new  resource  with  HTTP  POST   Client

Server

POST /movies HTTP/1.1 Host: example.com

201 Created Location: http://example.com/movies/1234

400 Bad Request

500 Internal Server Error

37  

Example  POST  Request   POST /movies HTTP/1.1! Host: example.com! ...! ! ! ! !The Godfather! !...! !

38  

POST  Seman6cs   •  POST  creates  a  new  resource   •  The  server  decides  on  the  resource’s  URI   •  POST  is  not  idempotent   –  A  sequence  of  two  or  more  POST  requests  has  side-­‐ effects   –  Human  Web:   •  “Do  you  really  want  to  post  this  form  again?”   •  “Are  you  sure  you  want  to  purchase  that  item  again?”  

–  Programma6c  Web:   •  if  you  post  twice,  you  create  two  resources   39  

Uniform  Interface   •  CREATE  a  new  resource  with  HTTP  PUT   Client

Server

PUT /movies/1234 HTTP/1.1 Host: example.com

200 OK

404 Not Found

409 Conflict

500 Internal Server Error

40  

Example  PUT  Request   PUT /movies/1234 HTTP/1.1! Host: example.com! ...! ! ! ! !The Godfather! !...! !

41  

PUT  Seman6cs   •  PUT  creates  a  new  resource   •  The  client  decides  on  the  resource’s  URI   •  PUT  is  idempotent   –  mul6ple  PUT  requests  have  no  side  effects   –  but  it  changes  the  resource  state  

42  

Create  with  PUT  or  POST?   •  The  generic  answer:  it  depends  J   •  Considera6ons   –  PUT  if  client   •  can  decide  on  the  URI   •  sends  complete  representa6on  to  the  server  

–  POST  if  server  creates  the  URI  (algorithmically)   –  some  firewalls  only  allow  GET  and  POST   –  POST  is  common  prac6ce   43  

CREATE  with  PUT  Example   # Create Amazon S3 bucket! ! PUT / HTTP/1.1! Host: colorpictures.s3.amazonaws.com! Content-Length: 0! Date: Wed, 01 Mar 2009 12:00:00 GMT! Authorization: AWS 15B4D3461F177624206A:xQE0diMbLRepdf3YB+FIEXAMPLE=! ! # Add Object to a bucket! ! PUT /my-image.jpg HTTP/1.1! Host: colorpictures.amazonaws.com! Date: Wed, 12 Oct 2009 17:50:00 GMT!

44  

Uniform  Interface   •  READ  an  exis6ng  resource  with  HTTP  GET   Client

Server

GET /movies/1234 HTTP/1.1 Host: example.com

200 OK

404 Not Found

500 Internal Server Error

45  

Example  GET  Request  /  Response   Request:! ! GET /movies/1234 HTTP/1.1! Host: example.com! Accept: application/xml! ...! ! Response:! ! HTTP/1.1 200 OK! Date: ...! Content-Type: application/xml! ! ! ! !The Godfather! !...! ! 46  

GET  Seman6cs   •  GET  retrieves  the  representa6on  (  =  the   current  state)  of  a  resource   •  GET  is  safe  (implies  idempotent)   –  does  not  change  state  of  resource   –  has  no  side-­‐effects  

•  If  GET  goes  wrong   –  GET  it  again!   –  no  problem  because  it  safe  (and  idempotent)   47  

Uniform  Interface   •  UPDATE  an  exis6ng  resource  with  HTTP  PUT   Client

Server

PUT /movies/1234 HTTP/1.1 Host: example.com

200 OK

404 Not Found

409 Conflict

500 Internal Server Error

48  

When  PUT  goes  wrong   •  If  we  get  5xx  error,  or  some  4xx  errors   –  simply  PUT  again!   –  no  problem,  because  PUT  is  idempotent  

•  If  we  get  errors  indica6ng  incompa6ble  states   then  do  some  forward/backward  compensa6on   work  and  maybe  PUT  again   –  409  Conflict  (e.g.,  change  your  username  to  a  name   that  is  already  taken)   –  417  Expecta6on  Failed  (the  server  won’t  accept  your   representa6on  –  fix  it,  if  possible)   49  

Uniform  Interface   •  DELETE  an  exis6ng  resource  with  HTTP  DELETE   Client

Server

DELETE /movies/1234 HTTP/1.1 Host: example.com

200 OK

404 Not Found

405 Method Not Allowed

500 Internal Server Error

50  

DELETE  Seman6cs   •  Stop  the  resource  from  being  accessible   –  logical  delete   –  not  necessarily  physical  

•  If  DELETE  goes  wrong   –  try  it  again!   –  DELETE  is  idempotent    

51  

The  Resource-­‐Oriented  Architecture   •  A  set  of  design  principles  for  building  RESTful   Web  Services   –  Addressability   –  Uniform  interface   –  Connectedness   –  Statelessness  

52  

Connectedness   •  In  RESTful  services,  resource  representa6ons  are   hypermedia   •  Served  documents  contain  not  just  data,  but  also  links   to  other  resources   HTTP/1.1 200 OK! Date: ...! Content-Type: application/xml! ! ! ! !The Godfather! !...! !http://example.com/actors/567! !

53  

The  Resource-­‐Oriented  Architecture   •  A  set  of  design  principles  for  building  RESTful   Web  Services   –  Addressability   –  Uniform  interface   –  Connectedness   –  Statelessness  

54  

Statelessness   •  Statelessness  =  every  HTTP  request  executes   in  complete  isola6on   •  The  request  contains  all  the  informa6on   necessary  for  the  server  to  fulfill  that  request   •  The  server  never  relies  on  informa6on  from  a   previous  request   –  if  informa6on  is  important  (e.g.,  user-­‐ authen6ca6on),  the  client  must  send  it  again   55  

Statelessness   •  This  constraint  does  not  say  “stateless  applica6ons”!   –  for  many  RESTful  applica6ons,  state  is  essen6al   –  e.g.,  shopping  carts  

•  It  means  to  move  state  to  clients  or  resources   •  State  in  resources  

–  the  same  for  every  client  working  with  the  service   –  when  a  client  changes  resource  state  other  clients  see  this   change  as  well  

•  State  in  clients  (e.g.,  cookies)  

–  specific  to  client  and  has  to  be  maintained  by  each  client   –  makes  sense  for  maintaining  session  state  (login  /  logout)   56  

State  in  the  Applica6on  

©  Erik  Wilde:  hMp://dret.net/netdret/docs/rest-­‐icwe2010/   57  

Statelessness  

©  Erik  Wilde:  hMp://dret.net/netdret/docs/rest-­‐icwe2010/   58  

Statelessness  

©  Erik  Wilde:  hMp://dret.net/netdret/docs/rest-­‐icwe2010/   59  

Tools  and  Frameworks   •  Ruby  on  Rails  -­‐  a  framework  for  building  RESTful  Web  applica6ons   –  hMp://www.rubyonrails.org/   •  Restlet  -­‐  framework  for  mapping  REST  concepts  to  Java  classes   –  hMp://www.restlet.org   •  Django  -­‐  framework  for  building  RESTful  Web  applica6ons  in  Python   •  JAX-­‐RC  specifica6on  (hMp://jsr311.java.net/)  provides  a  Java  API  for   RESTful  Web  Services  over  the  HTTP  protocol.   •  RESTEasy  (hMp://www.jboss.org/resteasy/)  -­‐  JBoss  project  that  provides   various  frameworks  for  building  RESTful  Web  Services  and  RESTful  Java   applica6ons.  Fully  cer6fied  JAX-­‐RC  implementa6on.  

60  

Readings   •  Fielding,  Roy:  Architectural  Styles  and  the   Design  of  Network-­‐based  Sogware   Architectures  (Chapters  4-­‐6):   hMp://www.ics.uci.edu/~fielding/pubs/ disserta6on/top.htm   •  Tutorial  Design  Principles,  PaMerns  and   Emerging  Technologies  for  RESTful  Web   Services  (Cesare  Pautasso  and  Erik  Wilde):   hMp://dret.net/netdret/docs/rest-­‐icwe2010/    

61  

INFO/CS 4302 Web Informa/on Systems - Cornell University

exposes a URI for every piece of informa/on it might serve. – which is usually an infinite number of URIs. 27 ... With HTTP we have all methods we need to manipulate Web resources (CRUD interface). – Create = POST (or PUT). – Read = GET. – Update = PUT. – Delete = DELETE. Client. Server. SQL. Client. HTTP. Web.

5MB Sizes 1 Downloads 252 Views

Recommend Documents

INFO/CS 4302 Web Informa/on Systems - Cornell University
Key Architectural Components. – Iden/fica/on: ??? – Interac/on: ??? – Standardized Document ... Specifies how software components communicate with each other. – e.g., Java API, 3rd party library APIs ... The Resource-‐Oriented Architecture.

Cornell University - Cornell eCommons
incorporates links to universities, programs, and resources worldwide as well as a database of cost estimates. ...... Link is a database of more than 500 college alumni who have offered to help students and alumni with their ...... programming, GIS,

Social language processing - Cornell blogs - Cornell University
example of a high-level feature would be the degree of cohesion in deceptive texts, since liars are expected ...... in Arabic from the Internet. There was a total of ...

Social language processing - Cornell blogs - Cornell University
cate with the public the narrative that defines their cause. The content and style ...... links to al-Qaeda) and non-false statements (e.g. that Hussein had used gas on his own people) produced ... Iraqi Intelligence Service (IIS). In one report by a

YoungHwa Seok Resume - Cornell University
CONTACT INFORMATION. Charles H. Dyson School of ... University, 2015. LL.M. in Business Administration and Law, Yonsei University (South Korea), 2005.

Jigsaw Image Mosaics - Cornell Computer Science - Cornell University
Dept. of Computer Science, Cornell University. Program of Computer Graphics, Cornell University. ABSTRACT. This paper introduces a new kind of mosaic, ...

The Cornell University Glee Club - Sign in
On Saturday, January 10th at 7:30 PM, the Cornell University Glee Club will ... office can be accessed online at www.kennedy-center.org (use the calendar to ... prime tickets in a select block that will not be sold to the public until December 5th,.

cornell university press - Salzburg
a driving force of economic globalization. In Protection for Exporters, ... —John S. Odell, Professor and Director, School of International Relations,. University of ...

Sponsored Search Equilibria for Conservative ... - Cornell University
New Structural Characterization v i. E[α σ(i). |v i. ] + E[α μ(i) v πμ (i). |v i. ] ≥ ¼ v i. E[α μ(i). |v i. ] Lemma: • Player i gets k or better if he bids > b π i. (k). • But this is a random variable … • Deviation bid: 2 E[b π

pdf-0979\the-triangle-fire-publisher-cornell-university-press ...
Try one of the apps below to open or edit this item. pdf-0979\the-triangle-fire-publisher-cornell-university-press-centennial-edition-by-leon-stein.pdf.

Social Network Theory and the Claim that ... - Cornell University
Shakespeare and nineteen of his peers considered as a social network. ..... solicitor and Town Clerk of Stratford for ten years was Londoner Thomas Greene.

cornell university “synthetic” evolution submitted to ...
our paradigm of evolution in a simulated environment, an experiment in which (since we control .... like shape memory alloys and Piezo-electric elements.

Statement to PBG, Cornell University, Wednesday, February 26, 2014 ...
Retrying... Statement to PBG, Cornell University, Wednesday, February 26, 2014.pdf. Statement to PBG, Cornell University, Wednesday, February 26, 2014.pdf.

czechcasting 4302 tereza.pdf
чÐμшÑÐoÐ3⁄4Ðμ. пÐ3⁄4Ñ€Ð1⁄2Ð3⁄4. Czechcasting 13.02.25 tereza pornorips. Stahni.si. damn good file hosting. Czech casting tereza 0700 free porn ...

Cornell Template_generic.pdf
Download. Connect more apps... Try one of the apps below to open or edit this item. Cornell Template_generic.pdf. Cornell Template_generic.pdf. Open. Extract.

courses_of_study_2009_10.pdf - ECommons@Cornell
Use of Animals in the Biological Sciences Curriculum: Cornell. University 168 ...... computer programming for students in the. College of ... Students who have studied a language for two or more years ...... to www.bscb.cornell.edu/majReq.php).

courses_of_study_2009_10.pdf - eCommons @ Cornell
B07 Day Hall, Ithaca, NY 14853-2801, 607 255-4232, e-mail: [email protected]. Photography by CU ...... 3 credits and placement out of one first-year writing seminar. 6. 3 credits ...... program director www.nutrition.cornell.edu/grad/cfnpp.html.

Recommender Systems - ePrints Soton - University of Southampton
that no one technique is best for all users in all situations. Thus we believe that ... ordinate the various recommendations so that only the best of them (from ...... ing domain issues such as quality, style and other machine unparsable ... IEE Proc

Survivable Information Storage Systems - Carnegie Mellon University
xFS,1 NASD,2 and Petal.3 These systems all provide applications with a single, ... such as RAID (redundant array of independent disks)4 ensuring scalable ...

Coordinate Systems - msdis - University of Missouri
Computer Precision ~ Continued ... Actual numeric ranges vary between computer implementations .... Since latitude and longitude are measured in degrees,.

pdf-1829\songs-from-the-hill-a-history-of-the-cornell-university-glee ...
Try one of the apps below to open or edit this item. pdf-1829\songs-from-the-hill-a-history-of-the-cornell-university-glee-club-by-michael-slon.pdf.

Web Content Manager - ICARDA Corporate Systems
Closing date: 08 March 2016. Main purpose of the position ... Coordinate web, online and social media projects across departments. •. Maintain a consistent look ...