47
App的幕後功臣 – 談談API設計基本原則 KKBOX/MOPCON/KSDG ericpi

KSDG 20150108 Mobile API Design Principles

Embed Size (px)

Citation preview

Page 1: KSDG 20150108 Mobile API Design Principles

App的幕後功臣 – 談談API設計基本原則�

KKBOX/MOPCON/KSDG� ericpi�

Page 2: KSDG 20150108 Mobile API Design Principles

關於我 – 畢瑄易 (ericpi)�

•  KKBOX COO Room Manager •  Kaohsiung Software Developer Group (KSDG) organizer

•  Mobile/Open/Platform CONference (MOPCON) co-founder & 2012/2013/2014 staff

•  Code For Kaohsiung (CfK) Staff

Page 3: KSDG 20150108 Mobile API Design Principles

這兩天開發者都在評論…�

Page 4: KSDG 20150108 Mobile API Design Principles

很多人提到該購票網站一次把頁面噴出來的做法…�

Page 5: KSDG 20150108 Mobile API Design Principles

明明現代 Web / App 都大量在搞不同資訊分段拿的伎倆�

Page 6: KSDG 20150108 Mobile API Design Principles

API是在幹什麼的?�

3G/4G/WiFi� Internet�

IDC/Cloud Server�

3rd Party Service�

Page 7: KSDG 20150108 Mobile API Design Principles

API是在幹什麼的?�

App/Web�

Internet�

Your Service�

3rd Party Service�

API

Page 8: KSDG 20150108 Mobile API Design Principles

API是在幹什麼的?�

App/Web�

Internet�

Your Service�

3rd Party Service:� Facebook, Twitter…blah�

Page 9: KSDG 20150108 Mobile API Design Principles

網路是很危險地�

3G/4G/WiFi� Internet�

IDC/Cloud Server�

3rd Party Service�

Page 10: KSDG 20150108 Mobile API Design Principles

[行前說明]�

Page 11: KSDG 20150108 Mobile API Design Principles

RFC 2616�

Page 12: KSDG 20150108 Mobile API Design Principles

RFC 2616� Hypertext Transfer Protocol -- HTTP/1.1�

Page 13: KSDG 20150108 Mobile API Design Principles

如果不遵循標準, 你跟 IE6 有什麼不一樣?!�

已退休, 可以不 要再捅我了嗎?

Page 14: KSDG 20150108 Mobile API Design Principles

十之八九 App 都會用 Web API�

Page 15: KSDG 20150108 Mobile API Design Principles

十之八九 App 都會用 Web API� •  stateless  protocol� •  scalability� •  availability�

Page 16: KSDG 20150108 Mobile API Design Principles

RFC7230 � HTTP/1.1: Message Syntax and Routing - low-level message parsing and connection management

RFC7231 � HTTP/1.1: Semantics and Content - methods, status codes and headers

RFC7232

� HTTP/1.1: Conditional Requests - e.g., If-Modified-Since

RFC7233 � HTTP/1.1: Range Requests - getting partial content

RFC7234 � HTTP/1.1: Caching - browser and intermediary caches

RFC7235 � HTTP/1.1: Authentication - a framework for HTTP authentication�

Page 17: KSDG 20150108 Mobile API Design Principles

正題開始, 先來看個小例子�

Page 18: KSDG 20150108 Mobile API Design Principles

GET http://you-app-backend.com/login?subscriber_id=123456789�

Page 19: KSDG 20150108 Mobile API Design Principles

GET http://you-app-backend.com/login?user=ericpi-demo&pass=qwert12345�

Page 20: KSDG 20150108 Mobile API Design Principles

POST http://you-app-backend.com/login      user=ericpi-demo&pass=qwert12345�

Page 21: KSDG 20150108 Mobile API Design Principles

POST http://you-app-backend.com/login      user=ericpi-demo&pass=fmq+2n+9RbtROzrcRFhWXA==�

Page 22: KSDG 20150108 Mobile API Design Principles

POST http://you-app-backend.com/login      user=ericpi-demo&pass=fmq+2n+9RbtROzrcRFhWXA==�

DES ECB 雙向 加解密演算法

Page 23: KSDG 20150108 Mobile API Design Principles

POST http://you-app-backend.com/login      user=ericpi-demo&pass=iloVeyOYO�

