add demo data for Swagger

This commit is contained in:
Meghdad committed 2026-07-23 07:40:44 +03:30
1 parent f6714f07cd
commit a353b80a15
4 files changed
+132 -2

No files matched your search

+29 -2
View File
@@ -4,12 +4,16 @@ from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
from typing import Annotated, cast
from fastapi import Depends, FastAPI, Header, HTTPException, Request, Response, status
from fastapi import Body, Depends, FastAPI, Header, HTTPException, Request, Response, status
from fastapi.responses import JSONResponse
from prometheus_client import CONTENT_TYPE_LATEST, generate_latest
from redis.asyncio import Redis
from starlette.middleware.base import RequestResponseEndpoint
from warehouse_allocation.api_examples import (
DEMO_ALLOCATION_REQUEST,
DEMO_ALLOCATION_RESPONSE,
)
from warehouse_allocation.application import AllocationService
from warehouse_allocation.config import Settings, get_settings
from warehouse_allocation.domain.exceptions import (
@@ -122,13 +126,36 @@ def create_app(
"/v1/allocations",
response_model=AllocationResponse,
responses={
200: {
"description": "Warehouse selected successfully.",
"content": {
"application/json": {
"example": DEMO_ALLOCATION_RESPONSE,
}
},
},
400: {"model": ErrorResponse},
409: {"model": ErrorResponse},
503: {"model": ErrorResponse},
},
)
async def allocate(
payload: AllocationRequest,
payload: Annotated[
AllocationRequest,
Body(
openapi_examples={
"demoQuote": {
"summary": "Runnable demo quote",
"description": (
"Uses inventory pre-seeded in the public demo. "
"It does not reserve or decrement stock, "
"so it is safe to run repeatedly."
),
"value": DEMO_ALLOCATION_REQUEST,
}
}
),
],
service: Annotated[AllocationService, Depends(get_service)],
) -> AllocationResponse:
started = time.perf_counter()
+76
View File
@@ -0,0 +1,76 @@
from typing import Final
DEMO_SKU: Final = "DEMO-SKU-001"
DEMO_WAREHOUSE_IDS: Final = ("demo-tehran", "demo-karaj")
DEMO_ALLOCATION_REQUEST: Final[dict[str, object]] = {
"requestId": "swagger-demo-quote",
"customerLocation": {
"latitude": 35.7219,
"longitude": 51.3347,
},
"items": [
{
"sku": DEMO_SKU,
"quantity": 1,
}
],
"transportationCosts": [
{
"warehouseId": "demo-tehran",
"fixedCost": 180000,
"distanceKm": 12,
"costPerKm": 3500,
"baseDeliveryMinutes": 55,
"surcharge": 0,
},
{
"warehouseId": "demo-karaj",
"fixedCost": 60000,
"distanceKm": 48,
"costPerKm": 2500,
"baseDeliveryMinutes": 105,
"surcharge": 0,
},
],
"logisticsConstraints": [
{
"warehouseId": "demo-tehran",
"trafficMultiplier": 1.25,
"fleetPriority": 1.1,
"handlingMinutes": 8,
"available": True,
},
{
"warehouseId": "demo-karaj",
"trafficMultiplier": 1.05,
"fleetPriority": 1,
"handlingMinutes": 12,
"available": True,
},
],
"weights": {
"cost": 0.7,
"deliveryTime": 0.3,
},
"promisedDeliveryMinutes": 180,
# A Swagger demo should remain repeatable and must not consume shared stock.
"reserveInventory": False,
}
DEMO_ALLOCATION_RESPONSE: Final[dict[str, object]] = {
"requestId": "swagger-demo-quote",
"warehouseId": "demo-karaj",
"estimatedDeliveryTime": 122,
"totalCost": 181350,
"routeOptimizationScore": 70,
"inventoryReserved": False,
"scoreBreakdown": {
"transportCost": 181350,
"normalizedCost": 0,
"normalizedDeliveryTime": 1,
"slaPenalty": 0,
"feasibleWarehouseCount": 2,
},
"calculatedAt": "2026-07-23T04:00:00Z",
}