Doc¶
-
class
flaskerk.base.
Flaskerk
(app=None, **configs)[source]¶ - Parameters
app – Flask app instance, you can register it later
configs – key-value pairs in
flaskerk.config.Config
example:
from flask import Flask from flaskerk import Flaskerk app = Flask(__name__) api = Flaskerk(app, version='0.2', title='Machine Translation service')
-
register
(app)[source]¶ - Parameters
app – flask.Flask
Register to Flask application.
This function will update some config, and add some routes to Flask application. If you have already pass the Flask application when Flaskerk is initialized, then it will register automatically.
-
property
spec
¶ Get OpenAPI spec for this Flask app.
-
update_config
(**kwargs)[source]¶ Manually update config.
This function will be triggered when you register this library to Flask instance, and configs in Flask.config[‘OPENAPI’] will be used to update.
-
validate
(query=None, data=None, resp=None, x=[], tags=[])[source]¶ - Parameters
query –
pydantic.BaseModel
schema for request. The parsed data will store inflask.request.query
.resp –
pydantic.BaseModel
schema for responsedata –
pydantic.BaseModel
schema for JSON datax – List of
flaskerk.exception.HTTPException
tags – List of str, each one is a tag for this API route
validate JSON data according to Model schema
from pydantic import BaseModel from flask import request, jsonify class Query(BaseModel): text: str limit: int @app.route('/api/predict', methods=['POST']) @api.validate(data=Query) def predict(): data = request.json_data print(data.text, data.limit) return jsonify(is_spam=True)
For more examples, check examples.
-
class
flaskerk.config.
Config
[source]¶ - Variables
endpoint – url path for docs
filename – openapi spec file name
openapi_version – openapi spec version
title – document title
version – service version
ui – ui theme, choose ‘redoc’ or ‘swagger’
mode – mode for route. normal includes undecorated routes and routes decorated by this instance. strict only includes routes decorated by this instance. greedy includes all the routes.
Flaskerk configuration.
-
class
flaskerk.exception.
HTTPException
[source]¶ HTTP Exceptions.
- Parameters
code (int) – HTTP status code
msg (str) – description
If you are using default code like 403, 404, 500 …, you can ignore the
msg
parameter and this module will use default description.If you want to define your own code and description, you must offer
msg
.examples:
from flaskerk import HTTPException code404 = HTTPException(code=404) # with customized discription code403 = HTTPException(code=403, msg='IP is blocked.') # customized code code777 = HTTPException(code=777, msg='bad luck') # abort code777.abort()