kemingy / flaskerk Goto Github PK
View Code? Open in Web Editor NEWA flask extension for api doc and validation of request&response.
Home Page: https://kemingy.github.io/flaskerk
License: MIT License
A flask extension for api doc and validation of request&response.
Home Page: https://kemingy.github.io/flaskerk
License: MIT License
not sure if this works for flask-restful these class-based routes.
I'm trying out flaskerk
and attempting to define an endpoint that creates a resource and returns a HTTP 201 Created response. I typically return the newly created resource in the response body for those types of endpoints.
It looks like the current implementation supports customizing the successful response code by raising an HTTPException
with a 2xx code, but this doesn't appear to work in combination with a response schema (I think this was raised in #26). It's also a little unintuitive to me to use exceptions as a way of signaling a successful action. Would you be open to a PR that added an optional resp_code
(or perhaps status
... whatever you prefer) kwarg to the validate decorator? Something like so:
@app.route("/users", methods=["POST"])
@api.validate(data=User, resp=User, resp_code=201)
def create_user():
# create the user...
return user, 201
Currently, this project is tested with examples. Of course, this is not a suitable way.
Here is the list that should be tested:
flaskerk.utils.parse_url
for every converter with and without argsflaskerk.exception.HTTPException
default and customized exceptionsflaskerk.base.Flaskerk.validate
flaskerk.base.Flaskerk
Hi I was playing around with flaskerk and found if you have a method
@app.route('/cars', methods=['GET'])
@api.validate(resp=Cars)
def get_cars() -> Cars:
# return Car(**cars[0])
# return {'data': [Car(**x).to_json() for x in cars]}
# return Cars(cars=[Car(**x) for x in cars])
# return cars
return Cars(cars=cars)
@app.route('/cars', methods=['POST'])
@api.validate(resp=Car, data=Car)
def post_cars() -> Car:
data = request.json_data
return Car(name=data.name, price=data.price, desc=data.desc)
in swagger will display only one (last one) , I fix in the code in base.py in line 103 or 104
instead of just
routes = {}
I put this
if path not in routes.keys():
routes[path] = {}
and it worked
By default, if the user has any pydantic.BaseModel
data, this API will have a 422 exception.
But if the user has defined a 422 exception, the message may be overwritten.
When running http:http://127.0.0.1:8080/docs/, it raise he below error
jinja2.exceptions.TemplateNotFound: redoc.html
Hello,
First, I have to say that I love the work that you guys are doing with this library. I was already thinking in create validation and automatic OpenAPI documentation similar to what FastAPI is doing.
I have two requests for the current status of Flaskerk.
I would like to change the layout of the Swagger docs in the configs. Since I want to use the "BaseLayout" instead of the "StandaloneLayout". I could achieve this by creating a custom template folder, changing the swagger.html file and passing "template_folder" parameter in the constructor, but would be great to be able to choose between different layouts directly in the config.
We need a way to support tags. If you take a look in the code below you will see my current implementation.
from flask import Flask, request, jsonify
from flaskerk import Flaskerk
from pydantic import BaseModel
import os
class Query(BaseModel):
text: str
app = Flask(__name__)
api = Flaskerk(
title='Demo Service',
version='1.0',
ui='swagger',
template_folder=os.path.join(os.getcwd(), "templates_api")
)
@app.route('/api/first')
@api.validate(query=Query, tags=["first"])
def first():
print(request.query)
return jsonify(label=0)
@app.route('/api/second')
@api.validate(query=Query, tags=["second"])
def second():
print(request.query)
return jsonify(label=0)
if __name__ == "__main__":
api.register(app)
app.run()
Then in the base.py you should change the validate function for:
def validate(self, query=None, data=None, resp=None, x=[], tags=[]):
Add tags to the validate_request.
# register tags
validate_request.tags = tags
and finally, add them to the spec.
if hasattr(func, 'tags'):
spec['tags'] = func.tags
Response data declared in validate(resp=Response)
is bonded to status code 200 by default.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.