Skip to main content

Installation

pip install flask-rest-framework

Test

py.test .

Overview

flask-rest-framework is inspired by Django REST framework

You can use this extension to develop your rest api quickly based on flask, each view contains this:

  • Authentication policies
  • Permission
  • Throttle

and each of them can be customized yourself, all of those are revolved around User, so you can define your own User class.

Example

from flask import Flask,jsonify
from flask_restframework import RestFramework

app = Flask(__name__)
rf = RestFramework()
rf.init_app(app)

from flask_restframework.views import APIView
from flask_restframework.authentication import BasicAuthentication,JWTAuthentication
from flask_restframework.permissions import AllowAny,IsAuthenticated

class PingView(APIView):

    authentication_classes=[BasicAuthentication, JWTAuthentication]
    permission_classes=[IsAuthenticated,]

    def get(self, *args, **kwargs):
        return jsonify({"args":args,"kwargs":kwargs,"request.args":request.args})

app.add_url_rule("/ping/<string:name>",view_func=PingView.as_view('ping'))

if __name__ == "__main__":
    app.run()

User

if you define your own User class, must configure it in flask config env: FLASK_RESTFRAMEWORK_USER_CLASS

app.config['FLASK_RESTFRAMEWORK_USER_CLASS] = 'your_user_class_path.YourUser'

and User class must has is_authenticated attribute, the type is boolean, this attribute will be used in permission.

more detail can see flask_restframework.user.BaseUser, i recomend your class inherit from it.

we use the User in authentication

Authenticaion

we offer BasicAuthentication and JWTAuthentication authentication class here, you could custom your authentication class or inherit them to complete auth

Permission

AllowAny permission class allows anyone access your API without authentication;

IsAuthenticated user must be authenticated before accessing API;

IsAuthenticatedOrReadOnly allow anyone access API if request method is safe('get','head','options'), else must be authenticated.

Throttling

before using throttle, we must configure cache to app, else it will not work:

from xxx import Cache
...
app = Flask(__name__)
cache = Cache()
rf = RestFramework()
rf.init_app(app,cache)
...

here we offer AnonRateThrottle and UserRateThrottle.

and the rate of throttling can be set by second,minute,hour,day.

...
class YourView(APIView):
    authentication_classes=[BasicAuthentication, JWTAuthentication]
    throttle_handlers = [{"class":AnonRateThrottle,"rate":"1/hour"},{"class":UserRateThrottle,"rate":"10/minute"}]
...

AnonRateThrottle

the AnonRateThrottle is for throttling anonymous user, namely permission class is AllowAny, if user is authenticated, it will not limit.

UserRateThrottle

the UserRateThrottle is for throttling authenticated user, if user is not authenticated, it also work.

Metadata

Release files for flask-rest-framework 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flask-rest-framework 0.0.3
File Size Uploaded
flask-rest-framework-0.0.3.tar.gz 12.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flask-rest-framework 0.0.3
File Interpreter ABI Platform
flask_rest_framework-0.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 28.7 kB

Release files / flask-rest-framework-0.0.3.tar.gz

Download URL flask-rest-framework-0.0.3.tar.gz
Size 12.1 kB
Tags Source
SHA-256 checksum
How to use checksums
b1ed9a933344fba3ebe7e794668653a114324e6aecc3f6f6bf73a3d07437c459
BLAKE2b-256 checksum
How to use checksums
fac926ac86dd83ff7077c90243759e66d903d8a02ae8ff3d95325c6e34efd9d9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.8.1 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.9

Release files / flask_rest_framework-0.0.3-py3-none-any.whl

Download URL flask_rest_framework-0.0.3-py3-none-any.whl
Size 16.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5cdfc072a5724df7116d7b0bf31848757336a98080f0dc16729951c2e0157781
BLAKE2b-256 checksum
How to use checksums
174e0561093ce2ef44a68a3a80f3186c19de148c1bbcea6ffd4b9a323655fd4b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.8.1 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.9

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page