달력

122024  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

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.

Posted by 생짜
|