Skip to content

Commit

Permalink
refactored check_league_endpoint to handle 401/404 errors, using chec…
Browse files Browse the repository at this point in the history
…kRequestStatus
  • Loading branch information
Robert Litts committed Jan 2, 2025
1 parent ceb79c4 commit 0f6692c
Showing 1 changed file with 36 additions and 16 deletions.
52 changes: 36 additions & 16 deletions espn_api/requests/espn_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,31 +136,51 @@ def get_player_card(self, playerIds: List[int], max_scoring_period: int, additio
return data

def check_league_endpoint(self):
# First, try the current LEAGUE_ENDPOINT
r = requests.get(self.LEAGUE_ENDPOINT, cookies=self.cookies)

# If the response is 401 Unauthorized, switch to the other endpoint
if r.status_code == 401:
# If the current LEAGUE_ENDPOINT was using the /leagueHistory/ endpoint, switch to the /seasons/{year}/segments/0 endpoint

if self.logger:
self.logger.log_request(
endpoint=self.LEAGUE_ENDPOINT,
params={},
headers={},
response=r.json()
)

# If the response is 401 or 404, try alternate endpoint
if r.status_code in [401, 404]:
# If the current LEAGUE_ENDPOINT was using the /leagueHistory/ endpoint
if "/leagueHistory/" in self.LEAGUE_ENDPOINT:
alternate_endpoint = self.LEAGUE_ENDPOINT.replace("/leagueHistory/", "/seasons/" + str(self.year) + "/segments/0/leagues/")
alternate_endpoint = self.LEAGUE_ENDPOINT.replace(
"/leagueHistory/",
f"/seasons/{self.year}/segments/0/leagues/"
)
else:
# Otherwise, switch to the /leagueHistory/{league_id}?seasonId={year} endpoint
alternate_endpoint = self.LEAGUE_ENDPOINT.replace("/seasons/" + str(self.year) + "/segments/0/leagues/", "/leagueHistory/") + "?seasonId=" + str(self.year)

# Switch to the /leagueHistory/{league_id}?seasonId={year} endpoint
base_endpoint = self.LEAGUE_ENDPOINT.replace(
f"/seasons/{self.year}/segments/0/leagues/",
"/leagueHistory/"
)
alternate_endpoint = f"{base_endpoint}?seasonId={self.year}"

# Try the alternate endpoint
r = requests.get(alternate_endpoint, cookies=self.cookies)

# If the alternate endpoint works (status 200), update the LEAGUE_ENDPOINT
if self.logger:
self.logger.log_request(
endpoint=alternate_endpoint,
params={},
headers={},
response=r.json()
)

# If the alternate endpoint works, update the LEAGUE_ENDPOINT
if r.status_code == 200:
self.LEAGUE_ENDPOINT = alternate_endpoint
if self.logger:
self.logger.logging.info(f"Switched to alternate endpoint: {self.LEAGUE_ENDPOINT}")
else:
# If both endpoints fail, log the error and raise an exception
if self.logger:
self.logger.logging.error(f"Both endpoints failed with status: {r.status_code}")
raise Exception(f"Both endpoints for league {self.league_id} failed with status {r.status_code}")
checkRequestStatus(r.status_code, self.cookies, self.league_id)
else:
# Check status of initial request if it wasn't 401/404
checkRequestStatus(r.status_code, self.cookies, self.league_id)

# Username and password no longer works using their API without using google recaptcha
# Possibly revisit in future if anything changes
Expand Down

0 comments on commit 0f6692c

Please sign in to comment.