Skip to main content

A package with tools for use with flask

Project description

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.

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

flask_decorator_tools-0.0.8.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flask_decorator_tools-0.0.8-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file flask_decorator_tools-0.0.8.tar.gz.

File metadata

  • Download URL: flask_decorator_tools-0.0.8.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for flask_decorator_tools-0.0.8.tar.gz
Algorithm Hash digest
SHA256 c8e9581aeb9ecaafc07218df5a077800b4dfa7ad6c722da4a13b029267b6176d
MD5 4e25ac2fc458f6b5191371da861d92bc
BLAKE2b-256 7b098c8fbf767b8b35cc9215bd28f226f9f3065d4a59a003c870781011e59994

See more details on using hashes here.

File details

Details for the file flask_decorator_tools-0.0.8-py3-none-any.whl.

File metadata

  • Download URL: flask_decorator_tools-0.0.8-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for flask_decorator_tools-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 5aa27d675450c711f4be969cb28d75ebd9da9adcae3e4d2c5c9460e9b4e652da
MD5 b3f075209023b05569a694630b225de7
BLAKE2b-256 4843b89b66b030be727ffcc1e833480b71b69bca3d3463a5691a06b35d3238dc

See more details on using hashes here.

Supported by

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