In this chapter, we will start working with Flask and its Flask-RESTful extension; we will also create a RESTful Web API that performs CRUD operations on a simple list. We will:
Imagine that we have to configure the messages to be displayed in an OLED display wired to an IoT (Internet of Things) device, the IoT device is capable of running Python 3.5, Flask, and other Python packages. There is a team that is writing code that retrieves string messages from a dictionary and displays them in the OLED display wired to the IoT device. We have to start working on a mobile app and a website that has to interact with a RESTful API to perform CRUD operations with string messages.
We don't need an ORM because we won't persist the string messages on a database. We will just work with an in-memory dictionary as our data source. It is one of the requirements for this RESTful API. In this case, the RESTful web service will be running on the IoT device, that is, we will run the Flask development server on the IoT device.
We will definitely lose scalability for our RESTful API because we have the in-memory data source in the server, and therefore, we cannot run the RESTful API in another IoT device. However, we will work with another example related to a more complex data source that will be able to scale in the RESTful way later. The first example is going to allow us to understand how Flask and Flask-RESTful work together with a very simple in-memory data source.
We have chosen Flask because it is more lightweight than Django, we don't need to configure an ORM and we want to start running the RESTful API on the IoT device, as soon as possible, to allow all the teams to interact with it. We will code the website with Flask too, and therefore, we want to use the same web micro-framework to power the website and the RESTful web service.
There are many extensions available for Flask that makes it easier to perform specific tasks with the Flask micro-framework. We will take advantage of Flask-RESTful, an extension that will allow us to encourage best practices while building our RESTful API. In this case, we will work with a Python dictionary as the data source. As previously explained, we will work with more complex data sources in the forthcoming examples.
First, we must specify the requirements for our main resource: a message. We need the following attributes or fields for a message:
bool
value indicating whether the message was printed at least once on the OLED displayThe following table shows the HTTP verbs, the scope, and the semantics for the methods that our first version of the API must support. Each method is composed by an HTTP verb and a scope and all the methods have a well-defined meaning for all the messages and collections. In our API, each message has its own unique URL.
HTTP verb |
Scope |
Semantics |
|
Collection of messages |
Retrieve all the stored messages in the collection, sorted by their name in ascending order |
|
Message |
Retrieve a single message |
|
Collection of messages |
Create a new message in the collection |
|
Message |
Update a field for an existing message |
|
Message |
Delete an existing message |
3.138.120.187