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
Built Distribution
Hashes for flask_decorator_tools-0.0.8.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | c8e9581aeb9ecaafc07218df5a077800b4dfa7ad6c722da4a13b029267b6176d |
|
MD5 | 4e25ac2fc458f6b5191371da861d92bc |
|
BLAKE2b-256 | 7b098c8fbf767b8b35cc9215bd28f226f9f3065d4a59a003c870781011e59994 |
Hashes for flask_decorator_tools-0.0.8-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5aa27d675450c711f4be969cb28d75ebd9da9adcae3e4d2c5c9460e9b4e652da |
|
MD5 | b3f075209023b05569a694630b225de7 |
|
BLAKE2b-256 | 4843b89b66b030be727ffcc1e833480b71b69bca3d3463a5691a06b35d3238dc |