Skip to content

Commit 6786bb9

Browse files
committed
fix: Tests fixed for proper renaming of methods.
Endpoint enum integration into all API calls
1 parent 69f2fa2 commit 6786bb9

18 files changed

+214
-157
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,7 @@ archive.zip
2424
*~
2525
.*.sw?
2626

27-
*/.DS_Store
27+
*/.DS_Store
28+
29+
CLAUDE.md
30+
v3.md

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,26 @@ client = NHLClient(verbose=True)
5151
# OR Other available configurations:
5252
client = NHLClient(verbose={bool}, timeout={int}, ssl_verify={bool}, follow_redirects={bool})
5353
```
54+
55+
## Endpoint Modules
56+
57+
This project is organized into several sub modules, each representing a different endpoint of the NHL API.
58+
They are group by function to make the library easier to navigate and use. The main modules are:
59+
60+
- `stats`: Contains endpoints related to player and team statistics. This will have your basic stats, summary stats and
61+
more advanced stats using the new query builder. Which allows for more complex queries to be built up programmatically.
62+
- `schedule`: Contains endpoints related to the NHL schedule, including game dates, weekly schedules, and team schedules.
63+
- `standings`: Contains endpoints related to NHL standings, including current standings and historical standings.
64+
- `teams`: Contains endpoints related to NHL teams, including team information and rosters. You can find team_id(s) here along with franchise_id(s) needed for some of the stats queries.
65+
- `game_center`: Contains endpoints related to game center data, including box scores, play-by-play data, and game summaries.
66+
- `misc`: Contains miscellaneous endpoints that don't fit into the other categories, such as glossary terms, configuration data, and country information.
67+
68+
69+
- `helpers`: Contains helper functions and utilities for working with the NHL API, such as getting game IDs by season or calculating player statistics.
70+
These are experimental and often times make many requests, can return DFs or do calculations. Stuff I find myself doing over and over I tend to move into helpers for convenience. They are often
71+
cross domain, involve many sub requests, may integrate more machine learning techniques, or just make it easier to get the data you want. These will have built in sleeping to avoid hitting the API too hard, but you can override this by setting the `sleep` parameter to `False` in the function call.
72+
73+
5474
---
5575
## Stats with QueryBuilder
5676

nhlpy/api/game_center.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from typing import Optional, List
2-
from nhlpy.http_client import HttpClient
2+
from nhlpy.http_client import HttpClient, Endpoint
33

44

55
class GameCenter:
@@ -18,7 +18,7 @@ def boxscore(self, game_id: str) -> dict:
1818
Returns:
1919
dict: Game boxscore data
2020
"""
21-
return self.client.get(resource=f"gamecenter/{game_id}/boxscore").json()
21+
return self.client.get(endpoint=Endpoint.API_WEB_V1, resource=f"gamecenter/{game_id}/boxscore").json()
2222

2323
def play_by_play(self, game_id: str) -> dict:
2424
"""Get play-by-play data for a specific NHL game. GameIds can be retrieved from the schedule endpoint.
@@ -29,7 +29,7 @@ def play_by_play(self, game_id: str) -> dict:
2929
Returns:
3030
dict: Play-by-play game data
3131
"""
32-
return self.client.get(resource=f"gamecenter/{game_id}/play-by-play").json()
32+
return self.client.get(endpoint=Endpoint.API_WEB_V1, resource=f"gamecenter/{game_id}/play-by-play").json()
3333

3434
def landing(self, game_id: str) -> dict:
3535
"""Get detailed match up information for a specific NHL game. GameIds can be retrieved
@@ -41,19 +41,19 @@ def landing(self, game_id: str) -> dict:
4141
Returns:
4242
dict: Detailed game matchup data
4343
"""
44-
return self.client.get(resource=f"gamecenter/{game_id}/landing").json()
44+
return self.client.get(endpoint=Endpoint.API_WEB_V1, resource=f"gamecenter/{game_id}/landing").json()
4545

4646
def daily_scores(self, date: Optional[str] = None) -> dict:
4747
"""Get scores for NHL games on a specific date or current day.
4848
4949
Args:
50-
date (str, optional): Date to check scores in YYYY-MM-DD format.
50+
date (str, optional): Date to check scores in YYYY-MM-DD format.
5151
If not provided, returns current day's scores.
5252
5353
Returns:
5454
dict: Game scores and status information
5555
"""
56-
return self.client.get(resource=f"score/{date if date else 'now'}").json()
56+
return self.client.get(endpoint=Endpoint.API_WEB_V1, resource=f"score/{date if date else 'now'}").json()
5757

5858
def shift_chart_data(self, game_id: str, excludes: List[str] = None) -> dict:
5959
"""Gets shift chart data for a specific game.
@@ -66,13 +66,14 @@ def shift_chart_data(self, game_id: str, excludes: List[str] = None) -> dict:
6666
Returns:
6767
Dict containing the shift chart data.
6868
"""
69-
if not excludes:
69+
if excludes is None:
7070
excludes = ["eventDetails"]
7171

72-
base_url: str = "https://api.nhle.com/stats/rest/en/shiftcharts"
7372
exclude_p: str = ",".join(excludes)
7473
expr_p: str = f"gameId={game_id} and ((duration != '00:00' and typeCode = 517) or typeCode != 517 )"
75-
return self.client.get_by_url(full_resource=f"{base_url}?cayenneExp={expr_p}&exclude={exclude_p}").json()
74+
return self.client.get(
75+
endpoint=Endpoint.API_STATS, resource=f"en/shiftcharts?cayenneExp={expr_p}&exclude={exclude_p}"
76+
).json()
7677

7778
def right_rail(self, game_id: str) -> dict:
7879
"""Gets game stats and season series information for a specific game.
@@ -84,7 +85,7 @@ def right_rail(self, game_id: str) -> dict:
8485
Returns:
8586
Dict containing game stats and season series data.
8687
"""
87-
return self.client.get(resource=f"gamecenter/{game_id}/right-rail").json()
88+
return self.client.get(endpoint=Endpoint.API_WEB_V1, resource=f"gamecenter/{game_id}/right-rail").json()
8889

8990
def game_story(self, game_id: str) -> dict:
9091
"""Gets game story information for a specific game.
@@ -96,4 +97,4 @@ def game_story(self, game_id: str) -> dict:
9697
Returns:
9798
Dict containing game story data.
9899
"""
99-
return self.client.get(resource=f"wsc/game-story/{game_id}").json()
100+
return self.client.get(endpoint=Endpoint.API_WEB_V1, resource=f"wsc/game-story/{game_id}").json()

nhlpy/api/players.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import json
21
from typing import Dict, Any
32

43
from nhlpy.http_client import Endpoint, HttpClient
@@ -33,4 +32,4 @@ def players_by_team(self, team_abbr: str, season: str) -> Dict[str, Any]:
3332
Returns:
3433
Dict[str, Any]: Dictionary containing roster information for the specified team and season.
3534
"""
36-
return self.client.get(endpoint=Endpoint.API_WEB_V1, resource=f"roster/{team_abbr}/{season}").json()
35+
return self.client.get(endpoint=Endpoint.API_WEB_V1, resource=f"roster/{team_abbr}/{season}").json()

nhlpy/api/schedule.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from datetime import datetime
22
from typing import Optional, List
33

4-
from nhlpy.http_client import HttpClient
4+
from nhlpy.http_client import HttpClient, Endpoint
55

66

77
class Schedule:
@@ -23,7 +23,7 @@ def daily_schedule(self, date: str = None) -> dict:
2323
except ValueError:
2424
raise ValueError("Invalid date format. Please use YYYY-MM-DD.")
2525

26-
schedule_data: dict = self.client.get(resource=f"schedule/{date}").json()
26+
schedule_data: dict = self.client.get(endpoint=Endpoint.API_WEB_V1, resource=f"schedule/{date}").json()
2727
response_payload = {
2828
"nextStartDate": schedule_data.get("nextStartDate", None),
2929
"previousStartDate": schedule_data.get("previousStartDate", None),
@@ -53,7 +53,7 @@ def weekly_schedule(self, date: Optional[str] = None) -> dict:
5353
"""
5454
res = date if date else "now"
5555

56-
return self.client.get(resource=f"schedule/{res}").json()
56+
return self.client.get(endpoint=Endpoint.API_WEB_V1, resource=f"schedule/{res}").json()
5757

5858
def team_monthly_schedule(self, team_abbr: str, month: Optional[str] = None) -> List[dict]:
5959
"""Gets monthly schedule for specified team or the given month. If no month is supplied it will default to now.
@@ -66,7 +66,7 @@ def team_monthly_schedule(self, team_abbr: str, month: Optional[str] = None) ->
6666
List[dict]: List of games in the monthly schedule.
6767
"""
6868
resource = f"club-schedule/{team_abbr}/month/{month if month else 'now'}"
69-
response = self.client.get(resource=resource).json()
69+
response = self.client.get(endpoint=Endpoint.API_WEB_V1, resource=resource).json()
7070
return response.get("games", [])
7171

7272
def team_weekly_schedule(self, team_abbr: str, date: Optional[str] = None) -> List[dict]:
@@ -81,7 +81,7 @@ def team_weekly_schedule(self, team_abbr: str, date: Optional[str] = None) -> Li
8181
List[dict]: List of games in the weekly schedule.
8282
"""
8383
resource = f"club-schedule/{team_abbr}/week/{date if date else 'now'}"
84-
response = self.client.get(resource=resource).json()
84+
response = self.client.get(endpoint=Endpoint.API_WEB_V1, resource=resource).json()
8585
return response.get("games", [])
8686

8787
def team_season_schedule(self, team_abbr: str, season: str) -> dict:
@@ -94,7 +94,7 @@ def team_season_schedule(self, team_abbr: str, season: str) -> dict:
9494
Returns:
9595
dict: Complete season schedule data including metadata.
9696
"""
97-
request = self.client.get(resource=f"club-schedule-season/{team_abbr}/{season}")
97+
request = self.client.get(endpoint=Endpoint.API_WEB_V1, resource=f"club-schedule-season/{team_abbr}/{season}")
9898

9999
return request.json()
100100

@@ -111,8 +111,7 @@ def calendar_schedule(self, date: str) -> dict:
111111
Example:
112112
API endpoint: https://api-web.nhle.com/v1/schedule-calendar/2023-11-08
113113
"""
114-
return self.client.get(resource=f"schedule-calendar/{date}").json()
115-
114+
return self.client.get(endpoint=Endpoint.API_WEB_V1, resource=f"schedule-calendar/{date}").json()
116115

117116
def playoff_carousel(self, season: str) -> dict:
118117
"""Gets list of all series games up to current playoff round.
@@ -126,8 +125,8 @@ def playoff_carousel(self, season: str) -> dict:
126125
Example:
127126
API endpoint: https://api-web.nhle.com/v1/playoff-series/carousel/20232024/
128127
"""
129-
return self.client.get(resource=f"playoff-series/carousel/{season}").json()
130-
128+
return self.client.get(endpoint=Endpoint.API_WEB_V1, resource=f"playoff-series/carousel/{season}").json()
129+
131130
def playoff_series_schedule(self, season: str, series: str) -> dict:
132131
"""Returns the schedule for a specified playoff series.
133132
@@ -142,7 +141,9 @@ def playoff_series_schedule(self, season: str, series: str) -> dict:
142141
API endpoint: https://api-web.nhle.com/v1/schedule/playoff-series/20232024/a/
143142
"""
144143

145-
return self.client.get(resource=f"schedule/playoff-series/{season}/{series}").json()
144+
return self.client.get(
145+
endpoint=Endpoint.API_WEB_V1, resource=f"schedule/playoff-series/{season}/{series}"
146+
).json()
146147

147148
def playoff_bracket(self, year: str) -> dict:
148149
"""Returns the playoff bracket.
@@ -157,4 +158,4 @@ def playoff_bracket(self, year: str) -> dict:
157158
API endpoint: https://api-web.nhle.com/v1/playoff-bracket/2024
158159
"""
159160

160-
return self.client.get(resource=f"playoff-bracket/{year}").json()
161+
return self.client.get(endpoint=Endpoint.API_WEB_V1, resource=f"playoff-bracket/{year}").json()

nhlpy/api/standings.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
from typing import List, Optional
22

3+
from nhlpy.http_client import Endpoint
4+
35

46
class Standings:
57
def __init__(self, http_client):
68
self.client = http_client
79

8-
def get_standings(self, date: Optional[str] = None, season: Optional[str] = None) -> dict:
10+
def league_standings(self, date: Optional[str] = None, season: Optional[str] = None) -> dict:
911
"""Gets league standings for a specified season or date.
1012
1113
Retrieves NHL standings either for a specific date or for the end of a season.
@@ -32,7 +34,7 @@ def get_standings(self, date: Optional[str] = None, season: Optional[str] = None
3234

3335
res = date if date else "now"
3436

35-
return self.client.get(resource=f"standings/{res}").json()
37+
return self.client.get(endpoint=Endpoint.API_WEB_V1, resource=f"standings/{res}").json()
3638

3739
def season_standing_manifest(self) -> List[dict]:
3840
"""Gets metadata for all NHL seasons.
@@ -59,5 +61,5 @@ def season_standing_manifest(self) -> List[dict]:
5961
"wildcardInUse": true
6062
}]
6163
"""
62-
response = self.client.get(resource="standings-season").json()
64+
response = self.client.get(endpoint=Endpoint.API_WEB_V1, resource="standings-season").json()
6365
return response.get("seasons", [])

nhlpy/api/stats.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from nhlpy.api.query.builder import QueryContext
55
from nhlpy.api.query.filters import _goalie_stats_sorts
66
from nhlpy.api.query.sorting.sorting_options import SortingOptions
7-
from nhlpy.http_client import HttpClient
7+
from nhlpy.http_client import HttpClient, Endpoint
88

99

1010
class Stats:
@@ -34,7 +34,7 @@ def gametypes_per_season_directory_by_team(self, team_abbr: str) -> dict:
3434
]
3535
3636
"""
37-
return self.client.get(resource=f"club-stats-season/{team_abbr}").json()
37+
return self.client.get(endpoint=Endpoint.API_WEB_V1, resource=f"club-stats-season/{team_abbr}").json()
3838

3939
def player_career_stats(self, player_id: str) -> dict:
4040
"""Gets a player's career statistics and biographical information.
@@ -68,7 +68,7 @@ def player_career_stats(self, player_id: str) -> dict:
6868
'sweaterNumber': 97,
6969
'position': 'C',
7070
"""
71-
return self.client.get(resource=f"player/{player_id}/landing").json()
71+
return self.client.get(endpoint=Endpoint.API_WEB_V1, resource=f"player/{player_id}/landing").json()
7272

7373
def player_game_log(self, player_id: str, season_id: str, game_type: int) -> List[dict]:
7474
"""Gets a player's game log for a specific season and game type.
@@ -113,7 +113,9 @@ def player_game_log(self, player_id: str, season_id: str, game_type: int) -> Lis
113113
...
114114
]
115115
"""
116-
data = self.client.get(resource=f"player/{player_id}/game-log/{season_id}/{game_type}").json()
116+
data = self.client.get(
117+
endpoint=Endpoint.API_WEB_V1, resource=f"player/{player_id}/game-log/{season_id}/{game_type}"
118+
).json()
117119
return data.get("gameLog", [])
118120

119121
def team_summary(
@@ -211,7 +213,7 @@ def team_summary(
211213
default_cayenne_exp = f"gameTypeId={game_type_id} and seasonId<={end_season} and seasonId>={start_season}"
212214
q_params["cayenneExp"] = default_cayenne_exp
213215

214-
return self.client.get_by_url("https://api.nhle.com/stats/rest/en/team/summary", query_params=q_params).json()[
216+
return self.client.get(endpoint=Endpoint.API_STATS, resource="en/team/summary", query_params=q_params).json()[
215217
"data"
216218
]
217219

@@ -314,7 +316,7 @@ def skater_stats_summary_simple(
314316
default_cayenne_exp = f"franchiseId={franchise_id} and {default_cayenne_exp}"
315317
q_params["cayenneExp"] = default_cayenne_exp
316318

317-
return self.client.get_by_url("https://api.nhle.com/stats/rest/en/skater/summary", query_params=q_params).json()[
319+
return self.client.get(endpoint=Endpoint.API_STATS, resource="en/skater/summary", query_params=q_params).json()[
318320
"data"
319321
]
320322

@@ -413,8 +415,8 @@ def skater_stats_with_query_context(
413415

414416
q_params["sort"] = json.dumps(sort_expr)
415417
q_params["cayenneExp"] = query_context.query_str
416-
return self.client.get_by_url(
417-
f"https://api.nhle.com/stats/rest/en/skater/{report_type}", query_params=q_params
418+
return self.client.get(
419+
endpoint=Endpoint.API_STATS, resource=f"en/skater/{report_type}", query_params=q_params
418420
).json()
419421

420422
def goalie_stats_summary_simple(
@@ -510,7 +512,7 @@ def goalie_stats_summary_simple(
510512

511513
q_params["cayenneExp"] = default_cayenne_exp
512514

513-
response = self.client.get_by_url(
514-
f"https://api.nhle.com/stats/rest/en/goalie/{stats_type}", query_params=q_params
515+
response = self.client.get(
516+
endpoint=Endpoint.API_STATS, resource=f"en/goalie/{stats_type}", query_params=q_params
515517
).json()
516518
return response.get("data", [])

0 commit comments

Comments
 (0)