cenpy-devs / cenpy

Explore and download data from Census APIs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

API Error on Import

lazarust opened this issue · comments

Is there a reason the API is pinged when importing the library? It seems a little weird and has caused some issues for me when running tests. Thanks!

The library fetches the current copy of the metadata upon import, and uses this to structure the API. it's part of the design: we don't want to mirror or maintain duplicates of the main API metadata endpoints.

I suppose, strictly speaking, it doesn't have to be fetched on import, but rather could be pushed down into the first (if any) call to explorer.available() & cached once fetched.

Happy to merge any contribution to do this!

@ljwolf Could you point me to where that initial API fetch is? I'm having trouble finding it. Thanks 👍🏽

@lazarust See line 13 in cenpy/explorer.py:

cenpy/cenpy/explorer.py

Lines 12 to 35 in f767140

resp = raw_APIs = r.get("https://api.census.gov/data.json")
try:
resp.raise_for_status()
raw_APIs = resp.json()["dataset"]
APIs = {
entry["identifier"]
.split("id")[-1]
.lstrip("/"): {
key: value for key, value in diter(entry) if key != entry["identifier"]
}
for entry in raw_APIs
}
except r.HTTPError:
raise r.HTTPError(
"The main Census API Endpoint (https://api.census.gov/data.json) is not available."
" Try visiting https://api.census.gov/data.json in a web browser to verify connectivity."
)
except JSONDecodeError:
raise JSONDecodeError(
"The main Census API Endpoint (https://api.census.gov/data.json) returned malformed content."
" Try visiting https://api.census.gov/data.json in a web browser to verify connectivity."
)