restful 4 all - net conf uynetconf.uy/content/files/2014/materials/diegosapriza.restful4all.pdf ·...

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

Upload: lekhuong

Post on 19-Sep-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

RESTful 4 allDiego Sapriza

[email protected]@AV4TAr

Page 2: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

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

Page 3: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

Hi!Diego SaprizaI’M

@AV4TAr

Page 4: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación
Page 5: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

PHP.meetup.uy

DevOps.meetup.uy

..uy

Page 6: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

“El mundo evoluciona constantemente”

Page 7: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

Restful api

versionamiento

recursos

status codes

autenticación mensajes

paginación documentación!

hypermedia (HATEOAS)

verbos tools

Page 8: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

Quiénes hacen mayoritariamente

Web dev?

Page 9: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

REpresentational State transfer

no es un estándar

Page 10: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

Acuerdo

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

Page 11: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación
Page 12: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

Roy Fielding escalabilidad

:(

Page 13: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

restricciones escalabilidadClient-Server

Stateless

Cache

Uniform Interfaces Layered System

Code on demand (opcional)

Page 14: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

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

Page 15: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

0: The Swamp of POX

RPC  SOBRE  HTTP  

Page 16: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

JSON XML html images

Page 17: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

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 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

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

Page 19: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

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 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

1: RECURSOS

tienen una URI

mapean entidad/es

sustantivos

Page 21: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

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 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

identificación

/recurso/:id  

/recurso/:id/:acción  

!

/?r=recurso&id=:id  

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

Page 23: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

colecciones

/recursos  

/recursos/:id  

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

Page 24: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

Richardson Maturity Model

Page 25: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

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 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

Representación

Page 27: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

HTTP verbsGet Post Put Delete

Patch Options Head Trace Connect

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

Page 28: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

crud http

CREATE POST

READ GET

UPDATE PUT

DELETE DELETE

Page 29: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

método seguro idempotente cachable

GET

HEAD

POST

PUT

DELETE

Page 30: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

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 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

y ahora… ¿qué hacemos con estos msjs?

Page 32: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

Richardson Maturity Model

Page 33: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

Documentar…

Page 34: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

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 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

auto-descriptivos

¿Cómo procesar el mensaje?

¿qué parser utilizar?

¿Caching?

Page 36: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

ahora… ¿cómo son los mensajes?

HAL Collection JSON Siren

Page 37: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

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 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

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

o_O

Page 39: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

status codes

2xx - Success

3xx - Redirection

4xx - Client Error

5xx - Server Error

Page 40: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

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 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

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 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

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 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

clase.php

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

Page 44: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

clase.c#

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

Page 45: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

json

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

Page 46: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

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 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

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

Page 48: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

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 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

https://api.github.com

Page 50: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

{! "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 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

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

Page 52: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

Sigamos links

Page 53: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

Restful api

versionamiento!recursos

status codes

autenticación!mensajes

paginación

documentación!hypermedia (HATEOAS)

verbos tools

Page 54: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

Versiona tu API

Page 55: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

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

Page 56: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

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

Page 57: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

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

Page 58: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

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

por defecto última versión Headers para versiones anteriores

Page 59: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

autenticación

Page 60: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

crea tu propio método

Page 61: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

HTTP Basic Auth !

HTTP Digest !

OAuth2

Page 62: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

tools

Postman

Runscope

jsonmate.com

Page 63: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

Restful api

versionamiento

recursos

status codes

autenticación

mensajes

paginación

documentación!hypermedia (HATEOAS)

verbos tools

Page 64: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación
Page 65: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

@AV4TAr http://AV4TAr.com

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

Page 66: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación
Page 67: RESTful 4 all - NET Conf UYnetconf.uy/Content/files/2014/materials/DiegoSapriza.RESTful4all.pdf · Restful api versionamiento recursos status codes autenticación mensajes paginación

• 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