難道我喜歡瑤瑤 也要告訴你嗎?

Page 24: KSDG 20150108 Mobile API Design Principles

別說這個, 你聽過...�

Page 25: KSDG 20150108 Mobile API Design Principles

別說這個, 你聽過...� hash 嗎?�

Page 26: KSDG 20150108 Mobile API Design Principles

POST http://you-app-backend.com/login      user=ericpi-demo&pass=327bc4e22b649d47c4546a3ec93f376b�

那試試看MD5好了?

Page 27: KSDG 20150108 Mobile API Design Principles

哭哭…�

Page 28: KSDG 20150108 Mobile API Design Principles

POST http://you-app-backend.com/login      user=ericpi-demo&pass=0992d82e56834fc7f9647d1783772144�

那試試看兩倍 MD5!!!

Page 29: KSDG 20150108 Mobile API Design Principles

兩倍哭哭…�

Page 30: KSDG 20150108 Mobile API Design Principles

POST http://you-app-backend.com/login      user=ericpi-demo&pass=137245b801f93aada36b442a5b5bae556799aa9f�

聽說 SHA1  比較厲害!!!

Page 31: KSDG 20150108 Mobile API Design Principles

哭哭三次不用錢…�

Page 32: KSDG 20150108 Mobile API Design Principles

用 Google 就可以黑… 是要逼死誰…�

Page 33: KSDG 20150108 Mobile API Design Principles

HMAC (Hash-based Message Authentication Code)�

Page 34: KSDG 20150108 Mobile API Design Principles

     HMAC-MD5      HMAC-SHA1�

Page 35: KSDG 20150108 Mobile API Design Principles

光講到這邊, Play/iTunes Store 上的 App 已經倒滿地...�

Page 36: KSDG 20150108 Mobile API Design Principles

但今天不是 TDOH 聚會啊啊啊…�

Page 37: KSDG 20150108 Mobile API Design Principles

API 設計最主要需要管理�

•  資料� •  流程�

Page 38: KSDG 20150108 Mobile API Design Principles

請善用工具釐清設計思路�

Page 39: KSDG 20150108 Mobile API Design Principles

API Design 有很多參考資料/書�

•  Vinay Sahni - Best Practices for Designing a Pragmatic RESTful API� – http://goo.gl/OTNGdW�

•  Apigee eBook� – http://goo.gl/S9wA7F�

Page 40: KSDG 20150108 Mobile API Design Principles

API Design 有很多參考資料/書�

•  3scale eBook� – http://goo.gl/oEdwCv�

•  Build Apis You Won`t Hate� – http://goo.gl/5aaLbZ�

•  Coimotion Hack & Go� – http://goo.gl/o9zRzK�

Page 41: KSDG 20150108 Mobile API Design Principles

那今天就借用各家的重點�

Page 42: KSDG 20150108 Mobile API Design Principles

Vinay Sahni: Key requirements for the API�

•  It should use web standards where they make sense�

•  It should be friendly to the developer and be explorable via a browser address bar�

•  It should be simple, intuitive and consistent to make adoption not only easy but pleasant�

•  It should provide enough flexibility to power majority of the Enchant UI�

•  It should be efficient, while maintaining balance with the other requirements�

Page 43: KSDG 20150108 Mobile API Design Principles

簡單說�

•  長得正� •  要對阿宅友善� •  互動性與彈性好� •  可維護性佳�

Page 44: KSDG 20150108 Mobile API Design Principles

跟找女朋友 條件差不多

Page 45: KSDG 20150108 Mobile API Design Principles

簡單條列設計原則�

•  Restful API� •  獨立的 API 域名 & 相關 redirect� •  合理的endpoint結構(&命名)� •  納入版本控制� •  資訊過濾、排序、限制、分頁等基本參數�

Page 46: KSDG 20150108 Mobile API Design Principles

簡單條列設計原則�

•  尋求標準上的解法� – 請愛用 JSON or XML� – HTTP protocol� – 請愛用 https� – OAuth2�

•  例外與錯誤要講清楚� – HTTP status code�

•  Github 403 Forbidden => 404 Not Found� –  Error response�

•  把資源類內容也納入API�

Page 47: KSDG 20150108 Mobile API Design Principles

THANKS� 官方網站� http://mopcon.org/� � 粉絲群� http://fb.me/mopcon