Skip to content

Commit

Permalink
fix: handle backoff increases for api auth errors
Browse files Browse the repository at this point in the history
* added backoff giveup condition
* added message for 403 errors
  • Loading branch information
csautter committed Oct 6, 2024
1 parent 8409f42 commit 089fd62
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions myeia/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import os
import time
import warnings
from datetime import date
from typing import Optional, Union

Expand Down Expand Up @@ -39,6 +40,7 @@ def __init__(
max_tries=10,
raise_on_giveup=True,
jitter=backoff.full_jitter,
giveup=lambda e: e.response.status_code == 403,
)
def get_response(
self,
Expand All @@ -48,6 +50,9 @@ def get_response(
"""Helper function to get the response from the EIA API and return it as a dataframe."""
time.sleep(0.25)
response = requests.get(url, headers=headers)
if response.status_code == 403:
response.reason = "Forbidden! It's likely that the API key is invalid, not set or the request limit has been reached."

response.raise_for_status()
json_response = response.json()
return pd.DataFrame(json_response["response"]["data"])
Expand Down

0 comments on commit 089fd62

Please sign in to comment.