Slide 1

Slide 1 text

Alex Tercete [email protected] REST Definições e Boas Práticas

Slide 2

Slide 2 text

HTTP Firulas +

Slide 3

Slide 3 text

HTTP Recursos +

Slide 4

Slide 4 text

Recursos

Slide 5

Slide 5 text

Dog https://www.flickr.com/photos/mcpig/2203669161

Slide 6

Slide 6 text

POST /dogs GET /dogs/:id PUT /dogs/:id DELETE /dogs/:id

Slide 7

Slide 7 text

dog vs. dogs

Slide 8

Slide 8 text

POST /dogs/:id/feed POST /dogs/:id/throwBone GET /dogs/:id/showOwner POST /dogs/:id/takeOut

Slide 9

Slide 9 text

Verbos NÃO

Slide 10

Slide 10 text

Substantivos SIM

Slide 11

Slide 11 text

feed throwBone showOwner takeOut food  meal bone  game owner walk  exercise

Slide 12

Slide 12 text

POST /dogs/:id/meals POST /dogs/:id/games GET /dogs/:id/owners POST /dogs/:id/exercises

Slide 13

Slide 13 text

GET /dogs/getByFurColor GET /dogs/getByOwner

Slide 14

Slide 14 text

Query String

Slide 15

Slide 15 text

GET /dogs?furColor=:color GET /dogs?owner=:owner

Slide 16

Slide 16 text

GET /dogs?furColor=:color GET /owners/:id/dogs

Slide 17

Slide 17 text

Verbos

Slide 18

Slide 18 text

GET POST PUT DELETE id id ? id id id id

Slide 19

Slide 19 text

Cria Obtém Altera Remove GET POST PUT DELETE

Slide 20

Slide 20 text

Method Definitions RFC2616, Seção 9 http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

Slide 21

Slide 21 text

Status Code

Slide 22

Slide 22 text

Redirection Successful Client Error Server Error 2XX 3XX 4XX 5XX

Slide 23

Slide 23 text

Não é aqui. Tá tudo BEM! Você tá MAL! Eu tô MAL! 2XX 3XX 4XX 5XX

Slide 24

Slide 24 text

http://httpstatusdogs.com/

Slide 25

Slide 25 text

http://httpstatusdogs.com/

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

HTTP/1.1 201 Created Location: /dogs/Snoopy POST /dogs HTTP/1.1 Content-Type: application/json { "name": "Snoopy" }

Slide 30

Slide 30 text

HTTP/1.1 301 Moved Permanently Location: /composers/Beethoven GET /dogs/Beethoven HTTP/1.1

Slide 31

Slide 31 text

HTTP/1.1 401 Unauthorized WWW-Authenticate: Basic GET /dogs HTTP/1.1

Slide 32

Slide 32 text

HTTP/1.1 405 Method Not Allowed Allow: GET, POST DELETE /dogs HTTP/1.1

Slide 33

Slide 33 text

Status Code Definitions RFC2616, Seção 10 http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

Slide 34

Slide 34 text

Cache

Slide 35

Slide 35 text

HTTP/1.1 200 OK Cache-Control: private, max-age=300 Expires: Tue, 18 Mar 2014 19:00:00 GMT ETag: 50780f47f6839d47d60bc4555ee00c3f GET /dogs HTTP/1.1

Slide 36

Slide 36 text

HTTP/1.1 304 Not Modified Cache-Control: private, max-age=300 Expires: Tue, 18 Mar 2014 19:00:00 GMT ETag: 50780f47f6839d47d60bc4555ee00c3f GET /dogs HTTP/1.1 If-None-Match: 50780f47f6839d47d60bc4555ee00c3f

Slide 37

Slide 37 text

Caching in HTTP RFC2616 http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html

Slide 38

Slide 38 text

Versionamento

Slide 39

Slide 39 text

GET /v1/dogs HTTP/1.1 GET /dogs HTTP/1.1 X-VTEX-Zoo-Version: v1 GET /dogs HTTP/1.1 Accept: application/vnd.vtex.zoo-v1+json Rota Cabeçalho Proprietário Cabeçalho Padrão

Slide 40

Slide 40 text

