2. REST
REST es un patrón de diseño
El término fue creado por Roy Fielding en su
disertación de Ph.D. en el año 2000
http://www.ics.uci.edu/~fielding/pubs/dissertation
/top.htm
3. Principio #1
Todo es un RECURSO
Ejemplos:
Clientes
Lugares
Productos
4. Principio #2
Los Recursos tienen NOMBRES
Ejemplos:
http://acme.com/customers/coyote
http://world.com/usa/california/san_diego
xri://@acme*warehouse/(+rockets)
5. Principio #3
Los recursos soportan VERBOS
simples
Ejemplos:
Create, Read, Update, Delete
POST, GET, PUT, DELETE
7. REST y HTTP
Get
Obtiene un Resource
Está garantido que no causa side-effects (Safe)
Cacheable
Post
Crea un nuevo recurso
Unsafe, el efecto de este verbo no esta definido por http
8. REST y HTTP
Put
Actualiza un recurso existente
Se usa para crear recursos si el cliente conoce el Uri
Se puede llamar N veces y pasa lo mismo (idempotente)
Delete
Elimina un recurso
Se puede llamar N veces y pasa lo mismo (idempotente)
9. REST y HTTP
Simple : basado en pocos principios y operaciones
bien definidas
Escalable: se apiya en protocolos „stateless‟ y estado
distribuido
En capas: premite cualquier cantidad de
intermediarios como proxies, gateways y firewalls
12. PURE REST
http:/../v1/Recurso/<Id>
POST, GET, DELETE, PUT
No:
http:/../v1/Recurso/get/Id
http:/../v1/Recurso?Id=1
http:/../v1/Recurso.svc?Id=1
http:/../v1/Search?q=keyword
GET
13. Content Type
GET /customer/1 HTTP/1.1
Accept: application/xml
HTTP/1.1 200 OK
Content-Type: application/xml
<Customer><Id>1</Id></Customer>
GET /customer/1 HTTP/1.1
Accept: text/html
HTTP/1.1 200 OK
Content-Type: text/html
<html xmlns="http://www.w3.org/1999/xhtml"> ….
GET /customer/1 HTTP/1.1
Accept: image/gif
HTTP/1.1 200 OK
Content-Type: image/gif
Gif89a
14. REST vs „Big Web Services‟
No Caching – HTTP Caching
WSDL vs WADL o /help)
UDDI vs Google Search
HTTPS y HTTP Auth vs WS-Security
WS-Federation vs protocolos abiertos tipo OpenID
WS-Transaction vs Nada
WS-Reliable Messaging vs Nada
AtMostOnce, AtLeastOne, ExactlyOnce, InOrder
POST Exactly Once?
15. REST Pragmático – Lo REST
Usar HTTP + XML como mecanismo de
comunicación entre capas
Ventajas:
Fácil de usar
Caching, Caching, Caching
16. Caching
Los GETs se pueden cachear
Cache-Control header
“no-cache”
“max-age=Segundos”, por ejemplo, “max-age=60”
Expires header
“Expires: Sat, 26 Jul 1997 05:00:00 GMT”
Pragma: no-cache
17. Conditional GET
GET /icon-cart.gif HTTP/1.1
HTTP/1.1 200 OK
Content-Length: 389
Content-Type: image/gif
Last-Modified: Fri, 08 Aug 2008 16:19:58 GMT
GET /icon-cart.gif HTTP/1.1
If-Modified-Since: Fri, 08 Aug 2008 16:19:58 GMT
HTTP/1.1 304 Not Modified
18. Conditional GET
GET /icon-cart.gif HTTP/1.1
HTTP/1.1 200 OK
Content-Length: 389
Content-Type: image/gif
ETag: "d0afa29638fec81:43a"
GET /icon-cart.gif HTTP/1.1
If-None-Match: "d0afa29638fec81:43a“
HTTP/1.1 304 Not Modified
19. Cache en el Browser
Configuración La copia del La copia del No hay
cache es nueva cache es vieja directivas de
cache
Cada vez que se No hay request Request Request
visita la página Condicional Condicional
Cada vez que se No hay request Request Request
inicia IE Condicional Condicional
Automática No hay request Request Heurística
Condicional
Nunca No hay request Request No hay request
Condicional
20. Heurística
Todas las siguientes condiciones:
El recurso cacheado tiene un Content-Type que empieza con
image/.
El recurso cacheado tiene Last-Modified.
La URL cacheada no tiene un „?‟
“The cached resource has been conditionally requested at least
once within the most recent 25 percent of its overall age in the
cache”
http://msdn.microsoft.com/en-us/library/aa905731.aspx
21. Conditional GET
GET /icon-cart.gif HTTP/1.1
HTTP/1.1 200 OK
Cache-Control: max-age=1
Content-Length: 389
Content-Type: image/gif
Last-Modified: Fri, 08 Aug 2008 16:19:58 GMT
GET /icon-cart.gif HTTP/1.1
If-Modified-Since: Fri, 08 Aug 2008 16:19:58 GMT
HTTP/1.1 304 Not Modified
22. REST in SL
Http Request son asincrónicos
SL 2 no permite leer headers de requests o requests
que retornen <> HTTP 2XX
No „304 Not Modified‟
No permite PUT/DELETE