|
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 |
5 | 2 | from src.common.utils import get_logger |
| 3 | +from src.common.models import DriverLocationEvent |
6 | 4 |
|
| 5 | +logger = get_logger("DriverLocationAPI") |
7 | 6 | router = APIRouter() |
8 | | -logger = get_logger("DispatchAPI") |
9 | | - |
10 | 7 |
|
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 |
14 | 10 |
|
15 | 11 |
|
16 | 12 | @router.get("/drivers") |
17 | | -async def get_available_drivers(): |
| 13 | +async def get_all_drivers(): |
18 | 14 | """ |
19 | | - Returns the list of currently available drivers. |
| 15 | + Returns all active drivers and their current coordinates. |
20 | 16 | """ |
21 | 17 | if DRIVER_STORE is None: |
22 | | - raise HTTPException(status_code=500, detail="Driver store not initialized.") |
| 18 | + raise HTTPException(500, "Driver store not initialized.") |
23 | 19 |
|
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 | + } |
26 | 25 |
|
27 | 26 |
|
28 | | -@router.get("/matches") |
29 | | -async def get_match_results(): |
| 27 | +@router.get("/drivers/{driver_id}") |
| 28 | +async def get_driver(driver_id: str): |
30 | 29 | """ |
31 | | - Returns a list of recent match results. |
| 30 | + Returns location data for a specific driver. |
32 | 31 | """ |
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.") |
35 | 34 |
|
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.") |
37 | 38 |
|
| 39 | + return driver |
38 | 40 |
|
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(): |
41 | 44 | """ |
42 | | - Fetch a specific match result by list index. |
| 45 | + Quick counter to see how many drivers are active. |
43 | 46 | """ |
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.") |
46 | 49 |
|
47 | | - return MATCH_RESULTS_STORE[index] |
| 50 | + return {"active_drivers": DRIVER_STORE.count()} |
48 | 51 |
|
49 | 52 |
|
50 | 53 | @router.get("/health") |
51 | 54 | async def health_check(): |
52 | 55 | """ |
53 | | - Service status check. |
| 56 | + Returns service health. |
54 | 57 | """ |
55 | | - return {"status": "OK", "service": "dispatch-service"} |
| 58 | + return { |
| 59 | + "status": "OK", |
| 60 | + "service": "driver-location-service" |
| 61 | + } |
0 commit comments