Now, we will create the resources that compose our main building blocks for the RESTful API. First, we will create a few instances that we will use in the different resources. Then, we will create a MessageResource
class, that we will use to represent the message resource. Create a new views.py
file within the api
folder and add the following lines. The code file for the sample is included in the restful_python_chapter_06_01
folder, as shown:
from flask import Blueprint, request, jsonify, make_response
from flask_restful import Api, Resource
from models import db, Category, CategorySchema, Message, MessageSchema
from sqlalchemy.exc import SQLAlchemyError
import status
api_bp = Blueprint('api', __name__)
category_schema = CategorySchema()
message_schema = MessageSchema()
api = Api(api_bp)
class MessageResource(Resource):
def get(self, id):
message = Message.query.get_or_404(id)
result = message_schema.dump...