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

A bird's eye view on API development - the live action version

A bird's eye view on API development - the live action version

In this talk Frederick is going to prove he is more then just a pretty face! The talk will cover the basics of api design and will move on to more advanced topics like authentication. The talk itself is based on his blogpost (blog.madewithlove.be/post/birdseye-view-on-api/) but will delve deeper into the topics discussed there. The talk is language agnostic, so even if you're into some obscure programming language you can still adapt the techniques discussed.

Frederick Vanbrabant

June 09, 2016
Tweet

More Decks by Frederick Vanbrabant

Other Decks in Programming

Transcript

  1. { "id": 1, "title": "7 Things You Should NEVER Do

    to a potato", "subtitle": "you will never believe nr 4", "body": "lorem ipsum dolor kebab", "comments": [{ "id": 1, "body": "woah this changed my life" }, { "id": 2, "body": "I will never look at dem taters in the same way" }] } GET /getblogpost/1
  2. Why is this not so great ? Url is kinda

    structure less What if we get loads of articles? What if we get loads of comments? Damn we need to write documentation Is this secure?
  3. HTTP verbs Verb Return HTTP Code POST 201 Sends a

    payload to the server, Returns an empty body.
  4. HTTP verbs Verb Return HTTP Code POST 201 POST /posts

    { "title": "Why cheese is better then real friends", "body": "People only stand in the way of you and your cheese!" } Post body
  5. HTTP verbs Verb Return HTTP Code GET 200 Does not

    send a payload Returns a collection of or a single resource
  6. HTTP verbs Verb Return HTTP Code GET 200 POST /posts/2

    { "title": "How to fake your own death", "body": "You can buy cheese with the insurance money!" } Return body
  7. HTTP verbs Verb Return HTTP Code PUT 200 Updates a

    resource (needs the entire object) Returns an empty body.
  8. HTTP verbs Verb Return HTTP Code PUT 200 POST /posts

    { "title": "Help I'm stuck in a keynote making factory", "body": "Send cheese!" } Put body
  9. HTTP verbs Verb Return HTTP Code PATCH 200 Accepts instructions

    to update the resource Returns the resource
  10. Wait what? Accepts instructions to update the resource returns the

    resource PATCH /artists/kanye/ [ { "op": "replace", "path": "/taylorwift/insults", "value": 2 }, ]
  11. Wait what? Accepts instructions to update the resource returns the

    resource PATCH /artists/kanye/albums/lifeofpablo [ {"taylor_swift_insults" : "2"} ] Content-Type: application/partial-update-json
  12. HTTP CODE Range 1XX - Information Code What it means

    100 continue 101 Switching Protocols 102 Processing
  13. HTTP CODE Range 2XX - Success Code What it means

    200 Ok 201 Created 202 Accepted
  14. HTTP CODE Range 4XX - Client error Code What it

    means 400 Bad Request 401 Unauthorized 404 Not found
  15. HTTP CODE Range 5XX - Server error Code What it

    means 500 Internal Server Error 503 Service Unavailable 504 Gateway Timeout
  16. What about the endpoint Endpoint structure <> database structure Get

    /book/553 { "title": "Coping with cheese addiction", "writer": "M.ouse", "price": 14.55 } Books Writers Prices
  17. Let's check out that body? { "id": 1, "title": "7

    Things You Should NEVER Do to a potato", "subtitle": "you will never believe nr 4", "body": "lorem ipsum dolor kebab", "comments": [{ "id": 1, "body": "woah this changed my life" }, { "id": 2, "body": "I will never look at dem taters in the same way" }] }
  18. { "count": 87, "next": "http://swapi.co/api/people/?page=2", "previous": null, "results": [ {

    "name": "Luke Skywalker", "height": "172", "mass": "77", "hair_color": "blond", "skin_color": "fair", "eye_color": "blue", "birth_year": "19BBY", "gender": "male", "homeworld": "http://swapi.co/api/planets/1/", "films": [ "http://swapi.co/api/films/6/", "http://swapi.co/api/films/3/", "http://swapi.co/api/films/2/", "http://swapi.co/api/films/1/", "http://swapi.co/api/films/7/" ], "species": [ "http://swapi.co/api/species/1/" ], "vehicles": [ "http://swapi.co/api/vehicles/14/", "http://swapi.co/api/vehicles/30/" ], "starships": [ "http://swapi.co/api/starships/12/", "http://swapi.co/api/starships/22/" ], "created": "2014-12-09T13:50:51.644000Z", "edited": "2014-12-20T21:17:56.891000Z", "url": "http://swapi.co/api/people/1/"
  19. { "name": "Luke Skywalker", "height": "172", "mass": "77", "hair_color": "blond",

    "skin_color": "fair", "eye_color": "blue", "birth_year": "19BBY", "gender": "male", "homeworld": "http://swapi.co/api/planets/1/", "films": [ "http://swapi.co/api/films/6/", "http://swapi.co/api/films/3/", "http://swapi.co/api/films/2/", "http://swapi.co/api/films/1/", "http://swapi.co/api/films/7/" ], "species": [ "http://swapi.co/api/species/1/" ], "vehicles": [ "http://swapi.co/api/vehicles/14/", "http://swapi.co/api/vehicles/30/" ], "starships": [ "http://swapi.co/api/starships/12/", "http://swapi.co/api/starships/22/" ], "created": "2014-12-09T13:50:51.644000Z", "edited": "2014-12-20T21:17:56.891000Z", "url": "http://swapi.co/api/people/1/" }
  20. HATEOAS Hypermedia as the Engine of Application State { "name":

    "Boba Fett", "height": "183", "mass": "78.2", "hair_color": "black", "skin_color": "fair", "eye_color": "brown", "birth_year": "31.5BBY", "gender": "male", "homeworld": "http://swapi.co/api/planets/10/", "films": [ "http://swapi.co/api/films/5/", "http://swapi.co/api/films/3/", "http://swapi.co/api/films/2/" ], "species": [ "http://swapi.co/api/species/1/" ], "vehicles": [], "starships": [ "http://swapi.co/api/starships/21/" ], "created": "2014-12-15T12:49:32.457000Z", "edited": "2014-12-20T21:17:50.349000Z", "url": "http://swapi.co/api/people/22/" }
  21. HATEOAS "I'm not going to make 500 calls just to

    get some comments" GET /articles/1/comments GET /articles/1/comments/2,4,6,8
  22. HATEOAS "Can't you just add the resources to the url?"

    GET /articles/1?include=comments,likes
  23. JWT (Json web tokens) Server Client make new calls with

    the token Return token Send credentials
  24. OAuth2 Server Client make new calls with the token Return

    token + Refresh token Send credentials
  25. OAuth2 Server Client make new calls with the token Return

    token + Refresh token Send refresh token
  26. Random TIPS Want to know more? This talks in text

    form: https://blog.madewithlove.be/post/birdseye-view-on-api Phil Sturgeon's book: https://leanpub.com/build-apis-you-wont-hate Great O'Reily book RESTful Web APIs: http://shop.oreilly.com/product/0636920028468.do