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

REST not equals REST

REST not equals REST

REST is a defacto standard and every developer implements REST APIs in a different way. In this talk I'll show best practices and standards around REST. I hope that helps you to build APIs with higher developer experience.

Claudio Altamura

December 12, 2019
Tweet

More Decks by Claudio Altamura

Other Decks in Programming

Transcript

  1. DEVELOPER EXPERIENCE Why does it matter? users of your technology

    are happier promote it more stay longer when the product https://hackernoon.com/the-best-practices-for-a-great-developer-experience-dx-9036834382b0
  2. RESOURCE NAMING Use nouns to represent resources plural spinal-case or

    snake_case hierarchical relationships with slash GET /accounts/1234 GET /accounts/{id}/settings
  3. URI STRUCTURE foo://example.com:8042/over/there?name=ferret#nose \_/ \______________/\_________/ \_________/ \__/ | | |

    | | scheme authority path query fragment https://www.ietf.org/rfc/rfc3986.txt
  4. HTTP METHODS 1 returns a list returns a single resource

    creates a resource GET /accounts GET /accounts/1 POST /accounts
  5. HTTP METHODS 2 updates a resource modi es a resource

    deletes a resource PUT /accounts/1 PATCH /accounts/1 DELETE /accounts/1
  6. HTTP STATUS CODES SUCCESS CODES code comment 200 OK 201

    CREATED 202 ACCEPTED 204 NO CONTENT 206 PARTIAL CONTENT
  7. CLIENT ERRORS 2 code comment 405 METHOD NOT ALLOWED 406

    NOT ACCEPTABLE 409 CONFLICT 410 GONE 429 TOO MANY REQUESTS
  8. LEVEL 1 many URIs only a single HTTP verb –

    generally HTTP POST POST http://myws.com/api/createAccount POST http://myws.com/api/updateAccount POST http://myws.com/api/deleteAccount
  9. LEVEL 2 URI for every resource using HTTP Verbs, e.g.

    GET, POST, PUT, DELETE GET http://myws.com/api/accounts POST http://myws.com/api/accounts/2 PUT http://myws.com/api/accounts/2 DELETE http://myws.com/api/accounts/2
  10. HATEOAS LINKS WITH METHODS Paypals HATEOAS links { "links": [{

    "href": "https://api.paypal.com/v1/payments/sale/36C38912MN9658832", "rel": "self", "method": "GET" }, { "href": "https://api.paypal.com/v1/payments/sale/36C38912MN9658832/refund" "rel": "refund", "method": "POST" }, { "href": "https://api.paypal.com/v1/payments/payment/PAY-5YK922393D847794YK "rel": "parent_payment", "method": "GET" }] }
  11. STANDARDS JSON API Spec HTTP HTTP Status Codes URI Syntax

    HATEOAS HAL https://jsonapi.org/ https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol https://en.wikipedia.org/wiki/List_of_HTTP_status_codes https://www.ietf.org/rfc/rfc3986.txt https://en.wikipedia.org/wiki/HATEOAS https://en.wikipedia.org/wiki/Hypertext_Application_Language https://tools.ietf.org/html/draft-kelly-json-hal-00
  12. APIS Kubernetes Gitlab Rocket.Chat Rancher GitHub Twitter https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.16/ https://docs.gitlab.com/ee/api/api_resources.html https://rocket.chat/docs/developer-guides/rest-api/

    https://rancher.com/docs/rancher/v2.x/en/api/ https://developer.github.com/v3/ https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/get- statuses-retweets-id