Skip to content

Commit 1f95c4e

Browse files
authored
Create api_router.py
1 parent 7b1c2c2 commit 1f95c4e

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

api_router.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from fastapi import APIRouter, HTTPException
2+
from src.pricing-service.pricing_engine import PricingEngine
3+
from src.common.utils import get_logger
4+
5+
router = APIRouter()
6+
logger = get_logger("PricingAPI")
7+
8+
# In-memory surge cache for fast lookup
9+
SURGE_CACHE = {} # { "A1": 1.8, "B2": 1.2, ... }
10+
11+
def update_surge(zone_id: str, multiplier: float):
12+
"""
13+
Called by the consumer after computing new zone surge.
14+
"""
15+
SURGE_CACHE[zone_id] = multiplier
16+
logger.info(f"[API] Updated surge multiplier for zone {zone_id} = {multiplier}")
17+
18+
19+
@router.get("/surge/{zone_id}")
20+
async def get_surge(zone_id: str):
21+
"""
22+
Fetch latest surge multiplier for a given zone.
23+
"""
24+
if zone_id not in SURGE_CACHE:
25+
raise HTTPException(
26+
status_code=404,
27+
detail=f"No surge information available for zone {zone_id}"
28+
)
29+
30+
return {
31+
"zone_id": zone_id,
32+
"surge_multiplier": SURGE_CACHE[zone_id]
33+
}
34+
35+
36+
@router.get("/surge/all")
37+
async def get_all_surge():
38+
"""
39+
Get all current surge multipliers for every zone.
40+
"""
41+
return SURGE_CACHE

0 commit comments

Comments
 (0)