Skip to content

Commit f7d82f2

Browse files
authored
Merge pull request #31 from ks6088ts-labs/copilot/fix-30
Refactor router configuration to use explicit prefix, tags, and responses in app.py
2 parents 549a8bb + e715030 commit f7d82f2

File tree

13 files changed

+136
-123
lines changed

13 files changed

+136
-123
lines changed

.env.template

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ AZURE_AI_FOUNDRY_PROJECT_ENDPOINT="https://xxx.services.ai.azure.com/api/project
2323
AZURE_AI_FOUNDRY_API_KEY="<YOUR_API_KEY>"
2424

2525
# Chats WebSocket
26-
# Azure Container Apps: `wss://yourcontainerapps.japaneast.azurecontainerapps.io`
27-
CHATS_WEBSOCKET_URL="ws://localhost:8000"
26+
# Azure Container Apps: `wss://yourcontainerapps.japaneast.azurecontainerapps.io/chats`
27+
CHATS_WEBSOCKET_URL="ws://localhost:8000/chats"
2828

2929
# Microsoft Graph Sites
3030
MICROSOFT_GRAPH_TENANT_ID="<YOUR_TENANT_ID>"

template_fastapi/app.py

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
1212
from opentelemetry.trace import Span
1313

14-
from template_fastapi.routers import agents, chats, demos, files, foodies, games, items, speeches
14+
from template_fastapi.routers import agents, chats, demos, files, foodies, items, speeches
1515

1616
app = FastAPI()
1717

@@ -36,11 +36,54 @@ def server_request_hook(span: Span, scope: dict):
3636
FastAPIInstrumentor.instrument_app(app)
3737

