Package: simple-flask-restful
simple_flask_restfulsimple_flask_restfulis a lightweight Python package that provides a simpler interface to create RESTful APIs using Flask.InstallationThe package can be installed via pip:pip3 install simple_flask_restfulUsageThe package provides aFlaskapplication object and anApiobject to define the RESTful resources. Here is an example:fromsimple_flask_restfulimportFlask,Resource,Apiapp=Flask(__name__)api=Api(app)classHelloWorld(Resource):defget(self):return{'message':'Hello, world!'}api.add_resource(HelloWorld,'/')if__name__=='__main__':app.run(debug=True)In the above example, we define aHelloWorldresource that returns a simple JSON message. We add this resource to theApiobject, which in turn adds it to theFlaskapplication object.ResourcesA resource is a Python class that defines the HTTP methods that are allowed on a particular URL endpoint. Here is an example:fromsimple_flask_restfulimportFlask,Resource,Api,reqparseclassMultiply(Resource):def__init__(self):self.parser=reqparse.RequestParser()self.parser.add_argument('num1',type=float,required=True)self.parser.add_argument('num2',type=float,required=True)defget(self):args=self.parser.parse_args()result=args['num1']*args['num2']return{'result':result}In the above example, we define aMultiplyresource that expects two query parameters,numoneandnum2, and returns their product. We use thereqparsemodule to parse the query parameters.To add this resource to theApiobject, we use theadd_resourcemethod:api.add_resource(Multiply, '/multiply', '/product')This maps theMultiplyresource to the/multiplyand/multiplyURL endpoint.Return Format:The return of method (get/post/put/delete) in the child class of Resource should be a dictionary or in JSON format. The default HTTP status code is 200.Example:return {'result': result}is the same asreturn {'result': result}, 200Running the ServerTo run the Flask server, simply call therunmethod on theFlaskapplication object:if__name__=='__main__':app.run(debug=True)This will start the server on port 5000. You can access the resources at their corresponding URL endpoints, for example:http://localhost:5000/multiply?numone=2&num2=3.reqparseWe are using thereqparsemodule to parse the arguments of the incoming request.add_argument()add_argument(name, dest=None, required=False, type=None, choices=None, help=None, default=None)This method adds a new argument to the parser.name: The name of the argument.dest: The name of the attribute to store the argument in. If not specified,namewill be used.required: IfTrue, this argument must be included in the request. Defaults toFalse.type: The expected data type of the argument. If specified, the argument will be coerced to this type. Supported types includestr,int,float,bool, and any callable that takes a single string argument and returns the parsed value. If not specified, the argument will be returned as a string.choices: A list of valid choices for the argument. If specified, the argument must be one of these choices.help: The error message to display if the argument is invalid.default: The default value for the argument if it is not included in the request.parse_args()parse_args(strict=False)This method parses the arguments of the incoming request.strict: IfTrue, the request must contain only the arguments that were added to the parser. IfFalse, additional arguments will be ignored. Defaults toFalse.Licensesimple_flask_restfulis released under the MIT License.