-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
85 lines (62 loc) · 2.86 KB
/
main.py
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
81
82
83
84
85
from functools import partial
from typing import Dict, Any
from fastapi import (
FastAPI,
routing
)
from fastapi.openapi.utils import get_openapi
from app.main.endpoints.eps import router as eps_router
from open_api.schemas import default_ref_template
from fastapi_xmlrpc.middleware import XMLRPCMiddleware
from fastapi_xmlrpc.errors import *
def custom_openapi():
if app.openapi_schema:
return app.openapi_schema
definitions: Dict[str, Dict[str, Any]] = {}
for route in app.routes:
if isinstance(route, routing.APIRoute) and hasattr(route, "openapi_extra"):
try:
for content in route.openapi_extra["requestBody"]["content"].values():
content["schema"]['xml'] = {"name": "methodResponse"}
definitions |= content["schema"].pop("definitions", {})
definitions[content["schema"]["title"]] = content["schema"]
content["schema"] = {"$ref": default_ref_template.format(model=content["schema"]["title"])}
for _ in route.responses.values():
for content in _['content'].values():
content["schema"]['xml'] = {"name": "methodResponse"}
definitions |= content["schema"].pop("definitions", {})
definitions[content["schema"]["title"]] = content["schema"]
content["schema"] = {"$ref": default_ref_template.format(model=content["schema"]["title"])}
except KeyError:
continue
except TypeError:
continue
openapi_schema = get_openapi(
title="Custom title",
version="2.5.0",
description="This is a very custom OpenAPI schema",
routes=app.routes
)
openapi_schema.setdefault("components", {}).setdefault("schemas", {}).update(definitions)
app.openapi_schema = openapi_schema
return app.openapi_schema
def get_application() -> FastAPI:
application = FastAPI()
application.add_middleware(
XMLRPCMiddleware,
router=eps_router
)
# required after add_middleware
application.include_router(eps_router)
application.add_exception_handler(RequestValidationError, request_validation_error_handler)
application.add_exception_handler(XMLRPCError, xmlrpc_error_handler)
application.add_exception_handler(HTTPException, http_error_handler)
application.add_exception_handler(Exception, global_error_handler)
###################
application.add_exception_handler(TypeError, global_error_handler)
application.add_exception_handler(TabError, global_error_handler)
application.add_exception_handler(ZeroDivisionError, global_error_handler)
application.add_exception_handler(KeyError, global_error_handler)
application.openapi = custom_openapi
return application
app = get_application()