30 lines
755 B
Python
30 lines
755 B
Python
import asyncio
|
|
import random
|
|
from fastapi import APIRouter, status
|
|
from starlette.responses import JSONResponse
|
|
|
|
from app.schema import Response
|
|
|
|
router = APIRouter()
|
|
|
|
@router.get('/demo', status_code=status.HTTP_200_OK, response_model=Response)
|
|
async def demo() -> JSONResponse:
|
|
"""
|
|
Demo API endpoint that randomly sleeps for 0-500ms before responding.
|
|
"""
|
|
# Random sleep between 0 and 500ms
|
|
sleep_time = random.uniform(0, 0.5)
|
|
await asyncio.sleep(sleep_time)
|
|
|
|
return JSONResponse(
|
|
status_code=status.HTTP_200_OK,
|
|
content={
|
|
'code': status.HTTP_200_OK,
|
|
'msg': 'ok',
|
|
'payload': {
|
|
'sleep_time_ms': round(sleep_time * 1000, 2)
|
|
}
|
|
}
|
|
)
|
|
|