Chapter 5.  Developing RESTful APIs with Flask

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:

  • Design a RESTful API that performs CRUD operations in Flask with the Flask-RESTful extension
  • Understand the tasks performed by each HTTP method
  • Set up the virtual environment with Flask and its Flask-RESTful extension
  • Declare status codes for the responses
  • Create the model to represent a resource
  • Use a dictionary as a repository
  • Configure output fields for serialized responses
  • Work with resourceful routing on top of Flask pluggable views
  • Configure resource routing and endpoints
  • Make HTTP requests to the Flask API
  • Work with command-line tools to interact with the Flask API
  • Work with GUI tools to interact with the Flask API

Designing a RESTful API to interact with a simple data source

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.

Tip

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:

  • An integer identifier
  • A string message
  • A duration in seconds that indicates the time the message has to be printed on the OLED display
  • A creation date and time-the timestamp will be added automatically when adding a new message to the collection
  • A message category description, such as "Warning" and "Information"
  • An integer counter that indicates the times the message has been printed in the OLED display
  • A bool value indicating whether the message was printed at least once on the OLED display

The 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

GET

Collection of messages

Retrieve all the stored messages in the collection, sorted by their name in ascending order

GET

Message

Retrieve a single message

POST

Collection of messages

Create a new message in the collection

PATCH

Message

Update a field for an existing message

DELETE

Message

Delete an existing message

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.120.187