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

Building API For The REST of Us

85a64fb3375926c0be5c75eccf40e853?s=47 Terry
September 26, 2016

Building API For The REST of Us

Slides in RubyConf China 2016 Chengdu

85a64fb3375926c0be5c75eccf40e853?s=128

Terry

September 26, 2016
Tweet

Transcript

  1. Building API For The REST of Us — Terry Tai

  2. About Me

  3. Host of Teahour.FM

  4. Creator of Railscasts-China

  5. I’m working for My Color Way 619'4 䨅⃚

  6. Co-founder of fengche.co

  7. Co-team of Peatio Project

  8. Let’s Building API

  9. Conditions • Based on Ruby on Rails • Not a

    pure API service, web is still the first-class citizen • Client builder is resource owner or ourselves
  10. 1. Stack Choice

  11. VS

  12. Performance

  13. Flexibility

  14. Level of abstraction

  15. Community

  16. I like Grape a little bit more!

  17. DSL Higer Abstraction

  18. Isolation

  19. Michael Bleigh Grape is created by my coworker in Intridea.

    He is also the creator of oauth2, omniauth, hashie, multi_json …
  20. 2. Routes & Version

  21. In Rails

  22. In Grape

  23. Rails with version in header

  24. Rails with version in header

  25. Rails with version in header

  26. Grape with version in header

  27. 3. Request & Params

  28. Params Encoding application/x-www-form-urlencoded multipart/form-data application/json

  29. Sending Params

  30. Rails server side

  31. Grape server side

  32. Grape params real world example

  33. 4.Authentication

  34. Basic Auth

  35. None
  36. With HTTPS?

  37. None
  38. Token Based Auth

  39. Token Based Auth • Simple • A little bit more

    secure than Basic Auth • Muti-tokens for authorisation
  40. Digest Based Auth

  41. What we use?

  42. Open Source Exchange

  43. Ancun

  44. Zhiren

  45. Signature based Authentication?

  46. Amazon use the similar strategy

  47. None
  48. One request includes: • access_key • tonce • payload •

    signature
  49. Signature

  50. payload

  51. None
  52. Everything Work Well until..

  53. payload

  54. WTF is that?

  55. Why not use JSON?

  56. One request includes: • access_key • tonce • payload •

    signature
  57. Strategy • Put business related data in payload as JSON

    • Put authentication related data out of payload
  58. Simpler than Amazon

  59. 5.Resource Presenter

  60. If you use Grape

  61. If you use Rails

  62. The most quick way

  63. Extract a Presenter layer

  64. Extract a Presenter layer

  65. Treat it as View!

  66. Jbuilder

  67. –DHH “Every time you use a loop in your view,

    but you don’t use partial, you might be wrong.”
  68. Jbuilder

  69. RABL

  70. None
  71. It’s not View

  72. Active Model Serializers

  73. View A new layer VS

  74. Two Stories ̽Terry ݏݏᦖඳԪ̾

  75. Story 1

  76. After user signup send him/her a welcome email

  77. Where to put this line?

  78. User model callback

  79. after_create ?

  80. after_commit on create

  81. Send in Callback

  82. Send in Controller

  83. Which one is the BEST way?

  84. I don’t know

  85. The second solution is from Rails Guides

  86. Story 2

  87. Jan Xie Ex-Intridea programmer Ex-Peatio co-member Founder of Cryptape ruby-pinyin

    author ruby-ethereum author My darling….
  88. His last project in Ruby?

  89. Python, Go

  90. Sinatra Node

  91. When A record created, B,C,D must be created at the

    same time
  92. A B C D

  93. None
  94. None
  95. Communicating emotions

  96. It’s become better but not BEST!

  97. What’s the BEST way?

  98. Jan’s solution — Service

  99. My solution — PubSub

  100. My solution — PubSub

  101. My solution — PubSub

  102. My solution — PubSub You can use Wisper gem to

    do similar thing
  103. Two hours PK…

  104. We decide change Nothing

  105. Maybe, there is no BEST way We need know why

    people think different We need know compromise
  106. Welcome to Chengdu ́౮᮷҅ Ӟଷ๶ԧ੪ӧమᐶ୏ጱउ૱̶͂

  107. Spicy Food

  108. Girls

  109. Spicy Food

  110. Girls

  111. RubyConf China in Chengdu

  112. We are hiring!

  113. None
  114. BTW҅I’m ́ፗካ͂

  115. Enjoy your “one night in Chengdu”