Hands-On Docker for Microservices with Python
上QQ阅读APP看书,第一时间看更新

Designing the RESTful API

We will follow the principles of RESTful design for our API. This means we will use constructed URIs that represent resources and then use the HTTP methods to perform actions over these resources.

In this example, we will only use the  GET (to retrieve), POST (to create), and DELETE (to delete) methods as the thoughts are not editable. Remember that PUT (to overwrite completely) and PATCH (to perform a partial update) are also available.

One of the main properties of RESTful APIs is that requests need to be stateless, which means that each request is totally self-contained and can be served by any server. All the required data should be either at the client (that will send it attached to the request) or in a database (so the server will retrieve it in full). This property is a hard requirement when dealing with Docker containers, as they can be destroyed and recreated without warning.

While it is common to have resources that map directly to rows in a database, this is not necessary. The resources can be a composition of different tables, part of them, or even represent something different altogether, such as an aggregation of data, whether certain conditions are met, or a forecast based on analysis on the current data.

Analyze the needs of the service and don't feel constrained by your existing database design. Migrating a microservice is a good opportunity to revisit the old design decisions and to try to improve the general system. Also, remember the Twelve-Factor App principles ( https://12factor.net/) for improving the design.

It's always good to have a brief reminder about REST before starting an API design, so you can check https://restfulapi.net/ for a recap.