Working with Models, SQLAlchemy, and Hyperlinked APIs in Flask
In this chapter, we will expand the capabilities of the RESTful API that we started in the previous chapter. We will use SQLAlchemy as our ORM to work with a PostgreSQL database, and we will take advantage of advanced features included in Flask and Flask-RESTful that will allow us to easily organize code for complex APIs, such as models and blueprints.
We will go through the following topics in this chapter:
- Design a RESTful API to interact with a PostgreSQL 10.5 database
- Understand the tasks performed by each HTTP method
- Install packages with the requirements.txt file to simplify our common tasks
- Create the database
- Configure the database
- Write code for the models with their relationships
- Use schemas to validate, serialize, and deserialize models
- Combine blueprints with resourceful routing
- Understand and configure resourceful routing
- Register the blueprint and run migrations
- Verify the contents of the PostgreSQL database
- Create and retrieve related resources
Designing a RESTful API to interact with a PostgreSQL 10.5 database
So far, our RESTful API has performed CRUD operations on a simple in-memory dictionary that acted as a data repository. The dictionary is never persisted and, therefore, the data is lost whenever we restart our Flask development server.
Now, we want to create a more complex RESTful API with Flask RESTful to interact with a database model that allows us to work with notifications that are grouped into notification categories. In our previous RESTful API, we used a string attribute to specify the notification category for a notification. In this case, we want to be able to easily retrieve all the notifications that belong to a specific notification category and, therefore, we will have a relationship between a notification and a notification category.
We must be able to perform CRUD operations on different related resources and resource collections. The following table enumerates the resources and the class name that we will create to represent the model:
The notification category (NotificationCategory) just requires the following data:
- An integer identifier
- A string name
We need the following data for a notification (Notification):
- An integer identifier
- A foreign key to a notification category (NotificationCategory)
- A string message
- A TTL (short for Time to Live), that is, a duration in seconds that will indicate the amount of time the notification message has to be displayed on the OLED display
- A creation date and time. The timestamp will be added automatically when adding a new notification to the collection
- An integer counter that indicates the times when the notification message has been displayed on the OLED display
- A bool value indicating whether the notification message was displayed at least once on the OLED display
We will take advantage of many packages related to Flask RESTful and SQLAlchemy that make it easier to serialize and deserialize data, perform validations, and integrate SQLAlchemy with Flask and Flask RESTful. This way, we will reduce the boilerplate code.
'karma( 업 ) > Web' 카테고리의 다른 글
Flask Blueprint, REST, REST API? (0) | 2019.10.28 |
---|---|
REST? RESTful?(去去去中知 行行行裏覺) (0) | 2019.10.21 |
부트스트랩? (0) | 2019.10.21 |
AWS EC2에 face_recognition 설치하기( dlib 오류처리 ) (0) | 2019.10.18 |
Hands-On RESTful Python Web Services - Second Edition (1) | 2019.10.12 |