RESTful Para todos by Diego Sapriza

67
RESTful 4 all Diego Sapriza [email protected] @AV4TAr

description

RESTful Para todos Diego Sapriza .NET Conf UY 2014

Transcript of RESTful Para todos by Diego Sapriza

Page 1: RESTful Para todos by Diego Sapriza

RESTful 4 allDiego Sapriza

[email protected]@AV4TAr

Page 2: RESTful Para todos by Diego Sapriza

Como diseñar una API RESTful sin morir en el intento.

Page 3: RESTful Para todos by Diego Sapriza

Hi!Diego SaprizaI’M

@AV4TAr

Page 4: RESTful Para todos by Diego Sapriza
Page 5: RESTful Para todos by Diego Sapriza

PHP.meetup.uy

DevOps.meetup.uy

..uy

Page 6: RESTful Para todos by Diego Sapriza

“El mundo evoluciona constantemente”

Page 7: RESTful Para todos by Diego Sapriza

Restful api

versionamiento

recursos

status codes

autenticación mensajes

paginación documentación!

hypermedia (HATEOAS)

verbos tools

Page 8: RESTful Para todos by Diego Sapriza

Quiénes hacen mayoritariamente

Web dev?

Page 9: RESTful Para todos by Diego Sapriza

REpresentational State transfer

no es un estándar

Page 10: RESTful Para todos by Diego Sapriza

Acuerdo

https://www.flickr.com/photos/124247024@N07/

Page 11: RESTful Para todos by Diego Sapriza
Page 12: RESTful Para todos by Diego Sapriza

Roy Fielding escalabilidad

