No project description provided
Project description
Flaskerk
Provide OpenAPI document and validation for flask service.
Mainly built for Machine Learning Model services.
If you're using Falcon, check my another library Falibrary.
Features
- JSON data(request&response) validation with pydantic
- support HTTP exceptions (default&customized)
- OpenAPI spec
- Redoc UI
- Swagger UI
- support flask url path validation
- support header validation
- support cookie validation
Quick Start
install with pip install flaskerk
(Python 3.6+)
Simple demo
from flask import Flask, request, jsonify
from flaskerk import Flaskerk
from pydantic import BaseModel
class Query(BaseModel):
text: str
app = Flask(__name__)
api = Flaskerk()
@app.route('/api/classify')
@api.validate(query=Query)
def classify():
print(request.query)
return jsonify(label=0)
if __name__ == "__main__":
api.register(app)
app.run()
Changes you need to make:
- create model with
pydantic
- decorate the route function with
Flaskerk.validate()
- specify which part you need in
validate
query
(args in url)data
(JSON data)resp
(response)x
(HTTP Exceptions)
- register to Flask application
After that, this library will help you validate the incoming request and provide API document in /docs
.
For more details, check the document.
More feature
from flask import Flask, request
from pydantic import BaseModel, Schema
from random import random
from flaskerk import Flaskerk, HTTPException
app = Flask(__name__)
api = Flaskerk()
class Query(BaseModel):
text: str
class Response(BaseModel):
label: int
score: float = Schema(
...,
gt=0,
lt=1,
)
class Data(BaseModel):
uid: str
limit: int
vip: bool
e403 = HTTPException(code=403, msg='lucky for you')
@app.route('/api/predict/<string(length=2):source>/<string(length=2):target>', methods=['POST'])
@api.validate(query=Query, data=Data, resp=Response, x=[e403])
def predict(source, target):
print(f'=> from {source} to {target}') # path
print(f'Data: {request.json_data}') # Data
print(f'Query: {request.query}') # Query
if random() < 0.5:
e403.abort()
return Response(label=int(10 * random()), score=random())
if __name__ == '__main__':
api.register(app)
app.run()
try it with http POST ':5000/api/predict/zh/en?text=hello' uid=0b01001001 limit=5 vip=true
Open the docs in http://127.0.0.1:5000/docs .
For more examples, check examples.
FAQ
Can I just do the validation without generating API document?
Sure. If you don't register it to Flask application, there won't be document routes.
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file flaskerk-0.5.2.tar.gz
.
File metadata
- Download URL: flaskerk-0.5.2.tar.gz
- Upload date:
- Size: 9.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.31.1 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 02362ccfd9ae3e223a4ebe468d22376469be8c45e9f7bfe7ecb56d970d307b8b |
|
MD5 | 95f65ec70cd2fd7edda554f015eaf1dd |
|
BLAKE2b-256 | 0ee2491244fda8ddddf4c53abfb9c32d33b0c9fed1070cfc2dd8722d979ae78e |
File details
Details for the file flaskerk-0.5.2-py3-none-any.whl
.
File metadata
- Download URL: flaskerk-0.5.2-py3-none-any.whl
- Upload date:
- Size: 10.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.31.1 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 88784b9002170be86fd6151d73ea1694e3c48a04f1509e36193a08d2c356b25e |
|
MD5 | ff63b1d92e897ec2a403c5e4b6b7064a |
|
BLAKE2b-256 | 0a0e09fcef1d3519217c947cad634a16076a3decb1e03b38a3c00317d8e55318 |