Book Image

Flask Framework Cookbook

By : Shalabh Aggarwal
Book Image

Flask Framework Cookbook

By: Shalabh Aggarwal

Overview of this book

Table of Contents (19 chapters)
Flask Framework Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

A complete REST API example


In this recipe, we will convert the API structure created in the previous recipe, Creating a SQLAlchemy-independent REST API, into a full-fledged RESTful API interface.

Getting ready

We will take the API skeleton from the previous recipe as the base to create a complete functional SQLAlchemy-independent RESTful API. Although we will use SQLAlchemy as the ORM for demonstration, this recipe can be written in a similar fashion for any ORM or underlying database.

How to do it…

The following lines of code are the complete RESTful API for the Product model. These code snippets will go into the views.py file:

from flask.ext.restful import reqparse

parser = reqparse.RequestParser()
parser.add_argument('name', type=str)
parser.add_argument('price', type=float)
parser.add_argument('category', type=dict)

In the preceding snippet, we created parser for the arguments that we expected to have in our requests for POST and PUT. The request expects each of the argument to have a value...