Skip to content

Commit

Permalink
Adds /{player_id}/game-log/{season}/{game_type} endpoints (#36)
Browse files Browse the repository at this point in the history
* Adds /{player_id}/game-log/{season}/{game_type} endpoints

* Player/Franchise/Game IDs should all be referenced as strings.  Integers are not a suitable type for these.
  • Loading branch information
coreyjs authored Feb 13, 2024
1 parent 82b9580 commit 8a63958
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ client = NHLClient(verbose=True) # a tad more logging such as the URL being call

```python

client.club_stats_season(team_abbr="BUF") # kinda weird endpoint.
client.stats.club_stats_season(team_abbr="BUF") # kinda weird endpoint.

client.player_career_stats(player_id=8478402)
client.stats.player_career_stats(player_id="8478402")

# Team Summary Stats.
# These have lots of available parameters. You can also tap into the apache cayenne expressions to build custom
Expand Down
2 changes: 1 addition & 1 deletion nhlpy/_version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Should this be driven by the main pyproject.toml file? yes, is it super convoluted? yes, can it wait? sure

__version__ = "2.2.5"
__version__ = "2.2.6"
16 changes: 13 additions & 3 deletions nhlpy/api/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def club_stats_season(self, team_abbr: str) -> dict:
"""
return self.client.get(resource=f"club-stats-season/{team_abbr}").json()

def player_career_stats(self, player_id: int) -> dict:
def player_career_stats(self, player_id: str) -> dict:
"""
This returns the career stats for a player as well as player information.
Expand All @@ -29,6 +29,16 @@ def player_career_stats(self, player_id: int) -> dict:
"""
return self.client.get(resource=f"player/{player_id}/landing").json()

def player_game_log(self, player_id: str, season_id: str, game_type: int) -> List[dict]:
"""
Returns the game log, for the given player, for the given season and game type.
:param game_type: 1 is for preseason, 2 is for regular season, 3 is for playoffs.
:param season_id: Season format of "20222023", "20232024", etc.
:param player_id:
:return:
"""
return self.client.get(resource=f"player/{player_id}/game-log/{season_id}/{game_type}").json()["gameLog"]

def team_summary(
self,
start_season: str,
Expand Down Expand Up @@ -95,7 +105,7 @@ def skater_stats_summary(
self,
start_season: str,
end_season: str,
franchise_id: int = None,
franchise_id: str = None,
game_type_id: int = 2,
aggregate: bool = False,
sort_expr: List[dict] = None,
Expand All @@ -109,7 +119,7 @@ def skater_stats_summary(
c.stats.skater_stats_summary(franchise_id=10, start_season="20232024", end_season="20232024")
:param start_season: Season id, in format 20202021, 20212022, etc, that will be the start of the range.
:param end_season: Season id for the end range.
:param franchise_id: The ID of the franchise. Not to be confused with team_id found on other endpoints.
:param franchise_id: String, The ID of the franchise. Not to be confused with team_id found on other endpoints.
This seems to be specific to the /stats apis.
:param game_type_id: 2 is for regular season, 3 is for playoffs. I think 1 is for preseason.
:param aggregate: If doing multiple years, you can choose to aggreate the date per player, or have separate
Expand Down
1 change: 1 addition & 0 deletions nhlpy/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ class ClientConfig:
def __init__(self, verbose: bool = False) -> None:
self.verbose = verbose
self.api_web_base_url = "https://api-web.nhle.com"
self.api_base_url = "https://api.nhle.com"
self.api_web_api_ver = "/v1/"
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "nhl-api-py"
version = "2.2.5"
version = "2.2.6"
description = "NHL API. For standings, team stats, outcomes, player information. Contains each individual API endpoint as well as convience methods for easy data loading in Pandas or any ML applications."
authors = ["Corey Schaf <[email protected]>"]
readme = "README.md"
Expand Down
7 changes: 7 additions & 0 deletions tests/test_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ def test_get_schedule_by_team_by_week(h_m, nhl_client):
assert h_m.call_args[1]["url"] == "https://api-web.nhle.com/v1/club-schedule/BUF/week/now"


@mock.patch("httpx.get")
def test_get_schedule_by_team_by_week_with_date(h_m, nhl_client):
nhl_client.schedule.get_schedule_by_team_by_week(team_abbr="BUF", date="2024-02-10")
h_m.assert_called_once()
assert h_m.call_args[1]["url"] == "https://api-web.nhle.com/v1/club-schedule/BUF/week/2024-02-10"


@mock.patch("httpx.get")
def test_get_season_schedule(h_m, nhl_client):
nhl_client.schedule.get_season_schedule(team_abbr="BUF", season="20202021")
Expand Down
14 changes: 14 additions & 0 deletions tests/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,17 @@ def test_skater_stats_summary_franchise(h_m, nhl_client):
"%3A%20%22playerId%22%2C%20%22direction%22%3A%20%22ASC%22%7D%5D",
"cayenneExp": "franchiseId=19 and gameTypeId=2 and seasonId<=20232024 and seasonId>=20232024",
}


@mock.patch("httpx.get")
def test_player_game_log(h_m, nhl_client):
nhl_client.stats.player_game_log(player_id="8481528", season_id="20232024", game_type=2)
h_m.assert_called_once()
assert h_m.call_args[1]["url"] == "https://api-web.nhle.com/v1/player/8481528/game-log/20232024/2"


@mock.patch("httpx.get")
def test_player_game_log_playoffs(h_m, nhl_client):
nhl_client.stats.player_game_log(player_id="8481528", season_id="20232024", game_type=3)
h_m.assert_called_once()
assert h_m.call_args[1]["url"] == "https://api-web.nhle.com/v1/player/8481528/game-log/20232024/3"

0 comments on commit 8a63958

Please sign in to comment.