Skip to content

Commit 133b018

Browse files
authored
Update api_router.py
1 parent 4387e80 commit 133b018

File tree

1 file changed

+34
-28
lines changed

1 file changed

+34
-28
lines changed

api_router.py

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,61 @@
1-
from fastapi import APIRouter, HTTPException
2-
from typing import List, Dict
3-
4-
from src.common.models import DriverLocationEvent, MatchResultEvent
1+
from fastapi import APIRouter, HTTPException
52
from src.common.utils import get_logger
3+
from src.common.models import DriverLocationEvent
64

5+
logger = get_logger("DriverLocationAPI")
76
router = APIRouter()
8-
logger = get_logger("DispatchAPI")
9-
107

11-
# These will be injected by main.py
12-
DRIVER_STORE = None # Callable → List[DriverLocationEvent]
13-
MATCH_RESULTS_STORE = None # List[MatchResultEvent]
8+
# These will be set by main.py
9+
DRIVER_STORE = None
1410

1511

1612
@router.get("/drivers")
17-
async def get_available_drivers():
13+
async def get_all_drivers():
1814
"""
19-
Returns the list of currently available drivers.
15+
Returns all active drivers and their current coordinates.
2016
"""
2117
if DRIVER_STORE is None:
22-
raise HTTPException(status_code=500, detail="Driver store not initialized.")
18+
raise HTTPException(500, "Driver store not initialized.")
2319

24-
drivers = DRIVER_STORE()
25-
return {"count": len(drivers), "drivers": drivers}
20+
drivers = DRIVER_STORE.get_all_drivers()
21+
return {
22+
"count": len(drivers),
23+
"drivers": drivers
24+
}
2625

2726

28-
@router.get("/matches")
29-
async def get_match_results():
27+
@router.get("/drivers/{driver_id}")
28+
async def get_driver(driver_id: str):
3029
"""
31-
Returns a list of recent match results.
30+
Returns location data for a specific driver.
3231
"""
33-
if MATCH_RESULTS_STORE is None:
34-
raise HTTPException(status_code=500, detail="Match results store not initialized.")
32+
if DRIVER_STORE is None:
33+
raise HTTPException(500, "Driver store not initialized.")
3534

36-
return {"count": len(MATCH_RESULTS_STORE), "matches": MATCH_RESULTS_STORE}
35+
driver = DRIVER_STORE.get_driver(driver_id)
36+
if not driver:
37+
raise HTTPException(404, f"Driver {driver_id} not found.")
3738

39+
return driver
3840

39-
@router.get("/matches/{index}")
40-
async def get_match_by_index(index: int):
41+
42+
@router.get("/count")
43+
async def get_driver_count():
4144
"""
42-
Fetch a specific match result by list index.
45+
Quick counter to see how many drivers are active.
4346
"""
44-
if MATCH_RESULTS_STORE is None or index < 0 or index >= len(MATCH_RESULTS_STORE):
45-
raise HTTPException(status_code=404, detail="Match not found.")
47+
if DRIVER_STORE is None:
48+
raise HTTPException(500, "Driver store not initialized.")
4649

47-
return MATCH_RESULTS_STORE[index]
50+
return {"active_drivers": DRIVER_STORE.count()}
4851

4952

5053
@router.get("/health")
5154
async def health_check():
5255
"""
53-
Service status check.
56+
Returns service health.
5457
"""
55-
return {"status": "OK", "service": "dispatch-service"}
58+
return {
59+
"status": "OK",
60+
"service": "driver-location-service"
61+
}

0 commit comments

Comments
 (0)