Skip to main content

wrap up flask for restful interface

Project description

simple_flask_restful

simple_flask_restful is a lightweight Python package that provides a simpler interface to create RESTful APIs using Flask.

Installation

The package can be installed via pip:

pip3 install simple_flask_restful

Usage

The package provides a Flask application object and an Api object to define the RESTful resources. Here is an example:

from simple_flask_restful import Flask, Resource, Api

app = Flask(__name__)
api = Api(app)

class HelloWorld(Resource):
    def get(self):
        return {'message': 'Hello, world!'}

api.add_resource(HelloWorld, '/')

if __name__ == '__main__':
    app.run(debug=True)

In the above example, we define a HelloWorld resource that returns a simple JSON message. We add this resource to the Api object, which in turn adds it to the Flask application object.

Resources

A resource is a Python class that defines the HTTP methods that are allowed on a particular URL endpoint. Here is an example:

from simple_flask_restful import Flask, Resource, Api, reqparse
class Multiply(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)

    def get(self):
        args = self.parser.parse_args()
        result = args['num1'] * args['num2']
        return {'result': result}

In the above example, we define a Multiply resource that expects two query parameters, numone and num2, and returns their product. We use the reqparse module to parse the query parameters.

To add this resource to the Api object, we use the add_resource method:

api.add_resource(Multiply, '/multiply', '/product')

This maps the Multiply resource to the /multiply and /multiply URL 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 as return {'result': result}, 200

Running the Server

To run the Flask server, simply call the run method on the Flask application 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.

reqparse

We are using the reqparse module 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, name will be used.
  • required: If True, this argument must be included in the request. Defaults to False.
  • type: The expected data type of the argument. If specified, the argument will be coerced to this type. Supported types include str, 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: If True, the request must contain only the arguments that were added to the parser. If False, additional arguments will be ignored. Defaults to False.

License

simple_flask_restful is released under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simple_flask_restful-0.2.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

simple_flask_restful-0.2.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file simple_flask_restful-0.2.0.tar.gz.

File metadata

  • Download URL: simple_flask_restful-0.2.0.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for simple_flask_restful-0.2.0.tar.gz
Algorithm Hash digest
SHA256 6a950d523ac383f69b7bd4f4a7702c17c1d0f1408e77660e68754fb7bc7e227b
MD5 c543a3202d21d6cbc370dce4916c0775
BLAKE2b-256 e5acacc5117088bfbc434466602308f87b6f3958d74b9e8cfb0f990c97c3349d

See more details on using hashes here.

File details

Details for the file simple_flask_restful-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for simple_flask_restful-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dce2b84905e52d5edfedde4539022526deb59ec6e8d48fc9f14de3c3d5151ddc
MD5 c1771f33c2422059c24d21022f68dd06
BLAKE2b-256 a6877e61d0e344b951e8ce29ac824bcf530c84080f22e88cfa23f1de50631a82

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page