Skip to content

Commit 79f6942

Browse files
author
ks6088ts
committed
add a flaky API
1 parent ed6f269 commit 79f6942

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

template_fastapi/app.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
ref. https://github.com/tadata-org/fastapi_mcp/blob/v0.3.4/examples/shared/apps/items.py
44
"""
55

6+
import random
67
from os import getenv
78

89
from azure.monitor.opentelemetry import configure_azure_monitor
@@ -136,3 +137,28 @@ async def search_items(
136137
]
137138
for item in sample_items:
138139
items_db[item.id] = item
140+
141+
142+
# Add flaky API which receives percentage of failure
143+
@app.get("/flaky/{failure_rate}", tags=["flaky"], operation_id="flaky")
144+
async def flaky(failure_rate: int):
145+
"""
146+
A flaky endpoint that simulates a failure based on the provided failure rate.
147+
148+
The failure rate is a percentage (0-100) that determines the likelihood of failure.
149+
"""
150+
if not (0 <= failure_rate <= 100):
151+
raise HTTPException(
152+
status_code=400,
153+
detail="Failure rate must be between 0 and 100",
154+
)
155+
156+
if random.randint(0, 100) < failure_rate:
157+
raise HTTPException(
158+
status_code=500,
159+
detail="Simulated failure",
160+
)
161+
162+
return {
163+
"message": "Request succeeded",
164+
}

0 commit comments

Comments
 (0)