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

All about APIs

All about APIs

Presentation about best practices on RESTful APIs, created by Diogo Laginha Machado & Ricardo Vitorino:

- Basic Concepts
- Challenges
- API design
- Hypermedia
- Versioning
- Documentation

Ricardo Vitorino

March 23, 2013
Tweet

Other Decks in Technology

Transcript

  1. All about APIs >  GET          

     /presentations/apis >  Accept:    application/vnd.apple.keynote  version=1.0 <  Date:        Sat,  23  Mar  2013 <  Content: {  authors:  [  Diogo  Laginha,  Ricardo  Vitorino  ],  awesome:  true }
  2. What’s a resource? - Sources of information (documents or services)

    - Any interaction with a RESTful API is an interaction with a resource
  3. RESTful APIs 5 A set of URIs that map entities

    to endpoints. resource resource resource URI URI URI
  4. Actions 13 The way to perform an action in a

    RESTful API is to use HTTP verbs (request methods)
  5. Actions 14 HTTP methods: • GET • HEAD • OPTIONS

    • POST • PUT • PATCH • DELETE
  6. Representation 17 /articles/ - It doesn't include the format -

    A given resource can have different representations (XML, JSON, …)
  7. 21 Resources are mapped to URLs Actions are mapped to

    verbs The rest goes in the headers Resources are mapped to URIs Actions are mapped to verbs The rest goes in the headers
  8. 26 You want to say “Happy Birthday” on some social

    network. Option A Go directly to friend’s page and post. What if…
  9. 26 You want to say “Happy Birthday” on some social

    network. Option A Go directly to friend’s page and post. Option B Go to Homepage > search friend > post. What if…
  10. 26 You want to say “Happy Birthday” on some social

    network. Option A Go directly to friend’s page and post. Option B Go to Homepage > search friend > post. What if…
  11. 27 Then… Why shouldn’t you do the same for APIs?

    >  GET        /apis <  Content:    apis:  [    {        path:  "/presentations/",            description:  "Barcamp  presentations"        },        {        path:  "/users/",            description:  "Who  signed  up  for  Barcamp"        },    ]
  12. 28 That’s hypermedia! Actually, it’s called HATEOAS. Hypermedia as the

    Engine of Application State -Single endpoint for the resource
  13. 28 That’s hypermedia! Actually, it’s called HATEOAS. Hypermedia as the

    Engine of Application State -Single endpoint for the resource -All actions discovered by inspecting resource
  14. 29 HATEOAS Get a certain keynote:    presentation:  { id:

         1337, name:      "All  About  APIs", date:      "2013-­‐03-­‐23", authors:  {    path:      "/presentations/1337/authors/",        description:  "Keynote’s  authors", }    }
  15. 29 HATEOAS Get a certain keynote:    presentation:  { id:

         1337, name:      "All  About  APIs", date:      "2013-­‐03-­‐23", authors:  {    path:      "/presentations/1337/authors/",        description:  "Keynote’s  authors", }    } We know what to do next ☺
  16. 30 HATEOAS Get all the keynotes:    presentations:  [ {...},

       ... {...}    ], Meta:  { total:  100, paginated_objects:  25, next_page:  "/presentations?offset=25" }
  17. 30 HATEOAS Get all the keynotes:    presentations:  [ {...},

       ... {...}    ], Meta:  { total:  100, paginated_objects:  25, next_page:  "/presentations?offset=25" } }25 presentations
  18. 30 HATEOAS Get all the keynotes:    presentations:  [ {...},

       ... {...}    ], Meta:  { total:  100, paginated_objects:  25, next_page:  "/presentations?offset=25" } }25 presentations Where to get some more
  19. - Version a resource representation, not the API - Add

    a new version when the changes in the resource representation are non- backward compatible First of all 32
  20. - Don’t do this - Resources should only include nouns!

    33 Versioning GET  /presentations/v1/
  21. - Don’t do this - Resources should only include nouns!

    - Besides, it’s ugly 33 Versioning GET  /presentations/v1/
  22. Swagger 40 - Document your API with style - No

    more dull .doc files or ugly pages
  23. Swagger 40 - Document your API with style - No

    more dull .doc files or ugly pages - Give your users a playground
  24. References - http://www.apievangelist.com/ - http://publish.luisrei.com/articles/rest.html - http://blog.steveklabnik.com - http://www.designinghypermediaapis.com -

    https://twitter.com/hypermediaapis - https://developers.helloreverb.com/swagger/ - http://laginha.github.com/yard/ 46