Skip to content

Commit

Permalink
stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyagreco committed Sep 12, 2024
1 parent 9ef333e commit 506b68e
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 38 deletions.
2 changes: 1 addition & 1 deletion example/draft_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
if __name__ == "__main__":
# get all drafts that a user was in for a particular year
user_drafts = get_user_drafts_for_year(
user_id="my_user_id", sport=Sport.NFL, year="2020"
user_id="my_user_id", sport=Sport.NFL, year=2020
)

# get all drafts for a particular league
Expand Down
44 changes: 11 additions & 33 deletions example/league_example.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,35 @@
from sleeper.api import LeagueAPIClient
from sleeper.api.league import get_league, get_rosters, get_user_leagues_for_year
from sleeper.enum import Sport
from sleeper.model import (
League,
Matchup,
PlayoffMatchup,
Roster,
SportState,
TradedPick,
Transaction,
User,
)

if __name__ == "__main__":
# get a league by its ID
league: League = LeagueAPIClient.get_league(league_id="my_league_id")
league = get_league(league_id="my_league_id")

# get all leagues for a user by its ID in a particular year
user_leagues: list[League] = LeagueAPIClient.get_user_leagues_for_year(
user_leagues = get_user_leagues_for_year(
user_id="my_user_id", sport=Sport.NFL, year="2020"
)

# get all rosters in a particular league
league_rosters: list[Roster] = LeagueAPIClient.get_rosters(league_id="my_league_id")
league_rosters = get_rosters(league_id="my_league_id")

# get all users in a particular league
league_users: list[User] = LeagueAPIClient.get_users_in_league(
league_id="my_league_id"
)
league_users = get_users_in_league(league_id="my_league_id")

# get all matchups in a week for a particular league
week_1_matchups: list[Matchup] = LeagueAPIClient.get_matchups_for_week(
league_id="my_league_id", week=1
)
week_1_matchups = get_matchups_for_week(league_id="my_league_id", week=1)

# get the winners bracket for a particular league
winners_bracket: list[PlayoffMatchup] = LeagueAPIClient.get_winners_bracket(
league_id="my_league_id"
)
winners_bracket = get_winners_bracket(league_id="my_league_id")

# get the losers bracket for a particular league
losers_bracket: list[PlayoffMatchup] = LeagueAPIClient.get_losers_bracket(
league_id="my_league_id"
)
losers_bracket = get_losers_bracket(league_id="my_league_id")

# get all transactions in a week for a particular league
week_1_transactions: list[Transaction] = LeagueAPIClient.get_transactions(
league_id="my_league_id", week=1
)
week_1_transactions = get_transactions(league_id="my_league_id", week=1)

# get all traded picks for a particular league
traded_picks: list[TradedPick] = LeagueAPIClient.get_traded_picks(
league_id="my_league_id"
)
traded_picks = get_traded_picks(league_id="my_league_id")

# get the state of a particular sport
nfl_state: SportState = LeagueAPIClient.get_sport_state(sport=Sport.NFL)
nfl_state = get_sport_state(sport=Sport.NFL)
2 changes: 1 addition & 1 deletion sleeper/api/draft.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from sleeper.enum.Sport import Sport


def get_user_drafts_for_year(*, user_id: str, sport: Sport, year: str) -> list[dict]:
def get_user_drafts_for_year(*, user_id: str, sport: Sport, year: int) -> list[dict]:
url = build_route(
SLEEPER_APP_BASE_URL,
VERSION,
Expand Down
16 changes: 16 additions & 0 deletions sleeper/api/league.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
from sleeper.api.constants import (
LEAGUE_ROUTE,
LEAGUES_ROUTE,
SLEEPER_APP_BASE_URL,
USER_ROUTE,
VERSION,
)
from sleeper.api.util import build_route, get
from sleeper.enum import Sport


def get_league(*, league_id: str) -> dict:
url = build_route(SLEEPER_APP_BASE_URL, VERSION, LEAGUE_ROUTE, league_id)
return get(url)


def get_user_leagues_for_year(*, user_id: str, sport: Sport, year: int) -> list[dict]:
url = build_route(
SLEEPER_APP_BASE_URL,
VERSION,
USER_ROUTE,
user_id,
LEAGUES_ROUTE,
sport.value.lower(),
year,
)
return get(url)
2 changes: 1 addition & 1 deletion test/integration/test_api/test_draft.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
class TestDraft(unittest.TestCase):
def test_get_user_drafts_for_year_happy_path(self):
response = get_user_drafts_for_year(
user_id=USER_A_USER_ID, sport=Sport.NFL, year="2023"
user_id=USER_A_USER_ID, sport=Sport.NFL, year=2023
)
self.assertEqual(USER_A_DRAFTS_2023, response)

Expand Down
2 changes: 1 addition & 1 deletion test/unit/test_api/test_draft.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_get_user_drafts_for_year_happy_path(self, mock_requests_get):
mock_requests_get.return_value = mock_response

response = get_user_drafts_for_year(
user_id="user_id", sport=Sport.NFL, year="2020"
user_id="user_id", sport=Sport.NFL, year=2020
)

self.assertEqual(mock_list, response)
Expand Down
18 changes: 17 additions & 1 deletion test/unit/test_api/test_league.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import unittest
from unittest.mock import patch

from sleeper.api.league import get_league
from sleeper.api.league import get_league, get_user_leagues_for_year
from sleeper.enum import Sport
from test.unit.helper.helper_classes import MockResponse


Expand All @@ -18,3 +19,18 @@ def test_get_league(self, mock_requests_get):
mock_requests_get.assert_called_once_with(
"https://api.sleeper.app/v1/league/12345"
)

@patch("requests.get")
def test_get_user_leagues_for_year(self, mock_requests_get):
mock_list = [{"foo": "bar"}]
mock_response = MockResponse(mock_list, 200)
mock_requests_get.return_value = mock_response

response = get_user_leagues_for_year(
user_id="12345", sport=Sport.NFL, year=2024
)

self.assertEqual(mock_list, response)
mock_requests_get.assert_called_once_with(
"https://api.sleeper.app/v1/user/12345/leagues/nfl/2024"
)

0 comments on commit 506b68e

Please sign in to comment.