All Projects → kemingy → flaskerk

kemingy / flaskerk

Licence: MIT License
A flask extension for api doc and validation of request&response.

Programming Languages

python
139335 projects - #7 most used programming language
HTML
75241 projects
Makefile
30231 projects

Projects that are alternatives of or similar to flaskerk

Swagger2
Loading, parsing and validating requests to HTTP services based on Swagger v2.0 documents
Stars: ✭ 20 (-16.67%)
Mutual labels:  validation, openapi
symmetric
A powerful tool to enable super fast module-to-API transformations. Learn in minutes, implement in seconds. Batteries included.
Stars: ✭ 65 (+170.83%)
Mutual labels:  openapi, redoc
Openapi Spring Webflux Validator
🌱 A friendly kotlin library to validate API endpoints using an OpenApi 3.0 and Swagger 2.0 specification
Stars: ✭ 67 (+179.17%)
Mutual labels:  validation, openapi
Dredd
Language-agnostic HTTP API Testing Tool
Stars: ✭ 3,770 (+15608.33%)
Mutual labels:  validation, openapi
asymmetric
Ridiculously fast and easy module-to-API transformations. Learn in minutes, implement in seconds. Batteries included.
Stars: ✭ 35 (+45.83%)
Mutual labels:  openapi, redoc
Swagger Parser
Swagger 2.0 and OpenAPI 3.0 parser/validator
Stars: ✭ 710 (+2858.33%)
Mutual labels:  validation, openapi
Openapi Psr7 Validator
It validates PSR-7 messages (HTTP request/response) against OpenAPI specifications
Stars: ✭ 168 (+600%)
Mutual labels:  validation, openapi
Flasgger
Easy OpenAPI specs and Swagger UI for your Flask API
Stars: ✭ 2,825 (+11670.83%)
Mutual labels:  openapi, flask-extensions
spectree
API spec validator and OpenAPI document generator for Python web frameworks.
Stars: ✭ 190 (+691.67%)
Mutual labels:  openapi, redoc
cakephp-swagger-bake
Automatically generate OpenAPI, Swagger, and Redoc documentation from your existing CakePHP code.
Stars: ✭ 48 (+100%)
Mutual labels:  openapi, redoc
Openapi Cop
A proxy that validates responses and requests against an OpenAPI document.
Stars: ✭ 338 (+1308.33%)
Mutual labels:  validation, openapi
openapi-lint-vscode
OpenAPI 2.0/3.0.x intellisense, validator, linter, converter and resolver extension for Visual Studio Code
Stars: ✭ 47 (+95.83%)
Mutual labels:  validation, openapi
Jsonschema
An implementation of the JSON Schema specification for Python
Stars: ✭ 3,474 (+14375%)
Mutual labels:  validation, openapi
Openapi3 Validator
Validator for OpenAPI v3 specs
Stars: ✭ 11 (-54.17%)
Mutual labels:  validation, openapi
openapi-schemas
JSON Schemas for every version of the OpenAPI Specification
Stars: ✭ 22 (-8.33%)
Mutual labels:  validation, openapi
Openapi Spec Validator
OpenAPI Spec validator
Stars: ✭ 161 (+570.83%)
Mutual labels:  validation, openapi
Drf Yasg
Automated generation of real Swagger/OpenAPI 2.0 schemas from Django REST Framework code.
Stars: ✭ 2,523 (+10412.5%)
Mutual labels:  openapi, redoc
Redoc
📘 OpenAPI/Swagger-generated API Reference Documentation
Stars: ✭ 15,935 (+66295.83%)
Mutual labels:  openapi, redoc
starlite
Light, Flexible and Extensible ASGI API framework
Stars: ✭ 1,525 (+6254.17%)
Mutual labels:  openapi, redoc
apiflask
A lightweight Python web API framework.
Stars: ✭ 442 (+1741.67%)
Mutual labels:  openapi, redoc

Flaskerk

Build Status GitHub PyPI - Python Version

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.

Attention

I've created a new library named SpecTree. It supports all the features of Flaskerk, but with a better interface. Give it a try! This library may be archived in the future.

Features

  • Generate API document with Redoc UI or Swagger UI 😋
  • Less boilerplate code, annotations are really easy-to-use
  • Validate query, JSON data, response data with pydantic 😉
  • Better HTTP exceptions for API services (default & customized) (JSON instead of HTML) 😬

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 from request)
    • resp (response) this will be transformed to JSON data after validation
    • x (HTTP Exceptions list)
    • tags (tags for this API route)
  • register to Flask application

After that, this library will help you validate the incoming request and provide API document in /docs.

Parameters in Flaskerk.validate Corresponding parameters in Flask
query request.query
data request.json_data
resp \
x \

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(
    title='Demo Service',
    version='1.0',
    ui='swagger',
)

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

e233 = HTTPException(code=233, 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=[e233], tags=['model'])
def predict(source, target):
    """
    predict demo

    demo for `query`, `data`, `resp`, `x`
    """
    print(f'=> from {source} to {target}')  # path
    print(f'Data: {request.json_data}')  # Data
    print(f'Query: {request.query}')  # Query
    if random() < 0.5:
        e233.abort('bad luck')
    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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].