Skip to main content

Flask Tools

This is a library that provides some simple tools for making an API with flask.

Installation

pip install flask-decorator-tools

Usage

Serialise and Deserialise

Decorators for flask routes are available for serialisation and deserialisation. To serialise return an object and a response code and the response will be serialised to JSON. If no response code is given a response code of 200 is assumed.

@app.route('/test', methods = ['POST'])
@serialise
@authenticate()
def post_form_entry():
    testOutputClass = TestOutputClass()
    return testOutputClass, 200

To deserialise a class must be passed to the decorator, the incoming JSON body will then be deserialised to an object that is passed to the route as a parameter. The same applys for deserialising the args for a request.

@app.route('/test', methods = ['POST'])
@deserialise(TestInputClass)
@serialise
@authenticate()
def post_form_entry(testInputClass : TestInputClass):
    print(testInputClass)
    testOutputClass = TestOutputClass()
    return TestOutputClass, 200

@app.route('/test', methods = ['POST'])
@deserialise_args(FormEntryArgs)
@serialise
@authenticate()
def post_form_entry(args : TestInputClass):
    print(args)
    testOutputClass = TestOutputClass()
    return TestOutputClass, 200

The python classes that are deserialised to must accept a dictionary in their constructor. It is recommened that the pip package json-schema-to-class is used to generate the classes from json schemas.

Configs

A tool is provided to pass configs from a JSON config file. The configs can be read from a json file like below.

{
    "tokens":{
        "token1":100,
        "token2":"2"
    }
    "database":{
        "keys":{
            "first_key":"first",
            "second_key":"second"
        },
        "codes":{
            "first_code":"first",
            "first_code":"second"
        }
    }
}

This json object can then have configs read as follows.

@parse("configs/config.json","tokens")
@dataclass
class TokensConfig:
  token1: int
  token2: str
tokensConfig = TokensConfig()

@parse("configs/config.json","database.keys")
@dataclass
class DataBaseKeyConfig:
  first_key: str
  second_key: str
dataBaseKeyConfig = DataBaseKeyConfig()

@parse("configs/config.json","database.codes")
@dataclass
class DataBaseKeyConfig:
  first_key: str
  second_key: str
dataBaseKeyConfig = DataBaseKeyConfig()

The parse decorator is used in conjunction with the dataclass decorator. The parse decorator takes two parameters, the first is the route to the config file to read from and the second is the route through the json config object to the object to fill the class from. It is expected that an instance of the class will be created once.

Metadata

Release files for flask-decorator-tools 0.0.8

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-decorator-tools 0.0.8
File Size Uploaded
flask_decorator_tools-0.0.8.tar.gz 3.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flask-decorator-tools 0.0.8
File Interpreter ABI Platform
flask_decorator_tools-0.0.8-py3-none-any.whl Python 3 none any Details

Total release size: 8.2 kB

Release files / flask_decorator_tools-0.0.8.tar.gz

Download URL flask_decorator_tools-0.0.8.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
c8e9581aeb9ecaafc07218df5a077800b4dfa7ad6c722da4a13b029267b6176d
BLAKE2b-256 checksum
How to use checksums
7b098c8fbf767b8b35cc9215bd28f226f9f3065d4a59a003c870781011e59994
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.5.0 importlib_metadata/4.0.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

Release files / flask_decorator_tools-0.0.8-py3-none-any.whl

Download URL flask_decorator_tools-0.0.8-py3-none-any.whl
Size 4.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5aa27d675450c711f4be969cb28d75ebd9da9adcae3e4d2c5c9460e9b4e652da
BLAKE2b-256 checksum
How to use checksums
4843b89b66b030be727ffcc1e833480b71b69bca3d3463a5691a06b35d3238dc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.5.0 importlib_metadata/4.0.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

Release history Release notifications | RSS feed

This release

0.0.8 This release

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

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