From 089fd62e1572deb8c32651c334b53c0f7878c04e Mon Sep 17 00:00:00 2001 From: Carl-Christian Sautter Date: Sun, 6 Oct 2024 11:18:30 +0200 Subject: [PATCH 1/2] fix: handle backoff increases for api auth errors * added backoff giveup condition * added message for 403 errors --- myeia/api.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/myeia/api.py b/myeia/api.py index f05e415..5d8d02f 100644 --- a/myeia/api.py +++ b/myeia/api.py @@ -1,6 +1,7 @@ import logging import os import time +import warnings from datetime import date from typing import Optional, Union @@ -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, @@ -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"]) From 221632f08bcbd0bc7e65e72b56f3b5ba43c6b99e Mon Sep 17 00:00:00 2001 From: Carl-Christian Sautter Date: Sun, 6 Oct 2024 19:26:24 +0200 Subject: [PATCH 2/2] fix: get_series empty dataframe handling and column rename --- myeia/api.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/myeia/api.py b/myeia/api.py index 5d8d02f..1371548 100644 --- a/myeia/api.py +++ b/myeia/api.py @@ -105,10 +105,16 @@ def get_series( df = df.sort_index(ascending=False) df[data_identifier] = df[data_identifier].astype(float) + # Filtering the DataFrame by the specified date range can result in an empty DataFrame + if df.empty: + return df + for col in df.columns: if "name" in col.lower() or "description" in col.lower(): df = df.rename(columns={data_identifier: df[col][0]}) df = df[df[col][0]].to_frame() + break + return df def get_series_via_route(