Introduction to FastAPI
Matt Eckerle
Software and Data Engineering Leader
Traditional use: update an existing object
Parameters sent via query string as well as request body
Requires an application or framework
cURL
, requests
api = "http://moviereviews.co/reviews/1"
body = {"text": "A fantastic movie!"}
response = requests.put(api, json=body)
Traditional use: delete an existing object
Parameters sent via query string as well as request body
Requires an application or framework
cURL
, requests
api = "http://moviereviews.co/reviews/1"
response = requests.delete(api)
_id
convention for database IDsreview_id
: Table reviews
, column id
from pydantic import BaseModel
class DbReview(BaseModel):
movie: str
num_stars: int
text: str
# Reference database ID of Reviews
review_id: int
PUT endpoint to update an existing movie review:
/reviews
DbReview
(from previous slide)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 endpoint to remove an existing movie review:
/reviews
DbReview
{}
@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 {}
Introduction to FastAPI