Autenticação

Slide 41

Slide 41 text

GET /dogs HTTP/1.1 Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ== GET /dogs HTTP/1.1 Authorization: AWS AKIAIOSFODNN7EXAMPLE:fr...Dzg= GET /dogs HTTP/1.1 X-VTEX-Api-AppKey: some-key X-VTEX-Api-AppToken: 5Om3tOkE|\| Padrão Proprietária Muito Proprietária

Slide 42

Slide 42 text

The OAuth 2.0 Authorization Framework RFC6749 http://tools.ietf.org/html/rfc6749

Slide 43

Slide 43 text

Tratamento de Erros

Slide 44

Slide 44 text

HTTP/1.1 401 Unauthorized WWW-Authenticate: Basic Content-Type: application/json { “errorCode”: “auth.invalid_credentials”, “message”: “Credenciais inválidas”, “help”: “http://docs.vtex.com/api/authentication” }

Slide 45

Slide 45 text

HTTP/1.1 400 Bad Request Content-Type: application/json { “errorCode”: “checkout.invalid_order”, “message”: “Dados do pedido inválidos”, “reasons”: { “id”: “É obrigatório” } }

Slide 46

Slide 46 text

Hypermedia

Slide 47

Slide 47 text

HTTP/1.1 200 OK Content-Type: application/json { “name”: “Snoopy” “owners”: [{ “name”: “Charlie Brown” }] } GET /dogs/Snoopy HTTP/1.1 Accept: application/json

Slide 48

Slide 48 text

HTTP/1.1 200 OK Content-Type: application/json { “name”: “Snoopy”, “owners”: { “href”: “/dogs/Snoopy/owners” } } GET /dogs/Snoopy HTTP/1.1 Accept: application/json

Slide 49

Slide 49 text

Virtualização

Slide 50

Slide 50 text

Camada de Virtualização da API API API API Aplicação

Slide 51

Slide 51 text

Considerações Finais

Slide 52

Slide 52 text

Resumo • REST = HTML + Recursos • Verbos, não! Substantivos, sim! • Status Codes: – 20[014], 30[124], 4(0[13459]|2[29]), 50[03] • Cache • Versionamento: – Accept • Autenticação: – OAuth 2.0 • Hypermedia: – href • Virtualização

Slide 53

Slide 53 text

Bons exemplos • http://developer.github.com/v3/ • http://www.twilio.com/docs/api/rest • https://developer.paypal.com/webapps/develop er/docs/api/ • http://developer.netflix.com/docs

Slide 54

Slide 54 text

Maus exemplos • http://www.flickr.com/services/api/request.rest. html • https://dev.twitter.com/docs/api/1.1 • https://developers.facebook.com/docs/graph- api/reference

Slide 55

Slide 55 text

Referências • http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm • Apigee – https://blog.apigee.com/detail/restful_api_design_nouns_are_good_verbs_are_bad – https://blog.apigee.com/detail/restful_api_design_plural_nouns_and_concrete_name s – https://blog.apigee.com/detail/simplify_associations_sweep_complexities_under_the _http – https://blog.apigee.com/detail/restful_api_design_what_about_errors – https://blog.apigee.com/detail/restful_api_design_chatty_apis – https://blog.apigee.com/detail/restful_api_design_complement_with_sdk – https://blog.apigee.com/detail/restful_api_design_api_virtualization

Slide 56

Slide 56 text

Referências (2) • http://blogs.burnsidedigital.com/2013/07/whats-a-rest-api/ • Steve Klabnik – http://blog.steveklabnik.com/posts/2011-07-03-nobody-understands-rest-or- http – http://blog.steveklabnik.com/posts/2011-08-07-some-people-understand- rest-and-http – http://blog.steveklabnik.com/posts/2012-02-23-rest-is-over – http://blog.steveklabnik.com/posts/2012-02-13-an-api-ontology – http://timelessrepo.com/haters-gonna-hateoas • http://barelyenough.org/blog/2008/05/versioning-rest-web-services/ • http://www.mobify.com/blog/beginners-guide-to-http-cache-headers/

Slide 57

Slide 57 text

Alex Tercete [email protected] Obrigado!