:(

Page 13: RESTful Para todos by Diego Sapriza

restricciones escalabilidadClient-Server

Stateless

Cache

Uniform Interfaces Layered System

Code on demand (opcional)

Page 14: RESTful Para todos by Diego Sapriza

Richardson Maturity Modelhttp://bit.ly/api-rmm

Page 15: RESTful Para todos by Diego Sapriza

0: The Swamp of POX

RPC  SOBRE  HTTP  

Page 16: RESTful Para todos by Diego Sapriza

JSON XML html images

Page 17: RESTful Para todos by Diego Sapriza

0: The Swamp of POX

GET    http://srv.com/addin/auto-­‐harvest/end-­‐job/:id/

errors/:errors_messages  

http://srv.com/addin/auto-­‐harvest/start-­‐job/:id

Page 18: RESTful Para todos by Diego Sapriza

Richardson Maturity Modelhttp://bit.ly/api-rmm

Page 19: RESTful Para todos by Diego Sapriza

Uniform Interfaces

• Identificación recursos.

• Manipulación de recursos a través de su representación.

• Mensajes auto-descriptivos.

• Hypermedia como motor del estado de la aplicación (HATEOAS).

Page 20: RESTful Para todos by Diego Sapriza

1: RECURSOS

tienen una URI

mapean entidad/es

sustantivos

Page 21: RESTful Para todos by Diego Sapriza

uri

scheme:hierarchical part[?query][#fragment]

telnet://192.168.1.1

urn:isbn:978-1-449-3150-9

mailto:[email protected]

https://api.twilio.com/2010-­‐04-­‐01

Page 22: RESTful Para todos by Diego Sapriza

identificación

/recurso/:id  

/recurso/:id/:acción  

!

/?r=recurso&id=:id  

/?r=recurso&id=:id&a=:acción

Page 23: RESTful Para todos by Diego Sapriza

colecciones

/recursos  

/recursos/:id  

/recursos/:id?pagina=:n&limite=100

Page 24: RESTful Para todos by Diego Sapriza

Richardson Maturity Model

Page 25: RESTful Para todos by Diego Sapriza

Uniform Interfaces

• Identificación recursos.

• Manipulación de recursos a través de su representación.!

• Mensajes auto-descriptivos.

• Hypermedia como motor del estado de la aplicación (HATEOAS).

Page 26: RESTful Para todos by Diego Sapriza

Representación

Page 27: RESTful Para todos by Diego Sapriza

HTTP verbsGet Post Put Delete

Patch Options Head Trace Connect

http://bit.ly/http-­‐request-­‐methods

Page 28: RESTful Para todos by Diego Sapriza

crud http

CREATE POST

READ GET

UPDATE PUT

DELETE DELETE

Page 29: RESTful Para todos by Diego Sapriza

método seguro idempotente cachable

GET

HEAD

POST

PUT

DELETE

Page 30: RESTful Para todos by Diego Sapriza

GET /personas Obtener  lista  de  personas

POST /personas Agregar  una  persona

DELETE /personas/:id Eliminar  una  persona

GET /personas/:id Obtener  una  persona

PUT /personas/:id Actualizar  una  persona

GET /personas/:id/contactos Obtener  los  contactos  de  una  persona

POST /personas/:id/contactos Agregar  un  contacto  a  una  persona

POST /personas/subirImagen Subir  una  imagen

Page 31: RESTful Para todos by Diego Sapriza

y ahora… ¿qué hacemos con estos msjs?

Page 32: RESTful Para todos by Diego Sapriza

Richardson Maturity Model

Page 33: RESTful Para todos by Diego Sapriza

Documentar…

Page 34: RESTful Para todos by Diego Sapriza

Uniform Interfaces

• Identificación recursos.

• Manipulación de recursos a través de su representación.

• Mensajes auto-descriptivos.!

• Hypermedia como motor del estado de la aplicación (HATEOAS).

Page 35: RESTful Para todos by Diego Sapriza

auto-descriptivos

¿Cómo procesar el mensaje?

¿qué parser utilizar?

¿Caching?

Page 36: RESTful Para todos by Diego Sapriza

ahora… ¿cómo son los mensajes?

HAL Collection JSON Siren

Page 37: RESTful Para todos by Diego Sapriza

HTTP/1.1 200 OK!Content-Type: application/json!{! "status":"ok",! "message":"Data retrieved OK!",! "data" : [! !{! !! "id": 90, ! !! "modelId": 81, ! !! "path": "Somewhere over the rainbow.rvt"! !},! !{! !! "id": 91, ! !! "modelId": 13, ! !! "path": "Blue birds fly.rvt”! }]!}!

GET  http://server/addin/auto-­‐harvest/get-­‐jobs/ o_O

Page 38: RESTful Para todos by Diego Sapriza

HTTP/1.1 200 OK!Content-Type: application/json!{! "status":"error",! "message":"Page not found”,! "data" : []!}!

o_O

Page 39: RESTful Para todos by Diego Sapriza

status codes

2xx - Success

3xx - Redirection

4xx - Client Error

5xx - Server Error

Page 40: RESTful Para todos by Diego Sapriza

Error messages api-problem

HTTP/1.1 401 Unauthorized!Content-Type: application/problem+json!{!! "type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",!! "title": "Unauthorized",!! "status": 401,!! "detail": "Unauthorized",!! "authentication_uri": "/oauth"!}!

Page 41: RESTful Para todos by Diego Sapriza

Uniform Interfaces

• Identificación recursos.

• Manipulación de recursos a través de su representación.

• Mensajes auto-descriptivos.

• Hypermedia como motor del estado de la aplicación (HATEOAS).

Page 42: RESTful Para todos by Diego Sapriza

HATEOASClients make state transitions only through

actions that are dynamically identified within hypermedia by the server. !

Except for simple fixed entry points to the application, a client does not assume that any particular action is available for any

particular resources beyond those described in representations previously

received from the server.

Page 43: RESTful Para todos by Diego Sapriza

clase.php

public class Customer!{!! ! public $Id;!! ! public $Name;!}!

Page 44: RESTful Para todos by Diego Sapriza

clase.c#

public class Customer!{!! public int Id { get; set; }!! public string Name { get; set; }!}!

Page 45: RESTful Para todos by Diego Sapriza

json

{ ! "id" : "1"! "name" : "Diego"!}!

Page 46: RESTful Para todos by Diego Sapriza

HAL

http://bit.ly/hal-spec

{! "id": "diego",! "name": "Diego Sapriza”,!! "_links": {! "self": {! "href": "http://web.org/api/user/diego"! },! "website": {! "href": "http://web.org/api/locations/diego"! }! }!} !

Page 47: RESTful Para todos by Diego Sapriza

HAL{! .. *snip* ..! "_embedded": {! "website": {! "_links": {! "self": {! "href": “http://web.org/api/locations/diego"! }! },! "id": "diego",! "url": "http://diego.uy"! }! }!}!

Page 48: RESTful Para todos by Diego Sapriza

HAL - colecciones

{! "_links": {! "self" :{ "href": "http://web.org/api/user?page=3" },! "first":{ "href": "http://web.org/api/user" },! "prev" :{ "href": "http://web.org/api/user?page=2" },! "next" :{ "href": "http://web.org/api/user?page=4" },! "last" :{ "href": "http://web.org/api/user?page=133" }! },! "count": 3,! "total": 498,! ...!}

Page 49: RESTful Para todos by Diego Sapriza

https://api.github.com

Page 50: RESTful Para todos by Diego Sapriza

{! "current_user_url": "https://api.github.com/user",! "authorizations_url": "https://api.github.com/authorizations",! "emails_url": "https://api.github.com/user/emails",! "emojis_url": "https://api.github.com/emojis",! "events_url": "https://api.github.com/events",! "feeds_url": "https://api.github.com/feeds",! "following_url": "https://api.github.com/user/following{/target}",! "gists_url": "https://api.github.com/gists{/gist_id}",! "hub_url": "https://api.github.com/hub",! "issues_url": "https://api.github.com/issues",! "keys_url": "https://api.github.com/user/keys",! "notifications_url": "https://api.github.com/notifications",! "organization_url": "https://api.github.com/orgs/{org}",! "public_gists_url": "https://api.github.com/gists/public",! "rate_limit_url": "https://api.github.com/rate_limit",! "repository_url": "https://api.github.com/repos/{owner}/{repo}",! "starred_url": "https://api.github.com/user/starred{/owner}{/repo}",! "starred_gists_url": "https://api.github.com/gists/starred",! "team_url": "https://api.github.com/teams",! "user_url": "https://api.github.com/users/{user}",! "user_organizations_url": "https://api.github.com/user/orgs",! ...!}!

Page 51: RESTful Para todos by Diego Sapriza

Richardson Maturity Modelhttp://bit.ly/api-rmm

Page 52: RESTful Para todos by Diego Sapriza

Sigamos links

Page 53: RESTful Para todos by Diego Sapriza

Restful api

versionamiento!recursos

status codes

autenticación!mensajes

paginación

documentación!hypermedia (HATEOAS)

verbos tools

Page 54: RESTful Para todos by Diego Sapriza

Versiona tu API

Page 55: RESTful Para todos by Diego Sapriza

HTTP  GET  https://web.com/api/v1/users/diego

Page 56: RESTful Para todos by Diego Sapriza

HTTP  GET  https://web.com/api/users/diego  api-­‐version:  2

Page 57: RESTful Para todos by Diego Sapriza

HTTP  GET  https://web.com/api/users/diego  Accept:  application/vnd.myapi.v2+json

Page 58: RESTful Para todos by Diego Sapriza

HTTP  GET  https://web.com/api/users/diego

por defecto última versión Headers para versiones anteriores

Page 59: RESTful Para todos by Diego Sapriza

autenticación

Page 60: RESTful Para todos by Diego Sapriza

crea tu propio método

Page 61: RESTful Para todos by Diego Sapriza

HTTP Basic Auth !

HTTP Digest !

OAuth2

Page 62: RESTful Para todos by Diego Sapriza

tools

Postman

Runscope

jsonmate.com

Page 63: RESTful Para todos by Diego Sapriza

Restful api

versionamiento

recursos

status codes

autenticación

mensajes

paginación

documentación!hypermedia (HATEOAS)

verbos tools

Page 64: RESTful Para todos by Diego Sapriza
Page 65: RESTful Para todos by Diego Sapriza

@AV4TAr http://AV4TAr.com

https://speakerdeck.com/av4tar/restful-para-todos

Page 66: RESTful Para todos by Diego Sapriza
Page 67: RESTful Para todos by Diego Sapriza

• http://www.troyhunt.com/2014/02/your-api-versioning-is-wrong-which-is.html

• http://martinfowler.com/articles/richardsonMaturityModel.html

• http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api

• http://spf13.com/post/soap-vs-rest

• https://leanpub.com/build-apis-you-wont-hate

• https://speakerdeck.com/caseysoftware/on-the-edge-of-hypermedia-midwest-dot-io