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. In the second part, we created a RESTful API using Flask-Restless, which depends on SQLAlchemy as the ORM. In this part, we will use another Flask extension, Flask-Restful, which abstracts your ORM and does not make any assumptions about it. 

I will take the same sample application as in the last part of this series to maintain context and continuity. Although this example application is based on SQLAlchemy itself, this extension can be used along with any ORM in a similar fashion, as shown in this tutorial.

Installing Dependencies

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

The Application

Before we start, you might want to remove the code that we wrote for the second part of this tutorial series for more clarity.

As always, we will start with changes to our application’s configuration, which will look something like the following lines of code: 

flask_app/my_app/__init__.py

flask_app/my_app/product/views.py

argparse is a library that makes it easy to validate form data in Flask.

Testing the Application

This application can be tested exactly as we did in the second part of this tutorial series. I have kept the routing URL the same for the same purpose.

Conclusion

In this last part of this three-part tutorial series on developing RESTful APIs with Flask, we saw how to write ORM-independent RESTful APIs. This wraps up the basics of writing RESTful APIs with Flask in various ways. 

More can be learned about each of the methods covered, and you can explore this on your own, using the basics you’ve learned in this series.

This post has been updated with contributions from Esther Vaati. Esther is a software developer and writer for Envato Tuts+.


This content originally appeared on Envato Tuts+ Tutorials and was authored by Shalabh Aggarwal

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. In the second part, we created a RESTful API using Flask-Restless, which depends on SQLAlchemy as the ORM. In this part, we will use another Flask extension, Flask-Restful, which abstracts your ORM and does not make any assumptions about it. 

I will take the same sample application as in the last part of this series to maintain context and continuity. Although this example application is based on SQLAlchemy itself, this extension can be used along with any ORM in a similar fashion, as shown in this tutorial.

Installing Dependencies

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

The Application

Before we start, you might want to remove the code that we wrote for the second part of this tutorial series for more clarity.

As always, we will start with changes to our application's configuration, which will look something like the following lines of code: 

flask_app/my_app/__init__.py

flask_app/my_app/product/views.py

argparse is a library that makes it easy to validate form data in Flask.

Testing the Application

This application can be tested exactly as we did in the second part of this tutorial series. I have kept the routing URL the same for the same purpose.

Conclusion

In this last part of this three-part tutorial series on developing RESTful APIs with Flask, we saw how to write ORM-independent RESTful APIs. This wraps up the basics of writing RESTful APIs with Flask in various ways. 

More can be learned about each of the methods covered, and you can explore this on your own, using the basics you've learned in this series.

This post has been updated with contributions from Esther Vaati. Esther is a software developer and writer for Envato Tuts+.


This content originally appeared on Envato Tuts+ Tutorials and was authored by Shalabh Aggarwal


Print Share Comment Cite Upload Translate Updates
APA

Shalabh Aggarwal | Sciencx (2016-06-14T14:17:54+00:00) Building RESTful APIs With Flask: ORM Independent. Retrieved from https://www.scien.cx/2016/06/14/building-restful-apis-with-flask-orm-independent/

MLA
" » Building RESTful APIs With Flask: ORM Independent." Shalabh Aggarwal | Sciencx - Tuesday June 14, 2016, https://www.scien.cx/2016/06/14/building-restful-apis-with-flask-orm-independent/
HARVARD
Shalabh Aggarwal | Sciencx Tuesday June 14, 2016 » Building RESTful APIs With Flask: ORM Independent., viewed ,<https://www.scien.cx/2016/06/14/building-restful-apis-with-flask-orm-independent/>
VANCOUVER
Shalabh Aggarwal | Sciencx - » Building RESTful APIs With Flask: ORM Independent. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2016/06/14/building-restful-apis-with-flask-orm-independent/
CHICAGO
" » Building RESTful APIs With Flask: ORM Independent." Shalabh Aggarwal | Sciencx - Accessed . https://www.scien.cx/2016/06/14/building-restful-apis-with-flask-orm-independent/
IEEE
" » Building RESTful APIs With Flask: ORM Independent." Shalabh Aggarwal | Sciencx [Online]. Available: https://www.scien.cx/2016/06/14/building-restful-apis-with-flask-orm-independent/. [Accessed: ]
rf:citation
» Building RESTful APIs With Flask: ORM Independent | Shalabh Aggarwal | Sciencx | https://www.scien.cx/2016/06/14/building-restful-apis-with-flask-orm-independent/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.