-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
80 lines (59 loc) · 2.16 KB
/
main.py
File metadata and controls
80 lines (59 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from fastapi import FastAPI,Depends, Request,HTTPException
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from fastapi_jwt_auth import AuthJWT
from fastapi_jwt_auth.exceptions import AuthJWTException
from fastapi.openapi.utils import get_openapi
from responsables.router import router as responsablesRouter
from decouple import config
from responsables.Services import Utilities
from starlette.exceptions import HTTPException as StarletteHTTPException
app = FastAPI()
@app.exception_handler(AuthJWTException)
def authjwt_exception_handler(request: Request, exc: AuthJWTException):
print("token",request.headers)
print("path",request.url.path)
print("url",request.url)
print("schema", request.url.scheme)
return JSONResponse(
status_code=exc.status_code,
content={"detail": exc.message}
)
app.include_router(
responsablesRouter,
prefix="",
tags=["users"]
)
def custom_openapi():
if app.openapi_schema:
return app.openapi_schema
openapi_schema = get_openapi(
title="SEVi",
version="2.5.0",
description="API Documentation",
routes=app.routes,
)
# Custom documentation fastapi-jwt-auth
headers = {
"name": "Authorization",
"in": "header",
"required": True,
"schema": {
"title": "Authorization",
"type": "string"
},
}
# Get routes from index 4 because before that fastapi define router for /openapi.json, /redoc, /docs, etc
# Get all router where operation_id is authorize
router_authorize = [route for route in app.routes[4:] if "Autheticated" in route.tags]
for route in router_authorize:
method = list(route.methods)[0].lower()
try:
# If the router has another parameter
openapi_schema["paths"][route.path][method]['parameters'].append(headers)
except Exception:
# If the router doesn't have a parameter
openapi_schema["paths"][route.path][method].update({"parameters":[headers]})
app.openapi_schema = openapi_schema
return app.openapi_schema
app.openapi = custom_openapi