FastAPI 入門
Matt Eckerle
Software and Data Engineering Leader
傳統用途:更新現有物件
參數可透過查詢字串或請求本文傳送
需要應用程式或框架
cURL、requestsapi = "http://moviereviews.co/reviews/1"
body = {"text": "A fantastic movie!"}
response = requests.put(api, json=body)
傳統用途:刪除現有物件
參數可透過查詢字串或請求本文傳送
需要應用程式或框架
cURL、requestsapi = "http://moviereviews.co/reviews/1"
response = requests.delete(api)
_id 慣例review_id:資料表 reviews,欄位 idfrom pydantic import BaseModel
class DbReview(BaseModel):
movie: str
num_stars: int
text: str
# Reference database ID of Reviews
review_id: int
用 PUT 端點更新電影評論:
/reviewsDbReview(見前一張投影片)DbReview@app.put("/reviews", response_model=DbReview)
def update_review(review: DbReview):
# Update the movie review in the database
db_review = crud.update_review(review)
# Return the updated review
return db_review
用 DELETE 端點移除電影評論:
/reviewsDbReview{}@app.delete("/reviews")
def delete_review(review: DbReview):
# Delete the movie review from the database
crud.delete_review(review)
# Return nothing since the data is gone
return {}
FastAPI 入門