3838
# Include routers
39-
app.include_router(items.router)
40-
app.include_router(demos.router)
41-
app.include_router(games.router)
42-
app.include_router(foodies.router)
43-
app.include_router(files.router)
44-
app.include_router(speeches.router)
45-
app.include_router(chats.router)
46-
app.include_router(agents.router)
39+
# Routers configuration list
40+
routersConfig = [
41+
{
42+
"router": agents.router,
43+
"prefix": "/agents",
44+
"tags": ["agents"],
45+
},
46+
{
47+
"router": chats.router,
48+
"prefix": "/chats",
49+
"tags": ["chats"],
50+
},
51+
{
52+
"router": demos.router,
53+
"prefix": "/demos",
54+
"tags": ["demos"],
55+
},
56+
{
57+
"router": files.router,
58+
"prefix": "/files",
59+
"tags": ["files"],
60+
},
61+
{
62+
"router": foodies.router,
63+
"prefix": "/foodies",
64+
"tags": ["foodies"],
65+
},
66+
{
67+
"router": items.router,
68+
"prefix": "/items",
69+
"tags": ["items"],
70+
},
71+
{
72+
"router": speeches.router,
73+
"prefix": "/speeches",
74+
"tags": ["speeches"],
75+
},
76+
]
77+
78+
# Include routers using a loop
79+
for routerConfig in routersConfig:
80+
app.include_router(
81+
router=routerConfig["router"],
82+
prefix=routerConfig["prefix"],
83+
tags=routerConfig["tags"],
84+
responses={
85+
404: {
86+
"description": "Not found",
87+
},
88+
},
89+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .core import router # noqa: F401

template_fastapi/routers/agents.py renamed to template_fastapi/routers/agents/azure_ai_foundry.py

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717

1818

1919
@router.post(
20-
"/agents/",
20+
"/",
2121
response_model=AgentResponse,
22-
tags=["agents"],
2322
operation_id="create_agent",
2423
)
2524
async def create_agent(request: AgentRequest) -> AgentResponse:
@@ -33,9 +32,8 @@ async def create_agent(request: AgentRequest) -> AgentResponse:
3332

3433

3534
@router.get(
36-
"/agents/{agent_id}",
35+
"/{agent_id}",
3736
response_model=AgentResponse,
38-
tags=["agents"],
3937
operation_id="get_agent",
4038
)
4139
async def get_agent(agent_id: str) -> AgentResponse:
@@ -49,9 +47,8 @@ async def get_agent(agent_id: str) -> AgentResponse:
4947

5048

5149
@router.get(
52-
"/agents/",
50+
"/",
5351
response_model=AgentListResponse,
54-
tags=["agents"],
5552
operation_id="list_agents",
5653
)
5754
async def list_agents(
@@ -67,8 +64,7 @@ async def list_agents(
6764

6865

6966
@router.delete(
70-
"/agents/{agent_id}",
71-
tags=["agents"],
67+
"/{agent_id}",
7268
operation_id="delete_agent",
7369
)
7470
async def delete_agent(agent_id: str) -> dict:
@@ -86,9 +82,8 @@ async def delete_agent(agent_id: str) -> dict:
8682

8783

8884
@router.post(
89-
"/agents/{agent_id}/chat",
85+
"/{agent_id}/chat",
9086
response_model=ChatResponse,
91-
tags=["agents"],
9287
operation_id="chat_with_agent",
9388
)
9489
async def chat_with_agent(agent_id: str, request: ChatRequest) -> ChatResponse:
@@ -102,9 +97,8 @@ async def chat_with_agent(agent_id: str, request: ChatRequest) -> ChatResponse:
10297

10398

10499
@router.post(
105-
"/agents/threads/",
100+
"/threads/",
106101
response_model=ThreadResponse,
107-
tags=["agents"],
108102
operation_id="create_thread",
109103
)
110104
async def create_thread(request: ThreadRequest) -> ThreadResponse:
@@ -118,9 +112,8 @@ async def create_thread(request: ThreadRequest) -> ThreadResponse:
118112

119113

120114
@router.get(
121-
"/agents/threads/{thread_id}",
115+
"/threads/{thread_id}",
122116
response_model=ThreadResponse,
123-
tags=["agents"],
124117
operation_id="get_thread",
125118
)
126119
async def get_thread(thread_id: str) -> ThreadResponse:
@@ -134,8 +127,7 @@ async def get_thread(thread_id: str) -> ThreadResponse:
134127

135128

136129
@router.delete(
137-
"/agents/threads/{thread_id}",
138-
tags=["agents"],
130+
"/threads/{thread_id}",
139131
operation_id="delete_thread",
140132
)
141133
async def delete_thread(thread_id: str) -> dict:
@@ -153,9 +145,8 @@ async def delete_thread(thread_id: str) -> dict:
153145

154146

155147
@router.get(
156-
"/agents/threads/",
148+
"/threads/",
157149
response_model=ThreadListResponse,
158-
tags=["agents"],
159150
operation_id="list_threads",
160151
)
161152
async def list_threads(
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from fastapi import APIRouter
2+
3+
from template_fastapi.routers.agents.azure_ai_foundry import router as azure_ai_foundry_router
4+
5+
router = APIRouter()
6+
7+
router.include_router(
8+
router=azure_ai_foundry_router,
9+
prefix="/azure-ai-foundry",
10+
responses={
11+
404: {
12+
"description": "Not found",
13+
},
14+
},
15+
)

template_fastapi/routers/chats.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111

1212
@router.get(
13-
"/chats/",
14-
tags=["chats"],
13+
"/",
1514
)
1615
async def get(request: Request):
1716
"""Get the chat page with configurable WebSocket URL."""

template_fastapi/routers/demos.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33

44
from fastapi import APIRouter, HTTPException
55

6-
from template_fastapi.opentelemetry import get_tracer
6+
from template_fastapi.opentelemetry import get_meter, get_tracer
77

88
tracer = get_tracer(__name__)
9+
meter = get_meter(__name__)
910
router = APIRouter()
1011

1112

1213
@tracer.start_as_current_span("flaky_exception")
13-
@router.get("/flaky/exception", tags=["flaky"], operation_id="flaky_exception")
14+
@router.get("/flaky/exception", operation_id="flaky_exception")
1415
async def flaky_exception():
1516
"""
1617
A flaky endpoint that always raises an exception.
@@ -21,7 +22,7 @@ async def flaky_exception():
2122
)
2223

2324

24-
@router.get("/flaky/{failure_rate}", tags=["flaky"], operation_id="flaky")
25+
@router.get("/flaky/{failure_rate}", operation_id="flaky")
2526
async def flaky(failure_rate: int):
2627
"""
2728
A flaky endpoint that simulates a failure based on the provided failure rate.
@@ -45,7 +46,7 @@ async def flaky(failure_rate: int):
4546
}
4647

4748

48-
@router.get("/heavy_sync/{sleep_ms}", tags=["heavy"], operation_id="heavy_sync_with_sleep")
49+
@router.get("/heavy_sync/{sleep_ms}", operation_id="heavy_sync_with_sleep")
4950
async def heavy_sync_with_sleep(sleep_ms: int):
5051
"""
5152
A heavy synchronous endpoint that sleeps for the specified number of milliseconds.
@@ -66,3 +67,20 @@ async def heavy_sync_with_sleep(sleep_ms: int):
6667
return {
6768
"message": f"Slept for {sleep_ms} milliseconds",
6869
}
70+
71+
72+
roll_counter = meter.create_counter(
73+
"dice.rolls",
74+
description="The number of rolls by roll value",
75+
)
76+
77+
78+
@router.get("/roll_dice", operation_id="roll_dice")
79+
async def roll_dice():
80+
"""
81+
Simulate rolling a dice and record the roll in the meter.
82+
"""
83+
with tracer.start_as_current_span("roll_dice"):
84+
roll = random.randint(1, 6)
85+
roll_counter.add(1, {"roll.value": str(roll)})
86+
return roll

template_fastapi/routers/files.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111

1212

1313
@router.get(
14-
"/files/",
14+
"/",
1515
response_model=list[FileModel],
16-
tags=["files"],
1716
operation_id="list_files",
1817
)
1918
async def list_files(prefix: str | None = None) -> list[FileModel]:
@@ -27,9 +26,8 @@ async def list_files(prefix: str | None = None) -> list[FileModel]:
2726

2827

2928
@router.post(
30-
"/files/upload",
29+
"/upload",
3130
response_model=FileModel,
32-
tags=["files"],
3331
operation_id="upload_file",
3432
)
3533
async def upload_file(file: UploadFile = File(...)) -> FileModel:
@@ -44,9 +42,8 @@ async def upload_file(file: UploadFile = File(...)) -> FileModel:
4442

4543

4644
@router.post(
47-
"/files/upload-multiple",
45+
"/upload-multiple",
4846
response_model=list[FileModel],
49-
tags=["files"],
5047
operation_id="upload_multiple_files",
5148
)
5249
async def upload_multiple_files(files: list[UploadFile] = File(...)) -> list[FileModel]:
@@ -65,8 +62,7 @@ async def upload_multiple_files(files: list[UploadFile] = File(...)) -> list[Fil
6562

6663

6764
@router.get(
68-
"/files/{file_name}",
69-
tags=["files"],
65+
"/{file_name}",
7066
operation_id="download_file",
7167
)
7268
async def download_file(file_name: str):
@@ -89,9 +85,8 @@ async def download_file(file_name: str):
8985

9086

9187
@router.get(
92-
"/files/{file_name}/info",
88+
"/{file_name}/info",
9389
response_model=FileModel,
94-
tags=["files"],
9590
operation_id="get_file_info",
9691
)
9792
async def get_file_info(file_name: str) -> FileModel:
@@ -107,8 +102,7 @@ async def get_file_info(file_name: str) -> FileModel:
107102

108103

109104
@router.delete(
110-
"/files/{file_name}",
111-
tags=["files"],
105+
"/{file_name}",
112106
operation_id="delete_file",
113107
)
114108
async def delete_file(file_name: str) -> dict:
@@ -125,8 +119,7 @@ async def delete_file(file_name: str) -> dict:
125119

126120

127121
@router.delete(
128-
"/files/",
129-
tags=["files"],
122+
"/",
130123
operation_id="delete_multiple_files",
131124
)
132125
async def delete_multiple_files(file_names: list[str]) -> dict:

0 commit comments

Comments
 (0)