Skip to content

Commit

Permalink
update API error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
cklunch committed Oct 23, 2024
1 parent aefec98 commit 208091d
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 6 deletions.
Binary file modified dist/neonutilities-1.0.1-py3-none-any.whl
Binary file not shown.
Binary file modified dist/neonutilities-1.0.1.tar.gz
Binary file not shown.
8 changes: 5 additions & 3 deletions src/neonutilities/aop_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def get_file_urls(urls, token=None):
releases = []
for url in urls:
response = get_api(api_url=url, token=token)
if not response:
if response is None:
logging.info(
"Data file retrieval failed. Check NEON data portal for outage alerts.")

Expand Down Expand Up @@ -476,7 +476,8 @@ def by_file_aop(dpid,

# download issue log table
ilog = get_issue_log(dpid=dpid, token=None)
ilog.to_csv(f"{download_path}/issueLog_{dpid}.csv", index=False)
if ilog is not None:
ilog.to_csv(f"{download_path}/issueLog_{dpid}.csv", index=False)

# download citations
if "PROVISIONAL" in releases:
Expand Down Expand Up @@ -853,7 +854,8 @@ def get_buffer_coords(easting, northing, buffer):

# download issue log table
ilog = get_issue_log(dpid=dpid, token=None)
ilog.to_csv(f"{download_path}/issueLog_{dpid}.csv", index=False)
if ilog is not None:
ilog.to_csv(f"{download_path}/issueLog_{dpid}.csv", index=False)

# download citations
if "PROVISIONAL" in releases:
Expand Down
8 changes: 8 additions & 0 deletions src/neonutilities/get_issue_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
"""

import re
import logging
import pandas as pd
from .helper_mods.api_helpers import get_api

logging.basicConfig(level=logging.INFO, format='%(message)s')

# %% functions to validate inputs (should pull these out into another helper module??)


Expand Down Expand Up @@ -54,6 +57,9 @@ def get_change_log_df(dpid, token=None):
"""
req = get_api(
api_url=f"http://data.neonscience.org/api/v0/products/{dpid}", token=token)
if req is None:
logging.info(f"Error in metadata retrieval for {dpid}. Issue log not found.")
return None
all_product_info = pd.json_normalize(req.json()['data'])
change_log_df = pd.DataFrame(all_product_info['changeLogs'][0])

Expand Down Expand Up @@ -87,6 +93,8 @@ def get_eddy_issue_log(dpid, token=None):
if change_log_df is not None and not change_log_df.empty:
change_log_df['dpid'] = dpid
eddy_issue_log_list.append(change_log_df)
else:
return None

eddy_issue_log_df = pd.concat(eddy_issue_log_list, ignore_index=True)

Expand Down
12 changes: 9 additions & 3 deletions src/neonutilities/helper_mods/api_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ def get_status_code_meaning(status_code):

return response

except Exception:
raise ConnectionError(
"No response. NEON API may be unavailable, check NEON data portal for outage alerts. If the problem persists and can't be traced to an outage alert, check your computer for firewall or other security settings preventing Python from accessing the internet.")
except Exception as error:
print(error)
return None


def get_api_headers(api_url,
Expand Down Expand Up @@ -257,6 +257,9 @@ def get_zip_urls(url_set,

# get list of files from data endpoint
m_res = get_api(api_url=url_set[i], token=token)
if m_res is None:
logging.info("Connection error for a subset of urls. Check outputs for missing data.")
return None
m_di = m_res.json()

# only keep queried release
Expand Down Expand Up @@ -374,6 +377,9 @@ def get_tab_urls(url_set,

# get list of files from data endpoint
m_res = get_api(api_url=url_set[i], token=token)
if m_res is None:
logging.info("Connection error for a subset of urls. Check outputs for missing data.")
return None
m_di = m_res.json()

# only keep queried release
Expand Down
5 changes: 5 additions & 0 deletions src/neonutilities/tabular_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ def query_files(lst, dpid, site="all", startdate=None, enddate=None,
# construct full query url and run query
qurl = "http://data.neonscience.org/api/v0/data/query?productCode=" + dpid + sitesurl + dateurl + ipurl + "&package=" + package + relurl
qreq = get_api(api_url=qurl, token=token)
if qreq is None:
logging.info("No API response for selected query. Check inputs.")
return None
qdict = qreq.json()

# get file list from dictionary response
Expand Down Expand Up @@ -291,6 +294,8 @@ def zips_by_product(dpid, site="all", startdate=None, enddate=None,
if release != "current" and release != "PROVISIONAL":
rels = get_api(api_url="http://data.neonscience.org/api/v0/releases/",
token=token)
if rels is None:
raise ConnectionError("Data product was not found or API was unreachable.")
relj = rels.json()
reld = relj["data"]
rellist = []
Expand Down

0 comments on commit 208091d

Please sign in to comment.