Flask-RESTful uses resources built on top of Flask pluggable views as the main building block for a RESTful API. We just need to create a subclass of the flask_restful.Resource
class and declare the methods for each supported HTTP verb. A subclass of flask_restful.Resource
represents a RESTful resource and therefore, we will have to declare one class to represent the collection of messages and another one to represent the message resource.
First, we will create a Message
class that we will use to represent the message resource. Open the previously created api/api.py
file and add the following lines. The code file for the sample is included in the restful_python_chapter_05_01
folder, as shown:
class Message(Resource): def abort_if_message_doesnt_exist(self, id): if id not in message_manager.messages: abort( status.HTTP_404_NOT_FOUND, message="Message {0} doesn't exist...