0%

Flask-RESTful

Flask-RESTful

Flask-RESTful is an extension for Flask that adds support for quickly building REST APIs.

Install

1
pip install flask-restful

Basic Usage

Case project

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
test
├── models.py
├── routes.py
├── views.py
└── __init__.py

# models.py Create the SQLalchemy modelViews
class Car(db.Model):
__tablename__ = 'car'
carId = db.Column(db.INTEGER, primary_key=True)
carName = db.Column(db.String(10))

# views.py Create the RESTful modelViews
from flask_restful import Resource, fields, marshal_with
from .models import Car
class CarView(Resource):
resource_fields = {
'name': fields.String(attribute='carName'),
'id': fields.Integer(attribute='carId'),
}
@marshal_with(resource_fields)
def get(self):
data = Car.query.first()
print(data.carId)
return data
def post(self):
pass

# routes.py Register the Blueprints and add the resources
from .views import *
from flask_restful import Api
from . import test_bp
def add_resources(api):
api.add_resource(CarView, '/data')
def register_blueprints(app):
app.register_blueprint(test_bp, url_prefix='/test')

def init_app(app):
api = Api(test_bp)
register_blueprints(app)
add_resources(api)

Renaming Attributes

1
2
3
4
resource_fields = {
'name': fields.String(attribute='DatabaseName'),
'id': fields.Integer(attribute='DatabaseId'),
}

Default Values

1
2
3
4
fields = {
'name': fields.String(default='SmallStars'),
'address': fields.String,
}

Fields

  • String (default=None, attribute=None)

  • FormattedString (src_str)

    1
    2
    # If the key is not found in the object, returns the default value.
    fields.FormattedString("Hello {name}")
  • Url (endpoint=None, absolute=False, scheme=None, **kwarg)

    • endpoint (str) – Endpoint name. If endpoint is None, request.endpoint is used instead
    • absolute (bool) – If True, ensures that the generated urls will have the hostname included
    • scheme (str) – URL scheme specifier (e.g. http, https)
  • DateTime (dt_format=’rfc822’, **kwargs)

    • dt_format (str) – 'rfc822' or 'iso8601'
  • Float (default=None, attribute=None)

  • Integer (default=0, **kwargs)

  • Nested (nested, allow_null=False, **kwargs)

  • List (cls_or_instance, **kwargs)

  • Boolean (default=None, attribute=None)

  • Fixed (decimals=5, **kwargs)

marshal_with and marshal

  • marshal_with Return the processed data directly

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    # @marshal_with(car_fields)
    class CarView(Resource):
    car_fields = {
    'name': fields.String(attribute='carName'),
    'id': fields.String(attribute='carId')
    }

    @marshal_with(car_fields)
    def get(self):
    data = Car.query.all()
    return data
  • marshal Can return the custom data structure

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    class CarView(Resource):
    car_fields = {
    'name': fields.String(attribute='carName'),
    'id': fields.String(attribute='carId')
    }

    # @marshal_with(car_fields)
    def get(self):
    data = Car.query.all()
    data = marshal(data, CarView.car_fields)
    return {
    'data': data,
    'status': 'successful'
    }

References

-------------The end of this article, thanks for reading-------------