Skip to content

Commit a8ebc55

Browse files
committed
refactor: operation_id generation the fastapi100 way
1 parent 64c53fd commit a8ebc55

File tree

1 file changed

+41
-41
lines changed

1 file changed

+41
-41
lines changed

src/labs/api.py

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,44 @@
2323

2424
from .schema.ext import RootResponse
2525

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+
2642
"""A FastAPI application that serves handlers
2743
"""
2844
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,
3855
)
3956

57+
4058
@app.websocket("/ws")
4159
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}")
4664

4765

4866
# Additional routers of the application described in the routers package
@@ -56,42 +74,24 @@ async def app_startup():
5674
await broker.startup()
5775

5876
# On shutdown, we need to shutdown the broker
77+
78+
5979
@app.on_event("shutdown")
6080
async def app_shutdown():
6181
if not broker.is_worker_process:
6282
await broker.shutdown()
6383

6484
# Default handler
85+
86+
6587
@app.get(
6688
"/",
6789
status_code=status.HTTP_200_OK,
6890
)
6991
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

Comments
 (0)