Skip to content

Commit

Permalink
feat: support OpenAPI schema foreach models
Browse files Browse the repository at this point in the history
  • Loading branch information
Angel-Dijoux committed Jan 3, 2024
1 parent e6370d6 commit 8e18064
Show file tree
Hide file tree
Showing 17 changed files with 262 additions and 150 deletions.
4 changes: 4 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ pytest-mock = "*"
pytest-socket = "*"
pytest-sugar = "*"
factory-boy = "*"
apispec-webframeworks = "*"
flask-restful = "*"
flask-marshmallow = "*"
marshmallow-sqlalchemy = "*"

[dev-packages]

Expand Down
167 changes: 116 additions & 51 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,21 @@ class Config(object):
SQLALCHEMY_TRACK_MODIFICATIONS = False
JWT_SECRET_KEY = SECRET_KEY

SWAGGER = {"title": "Onisep_User API", "uiversion": "3", "version": "1.0.4"}
API_VERSION = "1.0.5"

SWAGGER = {"title": "Onisep Explorer", "uiversion": "3", "version": API_VERSION}


class DevelopmentConfig(Config):
DEBUG = True
SQLALCHEMY_DATABASE_URI = get_db_dev_uri()

SWAGGER = {
"title": "Onisep Explorer DEV",
"uiversion": "3",
"version": Config.API_VERSION,
}


class ProductionConfig(Config):
DEBUG = False
Expand Down
23 changes: 20 additions & 3 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,23 @@
from loguru import logger

from config import load_config
from src.config.swagger import swagger_config, template
from src.config.swagger import (
get_swagger_api_spec,
swagger_config,
)
from src.models.schemas import schemas
from flasgger.utils import apispec_to_template

from .errors import register_error_handlers
from .middlewares import after_request
from apispec.ext.marshmallow import MarshmallowPlugin
from apispec_webframeworks.flask import FlaskPlugin

db = SQLAlchemy()
db: SQLAlchemy = SQLAlchemy()
plugins = [
FlaskPlugin(),
MarshmallowPlugin(),
]


def create_app(environment=None):
Expand All @@ -33,10 +44,16 @@ def create_app(environment=None):
with app.app_context():
register_blueprints(app)

template = apispec_to_template(
app=app,
spec=get_swagger_api_spec(config=config, plugins=plugins),
definitions=schemas,
)
Swagger(app, config=swagger_config, template=template, parse=True)

app.after_request(after_request)
register_error_handlers(app)

Swagger(app, config=swagger_config, template=template)
return app


Expand Down
2 changes: 1 addition & 1 deletion src/blueprints/favoris.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def _create_new_formation(favori: dict) -> Formation:

@favoris.route("/", methods=["POST"])
@jwt_required()
@swag_from("../docs/favoris/postFavoris.yaml")
@swag_from("../docs/favoris/postFavoris.yaml", validation=True)
def post_favori_by_user_id() -> Tuple[Response, int] | HTTPException:
current_user = get_jwt_identity()

Expand Down
61 changes: 36 additions & 25 deletions src/config/swagger.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,47 @@
from src.constants.env import is_dev
from apispec import APISpec
from apispec_webframeworks.flask import FlaskPlugin
from apispec.ext.marshmallow import MarshmallowPlugin
from config import Config

from config import ProductionConfig

template = {
"swagger": "2.0",
"info": {
"title": ProductionConfig.SWAGGER["title"],
"description": "API for user want register onisep formation",
def get_swagger_api_spec(
config: Config, plugins: list[FlaskPlugin | MarshmallowPlugin]
) -> APISpec:
info = {
**config.SWAGGER,
"description": "API for user registration on Onisep formation",
"termsOfService": "/privacy_policy",
"contact": {
"responsibleOrganization": "",
"responsibleDeveloper": "",
"email": "[email protected]",
"url": "https://twitter.com/Elki_YT",
"url": "https://github.com/Angel-Dijoux",
},
"termsOfService": "https://twitter.com/Elki_YT",
"version": ProductionConfig.SWAGGER["version"],
},
"basePath": "/api/v1", # base bash for blueprint registration
"schemes": ["http" if is_dev() else "https"],
"securityDefinitions": {
"Bearer": {
"type": "apiKey",
"name": "Authorization",
"in": "header",
"description": 'JWT Authorization header using the Bearer scheme. Example: "Authorization: Bearer {token}"',
}
},
"security": [{"Bearer": []}],
}
"license": {
"name": "Apache 2.0",
"url": "https://www.apache.org/licenses/LICENSE-2.0.html",
},
}
spec = APISpec(
title=config.SWAGGER["title"],
version=config.SWAGGER["version"],
openapi_version="2.0",
plugins=plugins,
info=info,
)
api_key_scheme = {"type": "apiKey", "in": "header", "name": "X-API-Key"}
spec.components.security_scheme("Bearer", api_key_scheme)
return spec


swagger_config = {
"headers": [],
"host": ["localhost:5005" if is_dev() else "api.nc-elki.v6.army"],
"basePath": "/api/v1",
"schemes": ["http" if is_dev() else "https"],
"headers": [
("Access-Control-Allow-Origin", "*"),
("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"),
("Access-Control-Allow-Credentials", "true"),
],
"specs": [
{
"endpoint": "apispec",
Expand Down
2 changes: 2 additions & 0 deletions src/docs/auth/login/register.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ parameters:
responses:
201:
description: Lorsqu'un utilisateur s'enregistre avec succès.
schema:
$ref: "#/definitions/User"

400:
description: Un utilisateur fournit des informations d'identification incorrectes.
2 changes: 2 additions & 0 deletions src/docs/auth/me.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ get:
responses:
200:
description: ok.
schema:
$ref: "#/definitions/User"
Loading

0 comments on commit 8e18064

Please sign in to comment.