Advertisement
  1. Code
  2. Flask
Code

Building RESTful APIs With Flask: An ORM With SQLAlchemy

by
Difficulty:BeginnerLength:ShortLanguages:
This post is part of a series called Building RESTful APIs With Flask.
Building RESTful APIs With Flask: The DIY Approach
Building RESTful APIs With Flask: ORM Independent

In the first part of this three-part tutorial series, we saw how to write RESTful APIs all by ourselves using Flask as the web framework. The previous approach provided a whole lot of flexibility but also included writing a lot of code that otherwise could have been avoided in more generic cases. 

In this part, we will use a Flask extension, Flask-Restless, which simply generates RESTful APIs for database models defined with SQLAlchemy. I will take the same sample application as in the last part of this series to maintain context and continuity.

Installing Dependencies

While continuing with the application from the first part, we need to install only one dependency:

The Application

Flask-Restless makes adding RESTful API interfaces to models written with SQLAlchemy a piece of cake.  First, add the REST APIManager from the flask.ext.restless extension to the application configuration file.

flask_app/my_app/__init__.py

Just adding the above couple of lines to the existing code should suffice.

flask_app/my_app/catalog/views.py

This file comprises the bulk of the changes from the previous part. Below is the complete rewritten file.

It is pretty self-explanatory how the above code would work. We just imported the manager that was created in a previous file, and it is used to create an API for the Product model with the listed methods. We can add more methods like DELETE, PUT, PATCH, etc. as needed.

Application in Action

Let's test this application by creating some products and listing them. The endpoint created by this extension by default is http://localhost:5000/api/product.

As I did in the last part of this tutorial series, I will test this using the requests library via terminal.

How to Customize

It is really handy to have the RESTful APIs created automatically, but each application has some business logic which calls for customizations, validations, and clever/secure handling of requests as needed. 

Here, request preprocessors and postprocessors come to the rescue. As the names signify, methods designated as preprocessors run before the processing of the request, and methods designated as postprocessors run after the processing of the request. create_api() is the place where they are defined as dictionaries of the request type (GET, POST, etc.) and the methods as list which will act as preprocessors or postprocessors on the specified request. Below is a template example:

The GET, PUT, and PATCH requests have the flexibility of being fired for single as well as multiple records; therefore, they have two types each. In the code above, notice GET_SINGLE and GET_MANY for GET requests. 

The preprocessors and postprocessors accept different parameters for each type of request and work without any return value. This is left for you to try on your own.

Conclusion

In this part of this tutorial series, we saw how to create a RESTful API using Flask just by adding a couple of lines to a SQLAlchemy-based model. 

In the next and last part of this series, I will cover how to create a RESTful API using another popular Flask extension, but this time, the API will be independent of the modeling tool used for the database.

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.