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

REST API for Likes

REST API for Likes

REST API basic principles re-explained and some edge cases including soft-deletes and likes on resources. Presented for the first time at internal company conference during #mwlretreat.

39eb3f3d313b13f05534e496285040b8?s=128

Hannes Van De Vreken

September 30, 2014
Tweet

Transcript

  1. REST API solutions For likes/stars/tags

  2. Hi, my name is Hannes.

  3. None
  4. None
  5. None
  6. None
  7. None
  8. None
  9. • Developer • Laravel since 4.0.0-BETA2 • Darwin Analytics ABOUT

    ME
  10. madewithlove

  11. REST API for likes

  12. A Github user stars a repository

  13. STAR /repos/username/repo…

  14. REST API implementations for likes/stars/tags (relationships without attributes)

  15. • REST basics • Case study • How it’s done

    REST API FOR LIKES
  16. 1. REST basics

  17. None
  18. REST BASICS - SOAP POST /SomeService.aspx Host: example.org

  19. REST BASICS - SOAP <?xml version="1.0"?> <soap:Envelope xmlns:soap="http://www.w3.org/2001/…" soap:encodingStyle="http://www.w3.org/2001/…"> <soap:Body

    xmlns:m="http://www.example.org/stock"> <m:GetStockPrice> <m:StockName>IBM</m:StockName> </m:GetStockPrice> </soap:Body> </soap:Envelope>
  20. REST BASICS - REST EQUIVALENT GET /stocks/ibm HTTP/1.1 Host: example.org

    Accept: application/xml
  21. CRUD - HTTP verbs

  22. REST BASICS - HTTP METHOD - CREATE Create: POST

  23. REST BASICS - HTTP METHODS - CREATE POST /notes HTTP/1.1

    Host: example.org
  24. REST BASICS - HTTP METHODS - CREATE Create: POST or

    PUT
  25. REST BASICS - HTTP METHODS - CREATE PUT /notes/meeting1-report HTTP/1.1

    Host: example.org
  26. REST BASICS - HTTP METHODS - READ Read: GET

  27. REST BASICS - HTTP METHODS - READ GET /users/anahkiasen HTTP/1.1

    Host: api.github.com ! HTTP/1.1 200 OK
  28. REST BASICS - HTTP METHODS - UPDATE Update: PUT

  29. REST BASICS - HTTP METHODS - UPDATE PUT /users/andreascreten HTTP/1.1

    Host: api.github.com ! { "login": "andreascreten", "id": 4200, "avatar_url": "https://avatars.gith…", "email": "andreas@mwl.be", …
  30. REST BASICS - HTTP METHODS - UPDATE Update: PUT or

    PATCH
  31. REST BASICS - HTTP METHODS - UPDATE PATCH /users/andreascreten HTTP/1.1

    Host: api.github.com ! email=andreas@mwl.be
  32. REST BASICS - HTTP METHODS - DELETE Delete: DELETE

  33. REST BASICS - HTTP METHODS - DELETE DELETE /repos/andreascreten/bpost HTTP/1.1

    Host: api.github.com ! HTTP/1.1 204 No Content
  34. Headers

  35. /followers/ids.json REST BASICS - HTTP HEADERS - CONTENT TYPE

  36. REST BASICS - HTTP HEADERS - CONTENT TYPE Accept: application/json

  37. https://api.twitter.com/1.0/ https://api.twitter.com/1.1/ REST BASICS - HTTP HEADERS - VERSION

  38. Accept: application/vnd.github.v3+json REST BASICS - HTTP HEADERS - VERSION

  39. REST BASICS - HTTP HEADERS - AUTHENTICATION https://api.instagram.com/v1/users/ self?access_token=m4d3w1thl0ve

  40. GET /users/hannesvdvreken HTTP/1.1 Host: api.github.com Authorization: Bearer m4d3w1thl0ve REST BASICS

    - HTTP HEADERS - AUTHENTICATION
  41. REST BASICS - HTTP HEADERS - RATE LIMIT https://api.twitter.com/1.1/ application/rate_limit_status.json

  42. REST BASICS - HTTP HEADERS - RATE LIMIT HTTP/1.1 429

    Too Many Requests X-RateLimit-Limit: 60 X-RateLimit-Remaining: 0 X-RateLimit-Reset: 1377013266
  43. HTTP/1.1 429 Too Many Requests X-RateLimit-UserLimit: 60 X-RateLimit-UserRemaining: 0 X-RateLimit-UserReset:

    1377… REST BASICS - HTTP HEADERS - RATE LIMIT
  44. HTTP/1.1 429 Too Many Requests X-RateLimit-ClientLimit: 60 X-RateLimit-ClientRemaining: 0 X-RateLimit-ClientReset:

    1377… REST BASICS - HTTP HEADERS - RATE LIMIT
  45. URL scheme

  46. REST BASICS - URL SCHEME Required params = URL segments

  47. REST BASICS - URL SCHEME Optional params = GET params

  48. REST BASICS - URL SCHEME /orgs/{org_slug}/report/ {start_date}/{end_date}

  49. REST BASICS - URL SCHEME /orgs/mwl/report/ 2014-01-01/2014-06-30

  50. REST BASICS - URL SCHEME /orgs/mwl/report ?start_date=2014-01-01 &end_date=2014-06-30

  51. Soft delete? DELETE /endpoint

  52. REST BASICS - SOFT DELETE DELETE /notes/1234 HTTP/1.1 Host: example.org

  53. REST BASICS - SOFT DELETE GET /deleted_notes HTTP/1.1 Host: example.org

  54. REST BASICS - SOFT DELETE PATCH /deleted_notes/1234 HTTP/1.1 Host: example.org

    ! deleted=false
  55. REST BASICS - SOFT DELETE PATCH /notes/1234 HTTP/1.1 Host: example.org

    ! archived=true
  56. REST BASICS - SOFT DELETE GET /archived_notes HTTP/1.1 Host: example.org

  57. REST BASICS - SOFT DELETE PATCH /archived_notes/1234 HTTP/1.1 Host: example.org

    ! archived=false
  58. REST BASICS - RECAP • HTTP Verbs • HTTP Headers

    • Required vs. Optional
  59. 2. Case study

  60. CASE STUDY - TWITTER Twitter favorites

  61. CASE STUDY - TWITTER - FAVORITED? GET /1.1/statuses/show/51673… HTTP/1.1 Host:

    api.twitter.com ! { "favorited": true, …
  62. CASE STUDY - TWITTER - FAVORITE POST /1.1/favorites/create.json?id=51673… HTTP/1.1 Host:

    api.twitter.com
  63. CASE STUDY - TWITTER - UNFAVORITE POST /1.1/favorites/destroy.json?id=51673… HTTP/1.1 Host:

    api.twitter.com
  64. CASE STUDY - GITHUB Github stargazers

  65. CASE STUDY - GITHUB - STARRED? GET /user/starred/:owner/:repo HTTP/1.1 Host:

    api.github.com ! id
  66. CASE STUDY - GITHUB - STAR PUT /user/starred/:owner/:repo HTTP/1.1 Host:

    api.github.com ! HTTP/1.1 204 No Content
  67. CASE STUDY - GITHUB - UNSTAR DELETE /user/starred/:owner/:repo HTTP/1.1 Host:

    api.github.com ! HTTP/1.1 204 No Content
  68. CASE STUDY - FOURSQUARE Foursquare likes

  69. CASE STUDY - FOURSQUARE - LIKED? GET /v2/venues/4b292… HTTP/1.1 Host:

    api.foursquare.com ! { "like": true, …
  70. CASE STUDY - FOURSQUARE - LIKED? GET /v2/venues/4b292… HTTP/1.1 Host:

    api.foursquare.com ! { "dislike": true, …
  71. CASE STUDY - FOURSQUARE - LIKE POST /v2/venues/4b292…/like HTTP/1.1 Host:

    api.foursquare.com ! set=1
  72. CASE STUDY - FOURSQUARE - UNLIKE POST /v2/venues/4b292…/like HTTP/1.1 Host:

    api.foursquare.com ! set=0
  73. CASE STUDY - FOURSQUARE - LIKE POST /v2/checkins/4b292…/like HTTP/1.1 Host:

    api.foursquare.com ! set=1
  74. CASE STUDY - FOURSQUARE - UNLIKE POST /v2/checkins/4b292…/like HTTP/1.1 Host:

    api.foursquare.com ! set=0
  75. CASE STUDY - INSTAGRAM Instagram likes

  76. CASE STUDY - INSTAGRAM - LIKED? GET /v1/media/4b292…/likes HTTP/1.1 Host:

    api.instagram.com ! { "data": [ { "username": "andreascreten", "id": "206389", …
  77. CASE STUDY - INSTAGRAM - LIKE POST /v1/media/4b292…/likes HTTP/1.1 Host:

    api.instagram.com
  78. CASE STUDY - INSTAGRAM - UNLIKE DELETE /v1/media/4b292…/likes HTTP/1.1 Host:

    api.instagram.com
  79. 3. How it’s done

  80. In my opinion HOW IT’S DONE

  81. Github + Instagram HOW IT’S DONE

  82. HOW IT’S DONE - LIKED? GET /notes/{id} HTTP/1.1 Host: example.org

    ! { "liked": true, …
  83. HOW IT’S DONE - PEOPLE WHO LIKED GET /notes/{id}/likes HTTP/1.1

    Host: example.org
  84. HOW IT’S DONE - LIKE PATCH /notes/{id} HTTP/1.1 Host: example.org

    ! liked=true
  85. HOW IT’S DONE - UNLIKE PATCH /notes/{id} HTTP/1.1 Host: example.org

    ! liked=false
  86. • REST basics • Case study • How it’s done

    RECAP
  87. Thank you! @hannesvdvreken

  88. Questions/discussions

  89. • http:/ /wikipedia.org/wiki/SOAP • http:/ /myri.se • http:/ /meetup.com/mongodb-belgium •

    http:/ /mwl.be • http:/ /shop.oreilly.com/product/0636920028468.do • https:/ /dev.twitter.com • http:/ /instagram.com/developer • https:/ /developer.github.com • https:/ /developer.foursquare.com REFERENCES