23
23
24
24
from .schema .ext import RootResponse
25
25
26
+
27
+ def generate_operation_id (route : APIRoute ) -> str :
28
+ """
29
+ With a little help from FastAPI docs
30
+ https://bit.ly/3rXeAvH
31
+
32
+ Globally use the path name as the operation id thus
33
+ making things a lot more readable, note that this requires
34
+ you name your functions really well.
35
+
36
+ Read more about this on the FastAPI docs
37
+ https://shorturl.at/vwz03
38
+ """
39
+ return route .name
40
+
41
+
26
42
"""A FastAPI application that serves handlers
27
43
"""
28
44
app = FastAPI (
29
- title = __title__ ,
30
- version = __version__ ,
31
- description = settings .api_router .__doc__ ,
32
- docs_url = settings .api_router .path_docs ,
33
- root_path = settings .api_router .path_root ,
34
- terms_of_service = settings .api_router .terms_of_service ,
35
- contact = settings .api_router .contact ,
36
- license_info = settings .api_router .license_info ,
37
- openapi_tags = settings .api_router .open_api_tags
45
+ title = __title__ ,
46
+ version = __version__ ,
47
+ description = settings .api_router .__doc__ ,
48
+ docs_url = settings .api_router .path_docs ,
49
+ root_path = settings .api_router .path_root ,
50
+ terms_of_service = settings .api_router .terms_of_service ,
51
+ contact = settings .api_router .contact ,
52
+ license_info = settings .api_router .license_info ,
53
+ openapi_tags = settings .api_router .open_api_tags ,
54
+ generate_unique_id_function = generate_operation_id ,
38
55
)
39
56
57
+
40
58
@app .websocket ("/ws" )
41
59
async def websocket_endpoint (websocket : WebSocket ):
42
- await websocket .accept ()
43
- while True :
44
- data = await websocket .receive_text ()
45
- await websocket .send_text (f"Message text was: { data } " )
60
+ await websocket .accept ()
61
+ while True :
62
+ data = await websocket .receive_text ()
63
+ await websocket .send_text (f"Message text was: { data } " )
46
64
47
65
48
66
# Additional routers of the application described in the routers package
@@ -56,42 +74,24 @@ async def app_startup():
56
74
await broker .startup ()
57
75
58
76
# On shutdown, we need to shutdown the broker
77
+
78
+
59
79
@app .on_event ("shutdown" )
60
80
async def app_shutdown ():
61
81
if not broker .is_worker_process :
62
82
await broker .shutdown ()
63
83
64
84
# Default handler
85
+
86
+
65
87
@app .get (
66
88
"/" ,
67
89
status_code = status .HTTP_200_OK ,
68
90
)
69
91
async def root (request : Request ) -> RootResponse :
70
- """ Placeholder for the root endpoint
71
- """
72
- return RootResponse (
73
- message = "Welcome to the {} API" .format (__name__ ),
74
- root_path = request .scope .get ("root_path" )
75
- )
76
-
77
- # Hook up any events worth responding to
78
- # https://fastapi.tiangolo.com/advanced/events/
79
-
80
- # With a little help from FastAPI docs
81
- # https://bit.ly/3rXeAvH
82
- #
83
- # Globally use the path name as the operation id thus
84
- # making things a lot more readable, note that this requires
85
- # you name your functions really well.
86
- def use_route_names_as_operation_ids (app : FastAPI ) -> None :
87
- """
88
- Simplify operation IDs so that generated API clients have simpler function
89
- names.
90
-
91
- Should be called only after all routes have been added.
92
- """
93
- for route in app .routes :
94
- if isinstance (route , APIRoute ):
95
- route .operation_id = route .name
96
-
97
- use_route_names_as_operation_ids (app )
92
+ """ Placeholder for the root endpoint
93
+ """
94
+ return RootResponse (
95
+ message = "Welcome to the {} API" .format (__name__ ),
96
+ root_path = request .scope .get ("root_path" )
97
+ )
0 commit comments