Welcome to the interactive lecture session for INFO 2602 Lab week 1.

In this codelab you will reinforce the content learned this week in FastAPI. Namely:

In today's scenario, we'd see how we can use the knowledge we gained this week to build out a simple application. We'd be building a sample shuttle service application that can be used by campus.

We would be using the same workspace from the Week 1 Lab as a base for this session. We'd replace the contents of data.json with the following

[
    {
        "id": 1,
        "name": "UWI to Sal Hall",
        "status": "IN_TRANSIT",
        "speed_kmh": 32.5,
        "current_driver": {
            "id": 12,
            "name": "A. Mohammed"
        },
        "vehicle_details": {
            "id": "1",
            "reg": "TDY-1234",
            "capacity": 25,
            "has_aircon": true,
            "has_wifi": false
        },
        "history": [
            {
                "id": 1,
                "shuttle_id": 1,
                "last_seen": "2026-01-23T13:45:12Z",
                "latitude": 10.637210,
                "longitude": -61.409832
            },
            {
                "id": 2,
                "shuttle_id": 1,
                "last_seen": "2026-01-23T13:50:12Z",
                "latitude": 10.639021,
                "longitude": -61.405214
            },
            {
                "id": 3,
                "shuttle_id": 1,
                "last_seen": "2026-01-23T13:55:12Z",
                "latitude": 10.641114,
                "longitude": -61.401002
            }
        ]
    },
    {
        "id": 2,
        "name": "UWI to Mt Hope",
        "status": "STOPPED",
        "speed_kmh": 0,
        "current_driver": {
            "id": 18,
            "name": "R. Singh"
        },
        "vehicle_details": {
            "id": "2",
            "reg": "PCZ-4421",
            "capacity": 30,
            "has_aircon": true,
            "has_wifi": true
        },
        "history": [
            {
                "id": 4,
                "shuttle_id": 2,
                "last_seen": "2026-01-23T13:40:00Z",
                "latitude": 10.634882,
                "longitude": -61.418993
            },
            {
                "id": 5,
                "shuttle_id": 2,
                "last_seen": "2026-01-23T13:45:00Z",
                "latitude": 10.632441,
                "longitude": -61.421774
            },
            {
                "id": 6,
                "shuttle_id": 2,
                "last_seen": "2026-01-23T13:50:00Z",
                "latitude": 10.631118,
                "longitude": -61.424301
            }
        ]
    },
    {
        "id": 3,
        "name": "UWI to Curepe",
        "status": "OUT_OF_SERVICE",
        "speed_kmh": 0,
        "current_driver": null,
        "vehicle_details": {
            "id": "3",
            "reg": "HBU-8890",
            "capacity": 20,
            "has_aircon": false,
            "has_wifi": false
        },
        "history": [
            {
                "id": 7,
                "shuttle_id": 3,
                "last_seen": "2026-01-23T12:30:10Z",
                "latitude": 10.631218,
                "longitude": -61.415992
            }
        ]
    },
    {
        "id": 4,
        "name": "UWI to Tunapuna",
        "status": "IN_TRANSIT",
        "speed_kmh": 41.2,
        "current_driver": {
            "id": 25,
            "name": "K. Lewis"
        },
        "vehicle_details": {
            "id": "4",
            "reg": "PDD-7721",
            "capacity": 28,
            "has_aircon": true,
            "has_wifi": true
        },
        "history": [
            {
                "id": 8,
                "shuttle_id": 4,
                "last_seen": "2026-01-23T13:42:58Z",
                "latitude": 10.642773,
                "longitude": -61.398441
            },
            {
                "id": 9,
                "shuttle_id": 4,
                "last_seen": "2026-01-23T13:47:58Z",
                "latitude": 10.646109,
                "longitude": -61.394118
            },
            {
                "id": 10,
                "shuttle_id": 4,
                "last_seen": "2026-01-23T13:52:58Z",
                "latitude": 10.649221,
                "longitude": -61.390002
            }
        ]
    },
    {
        "id": 5,
        "name": "UWI to Arima",
        "status": "IN_TRANSIT",
        "speed_kmh": 55.8,
        "current_driver": {
            "id": 31,
            "name": "S. Ramdial"
        },
        "vehicle_details": {
            "id": "5",
            "reg": "TDT-9087",
            "capacity": 35,
            "has_aircon": true,
            "has_wifi": false
        },
        "history": [
            {
                "id": 11,
                "shuttle_id": 5,
                "last_seen": "2026-01-23T13:35:22Z",
                "latitude": 10.650332,
                "longitude": -61.382114
            },
            {
                "id": 12,
                "shuttle_id": 5,
                "last_seen": "2026-01-23T13:40:22Z",
                "latitude": 10.657441,
                "longitude": -61.374991
            },
            {
                "id": 13,
                "shuttle_id": 5,
                "last_seen": "2026-01-23T13:45:22Z",
                "latitude": 10.664882,
                "longitude": -61.368771
            }
        ]
    }
]

Who would use an app like this?

If an app like this existed, what are some of the features it should have?

What would the user interface look like?

from fastapi import FastAPI, HTTPException
import json
from datetime import datetime


app = FastAPI()

global data

with open('./data.json') as f:
    data = json.load(f)


@app.get('/')
def hello_world():
    return 'Hello, World!'

@app.get('/shuttles')
async def get_shuttle_overview():
    shuttle_list = []

    for shuttle in data:
        curr_data = {
            "id":shuttle['id'],
            "name":shuttle['name'],
            "reg_no":shuttle['vehicle_details']['reg'],
            "active":shuttle['status']
        }
        shuttle_list.append(curr_data)
    return shuttle_list

@app.get('/shuttle/{id}')
async def get_shuttle_detail(id):
    for shuttle in data:
        if int(id) == shuttle['id']:
            return {
                "name": shuttle['name'],
                "status":shuttle['status'],
                "has_wifi":shuttle['vehicle_details']['has_wifi'],
                "has_aircon":shuttle['vehicle_details']['has_aircon'],
                "capacity":shuttle['vehicle_details']['capacity'],
                "driver_name":shuttle['current_driver']['name']
            }
    raise HTTPException(status_code=404, detail="Shuttle not found")



@app.get('/shuttle/{id:int}/location')
async def get_shuttle_last_seen(id):
    for shuttle in data:
        if id == shuttle['id']:
            bus_history = shuttle['history']
            if not bus_history:
                raise HTTPException(status_code=404, detail="No History found for shuttle")
            item = bus_history[0]
            for history_item in bus_history:
                item_dt = datetime.fromisoformat(item['last_seen'].replace("Z", "+00:00"))
                history_item_dt = datetime.fromisoformat(history_item['last_seen'].replace("Z", "+00:00"))

                if history_item_dt>item_dt:
                    item = history_item
            return item

    raise HTTPException(status_code=404, detail="Shuttle not found")