Creating a complete RESTful API
In this recipe, we will convert the API structure created in the last recipe, Creating an extension-based REST interface, into a full-fledged RESTful API.
Getting ready
We will take the API skeleton from the last recipe as a basis to create a completely functional SQLAlchemy-independent RESTful API. Although we will use SQLAlchemy as the ORM for demonstration purposes, 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.
Start with imports and add parser
:
import json from flask_restful import Resource, 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...