from __future__ import annotations from typing import Any import pandas as pd import requests from config.settings import MLB_TEAMS_URL def fetch_mlb_teams() -> pd.DataFrame: response = requests.get(MLB_TEAMS_URL, params={"sportId": 1}, timeout=30) response.raise_for_status() payload = response.json() rows: list[dict[str, Any]] = [] for team in payload.get("teams", []): rows.append( { "team_id": team.get("id"), "name": team.get("name"), "team_code": team.get("teamCode"), "file_code": team.get("fileCode"), "abbreviation": team.get("abbreviation"), "location_name": team.get("locationName"), "club_name": team.get("clubName"), "league_name": (team.get("league") or {}).get("name"), "division_name": (team.get("division") or {}).get("name"), "venue_name": (team.get("venue") or {}).get("name"), } ) return pd.DataFrame(rows)