Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions docs/public-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,19 @@ get_attendance(
get_draft(year_id: int, **params)
get_awards(award_id: str, **params)
get_homerun_derby(game_id, **params)
get_team_stats(team_id: int, stats: list, groups: list, **params)
get_players_stats_for_game(person_id: int, game_id: int, **params)
get_player_stats(person_id: int, stats: list, groups: list, **params)
get_stats(stats: list, groups: list, **params)
get_persons(person_ids: str | list[int], **params)
get_scheduled_games_by_date(
date: str = None,
start_date: str = None,
end_date: str = None,
sport_id: int = 1,
**params,
)
get_gamepace(season: str, sport_id=1, **params)
get_team_id(team_name: str, search_key: str = 'name', **params)
get_people_id(
fullname: str,
Expand Down Expand Up @@ -357,9 +370,30 @@ introduced by the async port.
way its sibling game helpers do; missing linescore data falls through to an
implicit `None`.

Every other `Mlb` endpoint method not listed above is not yet supported on
`AsyncMlb`; calling it there raises `AttributeError`. See issue #305 for the
tracked expansion plan.
The four stat methods return the same nested `dict` their sync counterparts
do, keyed by stat group and then by stat type — `{'hitting': {'season': Stat}}`
— and return `{}` on a 400–499 response, on a body with no `stats`, and on a
`stats` entry carrying no splits. Note that an unrecognized value in `stats` or
`groups` is not rejected; it produces the same empty `{}`. Valid values are
listed at `https://statsapi.mlb.com/api/v1/statTypes` and
`https://statsapi.mlb.com/api/v1/statGroups`.

`AsyncMlb` now covers every endpoint method `Mlb` exposes. The only public
name that differs is lifecycle: `Mlb.close()` is spelled `AsyncMlb.aclose()`.

`get_scheduled_games_by_date` inherits the same documented quirk as
`Mlb.get_scheduled_games_by_date`: it is annotated `list[ScheduleGames]` but
returns `None` when no `date`, `start_date`/`end_date` pair, or `gamePks` was
given to select with. This is preserved for parity, not introduced by the
async port.

`get_gamepace` sends the same request on both clients but builds it
differently. `Mlb` embeds the season in the endpoint string
(`gamePace?season=2021`) and relies on Requests merging that query with the
rest of the parameters. HTTPX replaces a URL's existing query rather than
merging into it, so `AsyncMlb` passes the season as an ordinary parameter.
Callers see no difference; this matters only if you are reading the two
implementations side by side.

## Low-level adapter

Expand Down
17 changes: 17 additions & 0 deletions mlbstatsapi/_parsers/gamepace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from mlbstatsapi.models.gamepace import GamePace


def parse_gamepace(data: dict) -> GamePace | None:
"""Parse a GamePace from an MLB /gamePace response body.

The endpoint keys its metrics by whichever of ``teams``, ``leagues`` or
``sports`` the caller's ``orgType`` selected, so a body carrying none of
them has nothing to build from.
"""
if not data:
return None

if not (data.get("teams") or data.get("leagues") or data.get("sports")):
return None

return GamePace(**data)
18 changes: 17 additions & 1 deletion mlbstatsapi/_parsers/schedules.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from mlbstatsapi.models.schedules import Schedule
from mlbstatsapi.models.schedules import Schedule, ScheduleGames


def parse_schedule(data: dict) -> Schedule | None:
Expand All @@ -7,3 +7,19 @@ def parse_schedule(data: dict) -> Schedule | None:
return None

return Schedule(**data)


def parse_scheduled_games(data: dict) -> list[ScheduleGames]:
"""Parse the games out of an MLB /schedule response body, flattened.

The response nests games under one entry per date; this returns them as a
single list, dropping the date grouping.
"""
if not data or not data.get("dates"):
return []

return [
ScheduleGames(**game)
for date in data["dates"]
for game in date["games"]
]
16 changes: 16 additions & 0 deletions mlbstatsapi/_parsers/stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from mlbstatsapi import mlb_module


def parse_split_stats(data: dict) -> dict:
"""Parse split stat data from an MLB stats response body.

Shared by every stats endpoint -- ``/stats``, ``/people/{id}/stats``,
``/teams/{id}/stats``, and ``/people/{id}/stats/game/{game_id}`` -- all of
which return the same ``stats`` envelope.

Returns a dict keyed by stat group, then by stat type, or ``{}`` when the
response carries no stats.
"""
if not data or not data.get("stats"):
return {}
return mlb_module.create_split_data(data["stats"])
Loading
Loading