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

Web API's From Scratch - An Introduction to the API Development World

Web API's From Scratch - An Introduction to the API Development World

Build scalable API's is not an easy task and to do that, we need to understand how the web works, including clients and servers, after get this, we can build API's that are easy to use and understand by you and other developers.

Presented at 11th Gama Academy Experience.

Lucas Mendes

October 21, 2017
Tweet

More Decks by Lucas Mendes

Other Decks in Technology

Transcript

  1. Lucas Mendes | Software Architect | @devsdmf WEB API’S FROM

    SCRATCH AN INTRODUCTION TO THE API DEVELOPMENT WORLD.
  2. WEB API’S FROM SCRATCH URL’S ▸ https://www.nuvemshop.com.br ▸ https://www.nuvemshop.com.br/planos-e-precos ▸

    https://www.nuvemshop.com.br/blog?s=Loja+Virtual ▸ http://www.example.com:80/path/to/myfile.html? key1=value1&key2=value2#SomewhereInTheDocument
  3. WEB API’S FROM SCRATCH COMMON MIME-TYPES ▸ text/plain ▸ text/html

    ▸ image/jpeg ▸ image/png ▸ audio/mpeg ▸ video/mp4 ▸ application/json
  4. WEB API’S FROM SCRATCH HTTP METHODS ▸ GET ▸ HEAD

    ▸ POST ▸ PUT ▸ PATCH ▸ DELETE ▸ OPTIONS ▸ and some others…
  5. WEB API’S FROM SCRATCH GENERAL HEADERS ▸ Date: Wed, 21

    Oct 2015 07:28:00 GMT ▸ Cache-Control: public, max-age=31536000 ▸ Connection: keep-alive
  6. WEB API’S FROM SCRATCH REQUEST HEADERS ▸ Accept: text/html, application/xml,

    application/json ▸ Accept-Encoding: gzip, compress ▸ Accept-Language: pt-BR ▸ Cookie: PHPSESSID=123123123; csrftoken=u32t4o3tb3gg43; ▸ User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36 ▸ Referer: https://developer.mozilla.org/en-US/docs/Web/JavaScript
  7. WEB API’S FROM SCRATCH RESPONSE HEADERS ▸ Age: 60 ▸

    Location: /redirect ▸ Server: Apache/2.4.1 (Unix) ▸ Content-Encoding: gzip
  8. WEB API’S FROM SCRATCH ENTITY HEADERS ▸ Content-Type: text/html ▸

    Content-Length: 1024 ▸ Content-Language: pt-BR ▸ Content-Encoding: gzip
  9. WEB API’S FROM SCRATCH 1XX - INFORMATION RESPONSES ▸ 100

    Continue ▸ 101 Switching Protocol ▸ 102 Processing
  10. WEB API’S FROM SCRATCH 2XX - SUCCESSFUL RESPONSES ▸ 200

    OK ▸ 201 Created ▸ 202 Accepted ▸ 204 No Content ▸ 205 Reset Content
  11. WEB API’S FROM SCRATCH 3XX - REDIRECTION MESSAGES ▸ 300

    Multiple Choice ▸ 301 Moved Permanently ▸ 302 Found ▸ 304 Not Modified ▸ 307 Temporary Redirect
  12. WEB API’S FROM SCRATCH 4XX - CLIENT ERROR RESPONSES ▸

    400 Bad Request ▸ 401 Unauthorized ▸ 403 Forbidden ▸ 404 Not Found ▸ 405 Method Not Allowed ▸ Too many other codes…
  13. WEB API’S FROM SCRATCH 5XX - SERVER ERROR RESPONSES ▸

    500 Internal Server Error ▸ 501 Not Implemented ▸ 502 Bad Gateway ▸ 503 Service Unavailable ▸ 504 Gateway Timeout ▸ Too many others…
  14. WEB API’S FROM SCRATCH SAMPLE JSON DOCUMENT { "name": "Lucas

    Mendes", "company": "Tienda Nube", "age": 23, "weight": 88.0, "likeCoffee": true }
  15. WEB API’S FROM SCRATCH WHY JSON ? ▸ Simplicity ▸

    Readability ▸ Scalability ▸ Flexibility ▸ Is JavaScript !!
  16. WEB API’S FROM SCRATCH WHY REST ? ▸ Scalability ▸

    Generality ▸ Technology Independence ▸ Latency (Caching) ▸ Security ▸ Encapsulation
  17. WEB API’S FROM SCRATCH GETTING A COLLECTION RESOURCE Request: GET

    /customers
 Accept: application/json Response: 200 OK [
 {
 "name": "Lucas Mendes",
 "email": “[email protected]"
 }
 ]
  18. WEB API’S FROM SCRATCH GETTING AN INSTANCE RESOURCE Request: GET

    /customers/182
 Accept: application/json Response: 200 OK {
 "name": "Lucas Mendes",
 "email": "[email protected]",
 "age": 23,
 "ordersCount": 1,
 "active": true
 }
  19. WEB API’S FROM SCRATCH PUT FOR CREATE Identifier is known

    by the client: PUT /customers/clientSpecifiedId {
 "name": "Lucas Mendes",
 "email": "[email protected]",
 "age": 23
 }
  20. WEB API’S FROM SCRATCH PUT FOR UPDATE Full replacement: PUT

    /customers/existingId {
 "name": "Lucas Mendes",
 "email": "[email protected]",
 "age": 23
 }
  21. WEB API’S FROM SCRATCH POST FOR CREATE On a parent

    resource: POST /customers {
 "name": "Lucas Mendes",
 "email": "[email protected]",
 "age": 23
 } Response: 201 Created
 Location: https://api.tiendanube.com/customers/182
  22. WEB API’S FROM SCRATCH POST FOR UPDATE On instance resource:

    POST /customers/182 {
 "name": "Lucas Mendes",
 "email": "[email protected]",
 "age": 23
 } Response: 200 OK
  23. WEB API’S FROM SCRATCH UPDATE WITH PATCH Send only the

    changed fields: PATCH /customers/182 {
 "name": "John Doe"
 } Response: 200 OK
  24. WEB API’S FROM SCRATCH DELETE A RESOURCE Empty body in

    request and response: DELETE /customers/182 
 Response: 200 OK
  25. WEB API’S FROM SCRATCH RESOURCE FORMAT - MEDIA TYPES ▸

    Request: Accept header ▸ Response: Content-Type header ▸ application/json ▸ application/json, application/xml ▸ …
  26. WEB API’S FROM SCRATCH RESOURCE FORMAT - CAMEL CASE "JS"

    in "JSON" = JavaScript Wrong: {
 "user_name": "Lucas Mendes"
 } Right: {
 "userName": "Lucas Mendes”
 }
  27. WEB API’S FROM SCRATCH RESOURCE FORMAT - DATE/TIME/TIMESTAMP Use ISO-8601

    Example: {
 "createdTimestamp": "2017-10-20T21:39:37Z"
 }
  28. WEB API’S FROM SCRATCH RESPONSE BODY GET obvious What about

    POST ? Return the representation in the response when feasible Add override (?_body=false) for control
  29. WEB API’S FROM SCRATCH RESPONSE BODY - CONTENT NEGOTIATION Use

    Accept header Header values comma delimited in order of preference GET /orders/18572
 Accept: application/json, text/plain
  30. WEB API’S FROM SCRATCH RESPONSE BODY - RESOURCE EXTENSION GET

    /orders/18572.json
 GET /orders/18572.xml
 GET /orders/18572.csv … Conventionally overrides Accept header
  31. WEB API’S FROM SCRATCH REFERENCES (LINKING) ▸ Distributed Hypermedia is

    paramount! ▸ Every accessible Resource has a unique URL. ▸ Replace IDs when possible.
  32. WEB API’S FROM SCRATCH REFERENCES (LINKING) Instance Resource with Reference

    GET /orders/18572 {
 "customer": {
 "href": "https://api.tiendanube.com/v1/customers/182"
 }
 "itemsPrice": 165.72,
 "shippingPrice": 20.00,
 "totalPrice": 185.72
 }
  33. WEB API’S FROM SCRATCH PAGINATION ▸ Collection Resources must support

    pagination query parameters ▸ Offset ▸ Limit GET /stores?offset=50&limit=10
  34. WEB API’S FROM SCRATCH PARTIAL REPRESENTATION ▸ Provide content filtering

    by using query parameters GET /stores/57432?fields=email,domain
  35. WEB API’S FROM SCRATCH ERRORS ▸ As descriptive as possible

    ▸ As much information as possible ▸ Developers are your customers
  36. WEB API’S FROM SCRATCH ERRORS - SAMPLE ERROR RESPONSE Request:

    POST /coupons {
 …
 } Response: 400 Bad Request {
 "errorCode": 7601,
 "errorMessage": "The specified coupon code is already taken”,
 "moreInfo": "https://developers.tiendanube.com/api/docs/errors/12345"
 }
  37. WEB API’S FROM SCRATCH CACHE ▸ Use HTTP cache for

    client-side caching ▸ Use cache servers for increase the performance ▸ Return a 304 Not Modified when the resource is cached
  38. WEB API’S FROM SCRATCH SECURITY GUIDE-LINES ▸ Avoid sessions when

    possible: ▸ Authenticate every request if necessary ▸ Be Stateless ▸ Authorize based on resource content, not on URL! ▸ Use existing protocol: ▸ HTTP Basic Authentication (only over SSL), OAuth, JWT
  39. WEB API’S FROM SCRATCH SECURITY GUIDE-LINES ▸ Custom Authentication Scheme:

    ▸ Only if your API is private ▸ Only if you really, really know what are you doing ▸ Use API keys instead of username/passwords
  40. WEB API’S FROM SCRATCH WHY USE API KEYS ? ▸

    Limited Exposure ▸ Password reset ▸ Independence ▸ Speed ▸ Traceability
  41. WEB API’S FROM SCRATCH 401 VS 403 ▸ 401 “Unauthorized”

    really means Unauthenticated ▸ “You need valid credentials for me to response to this request” ▸ 403 “Forbidden” really means Unauthorized ▸ “Ok, you have the credentials, but you’re not allowed to access this resource”
  42. WEB API’S FROM SCRATCH DOCUMENTATION AND SPECIFICATION ▸ Specification first,

    code later ▸ Documentation and Specification are not the same thing ▸ Documentation must be concise with the implementation ▸ Always provide a changelog ▸ Provide one SDK at least
  43. THANK YOU! Lucas Mendes
 Software Architect at Tienda Nube
 about.me/devsdmf

    We're hiring, join the crew! 
 bit.ly/work-at-tiendanube