REST API

REST API

困難

說明

實作一個 RESTful API 來追蹤 IOU。

四位室友常常互相借錢,卻老是記不清誰欠誰多少錢。

你的任務是實作一個簡單的 RESTful API,它會透過 POST 請求接收 IOU,並能透過 GET 請求提供指定的摘要資訊。

API 規格

使用者物件

{
  "name": "Adam",
  "owes": {
    "Bob": 12.0,
    "Chuck": 4.0,
    "Dan": 9.5
  },
  "owed_by": {
    "Bob": 6.5,
    "Dan": 2.75
  },
  "balance": "<(total owed by other users) - (total owed to other users)>"
}

方法

說明 HTTP 方法 URL 酬載格式 無酬載的回應 含酬載的回應
列出使用者資訊 GET /users {"users":["Adam","Bob"]} {"users":<List of all User objects>} {"users":<List of User objects for <users> (sorted by name)}
建立使用者 POST /add {"user":<name of new user (unique)>} N/A <User object for new user>
建立 IOU POST /iou {"lender":<name of lender>,"borrower":<name of borrower>,"amount":5.25} N/A {"users":<updated User objects for <lender> and <borrower> (sorted by name)>}

其他資源

透過 GitHub 編輯 連結會在新視窗或分頁中開啟
Python Exercism

準備好開始 REST API 了嗎?

註冊 Exercism,透過 17 個概念146 個練習 和真人引導來學習並精通 Python,全部免費。