Axione-IPE-Viewer/webapp/eligibility_api/api_doc.py
2023-10-06 15:50:35 +02:00

56 lines
1.4 KiB
Python

from apispec.ext.marshmallow import MarshmallowPlugin
from apispec_webframeworks.flask import FlaskPlugin
from flasgger import APISpec, Schema, Swagger, fields
from eligibility_api.elig_api_exceptions import SwaggerValidationException
def _swagger_validation_error_handler(err, data, _):
print("API swagger error 400")
raise SwaggerValidationException(err, data)
def start_swagger(flask_app):
"""
Creates swagger /apidocs endpoint and adds it to flask app
"""
flask_app.config["SWAGGER"] = {
"uiversion": 3,
"ui_params": {
"apisSorter": "alpha",
"operationsSorter": "alpha",
"tagsSorter": "alpha",
},
}
# Create an APISpec
spec = APISpec(
title="API éligibilité",
version="0.1.0",
openapi_version="2.0",
plugins=(
FlaskPlugin(),
MarshmallowPlugin(),
),
)
template = spec.to_flasgger(
flask_app,
definitions=[
# ContributionSchema,
# PredictConfigSchema,
# VersionSchema,
# PredictInputsSchema,
# ImplementedModelSchema,
# ApiErrorSchema,
# LoggingHealthCheckSchema,
],
)
Swagger(
flask_app,
template=template,
parse=True,
validation_error_handler=_swagger_validation_error_handler,
)