Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RESTful para todos

RESTful para todos

Cómo diseñar APIs RESTful sin morir en el intento.

Vivimos en un mundo orientado a servicios interconectados. Las protagonistas de esta evolución: las APIs RESTful. Si tu aplicación está enfocada a servicios y al mundo entero tenés que saber cómo encararlas. Mostraré las mejores prácticas para diseñar APIs restful, desde su concepción hasta su ejecución, testing y documentación. Modelado de Entidades y colecciones, errores, estándares y mucho más. Con esta información estarás en condiciones de diseñar la API RESTful de tu próxima Startup multimillonaria :)

Realizada en:

- NetConf.uy Octubre 2014 (http://netconf.uy/)
- PHPmvd Noviembre 2014 (http://phpmvd.uy)

Diego Sapriza

October 03, 2014
Tweet

More Decks by Diego Sapriza

Other Decks in Technology

Transcript

  1. 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).
  2. 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).
  3. HTTP verbs Get Post Put Delete Patch Options Head Trace

    Connect http://bit.ly/http-­‐request-­‐methods
  4. 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
  5. 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).
  6. 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
  7. status codes 2xx - Success 3xx - Redirection 4xx -

    Client Error 5xx - Server Error
  8. 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" }
  9. 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).
  10. 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.
  11. 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" } } }
  12. HAL { .. *snip* .. "_embedded": { "website": { "_links":

    { "self": { "href": “http://web.org/api/locations/diego" } }, "id": "diego", "url": "http://diego.uy" } } }
  13. 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, ... }
  14. { "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
  15. { "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", ... }
  16. • 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