Slide 1

Slide 1 text

RESTful 4 all Diego Sapriza [email protected] @AV4TAr

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Hi!Diego Sapriza I’M @AV4TAr

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

PHP.meetup.uy DevOps.meetup.uy . .uy

Slide 6

Slide 6 text

“El mundo evoluciona constantemente”

Slide 7

Slide 7 text

Restful api versionamiento recursos status codes autenticación mensajes paginación documentación hypermedia (HATEOAS) verbos tools

Slide 8

Slide 8 text

Quiénes hacen mayoritariamente Web dev?

Slide 9

Slide 9 text

REpresentational State transfer técnica de arquitectura no es un estándar

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

Roy Fielding escalabilidad :(

Slide 13

Slide 13 text

restricciones escalabilidad Client-Server Stateless Cache Uniform Interfaces Layered System Code on demand (opcional)

Slide 14

Slide 14 text

Richardson Maturity Model http://bit.ly/api-rmm

Slide 15

Slide 15 text

0: The Swamp of POX RPC  SOBRE  HTTP  

Slide 16

Slide 16 text

JSON XML html images

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

Richardson Maturity Model http://bit.ly/api-rmm

Slide 19

Slide 19 text

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).

Slide 20

Slide 20 text

1: RECURSOS tienen una URI mapean entidad/es sustantivos

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

Richardson Maturity Model

Slide 25

Slide 25 text

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).

Slide 26

Slide 26 text

Representación

Slide 27

Slide 27 text

HTTP verbs Get Post Put Delete Patch Options Head Trace Connect http://bit.ly/http-­‐request-­‐methods

Slide 28

Slide 28 text

crud http CREATE POST READ GET UPDATE PUT DELETE DELETE

Slide 29

Slide 29 text

método seguro idempotente cachable GET HEAD POST PUT DELETE http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.1.2

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

y ahora… ¿qué hacemos con estos msjs?

Slide 32

Slide 32 text

Richardson Maturity Model

Slide 33

Slide 33 text

Documentar…

Slide 34

Slide 34 text

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).

Slide 35

Slide 35 text

auto-descriptivos ¿Cómo procesar el mensaje? ¿qué parser utilizar? ¿Caching?

Slide 36

Slide 36 text

ahora… ¿cómo son los mensajes? HAL Collection JSON Siren

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

status codes 2xx - Success 3xx - Redirection 4xx - Client Error 5xx - Server Error

Slide 40

Slide 40 text

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" }

Slide 41

Slide 41 text

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).

Slide 42

Slide 42 text

HATEOAS Clients 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.

Slide 43

Slide 43 text

clase.php public class Customer { public $Id; public $Name; }

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

HAL http://bit.ly/hal-spec { "id": "diego", "name": "Diego Sapriza”, "_links": { "self": { "href": "http://web.org/api/users/diego" }, "website": { "href": "http://web.org/api/locations/diego" } } }

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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, ... }

Slide 48

Slide 48 text

https://api.github.com

Slide 49

Slide 49 text

{ "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", ... } https://api.github.com

Slide 50

Slide 50 text

{ "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", ... }

Slide 51

Slide 51 text

Richardson Maturity Model http://bit.ly/api-rmm

Slide 52

Slide 52 text

Sigamos links

Slide 53

Slide 53 text

Restful api versionamiento recursos status codes autenticación mensajes paginación documentación hypermedia (HATEOAS) verbos tools

Slide 54

Slide 54 text

Versiona tu API

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

HTTP  GET   https://web.com/api/users/diego por defecto última versión Headers para versiones anteriores

Slide 59

Slide 59 text

autenticación

Slide 60

Slide 60 text

crea tu propio método

Slide 61

Slide 61 text

HTTP Basic Auth HTTP Digest OAuth2

Slide 62

Slide 62 text

tools Postman Runscope jsonmate.com

Slide 63

Slide 63 text

Restful api versionamiento recursos status codes autenticación mensajes paginación documentación hypermedia (HATEOAS) verbos tools

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

@AV4TAr http://AV4TAr.com https://speakerdeck.com/av4tar/restful-para-todos

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

We are hiring case.recruiterbox.com

Slide 68

Slide 68 text

• 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