1. Building a JSON CRUD API
Welcome back! We just learned how to write system tests for our FastAPI endpoints. Now let's learn how to combine the four types of endpoints to build a JSON CRUD API.
2. Four Steps in Object Management Lifecycle (CRUD)
When we manage objects in a database, there are four steps in the lifecycle. First we create objects, and then we can read and update them whenever we want. But once we delete an object, its lifecycle is over. Translating this lifecycle into API operations is easy, since the HTTP protocol specifies what operations should be used for each step.
We create objects with POST operations.
We read objects with GET operations.
We update objects with PUT operations.
And we delete objects with DELETE operations.
With our four object lifecycle steps tied to these four API operations, we can build a FastAPI application to manage database objects following the HTTP Protocol.
3. JSON CRUD API Motivation
Let's discuss first why we want to learn to build a JSON CRUD API. We want to learn important fundamentals. These include managing the entire object lifecycle from the previous slide and understanding best practices for HTTP operations. Understanding these makes us ready to design our own APIs right away.
This puts us in a position to pursue more advanced opportunities. These include injecting business logic for more complex data operations and building high throughput data and machine learning inference pipelines.
4. Building a CRUD Module
To make it easier to manage database operations from API endpoints, it's common practice to build a CRUD module. For this video, let's assume we have defined two pydantic models for a movie review. The model called Review has attributes movie, num_stars, and text. The model called DbReview has an additional database identifier for update and delete operations.
We can build a module in a file called crud.py with methods that handle the four types of operations based on input from those models.
Create review takes a Review without a database identifier and creates the review in the database.
Read review only needs a review ID to read the review from the database.
Update review needs a DbReview with a database identifier to update the review, while delete review just needs just a review ID to delete it.
With a CRUD module in place, it will be easy to build our CRUD API.
5. POST Endpoint to Create
A POST endpoint to create a movie review accepts a Review object, calls the CRUD module to create the review in the database, and returns a DbReview object with a database ID.
6. GET Endpoint to Read
GET endpoints are not supposed to accept request bodies, so a GET endpoint to read a movie review can only accept parameters from the URL string. But we can still accept review ID as a URL parameter as we have learned. Then we can call the CRUD module to read the review from the database and return a DbReview object.
7. PUT Endpoint to Update
A PUT endpoint to update a movie review accepts a DbReview object, calls the CRUD module to update the review in the database, and returns a DbReview object.
8. DELETE Endpoint to Delete
A DELETE endpoint to delete a movie review accepts a DbReview object, calls the CRUD module to delete the review from the database, and returns an empty dictionary, since at the end of the operation the object is gone.
9. Let's practice!
Now let's practice building JSON CRUD APIs with FastAPI.