Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #270 #271

Merged
merged 4 commits into from
Jan 24, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 40 additions & 17 deletions xgi/readwrite/xgi_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ def load_xgi_data(
The specified dataset does not exist.
"""

if dataset is None:
index_url = "https://gitlab.com/complexgroupinteractions/xgi-data/-/raw/main/index.json?inline=false"
index_data = _request_json_from_url(index_url)
print("Available datasets are the following:")
print(*index_data, sep="\n")
return

if read:
cfp = os.path.join(path, dataset + ".json")
if os.path.exists(cfp):
Expand Down Expand Up @@ -113,37 +120,23 @@ def _request_from_xgi_data(dataset=None):
Raises
------
XGIError
If HTTP request is not successful of the dataset does not exist.
If the HTTP request is not successful or the dataset does not exist.

See also
---------
load_xgi_data
"""

index_url = "https://gitlab.com/complexgroupinteractions/xgi-data/-/raw/main/index.json?inline=false"
r = requests.get(index_url)

if r.ok:
index_data = r.json()

if dataset is None:
print("Available datasets are the following:")
print(*index_data, sep="\n")
return
else:
raise XGIError(f"Error: HTTP response {r.status_code}")
index_data = _request_json_from_url(index_url)
nwlandry marked this conversation as resolved.
Show resolved Hide resolved

key = dataset.lower()
if key not in index_data:
print("Valid dataset names:")
print(*index_data, sep="\n")
raise XGIError("Must choose a valid dataset name!")

r = requests.get(index_data[key]["url"])
if r.ok:
return r.json()
else:
raise XGIError(f"Error: HTTP response {r.status_code}")
return _request_json_from_url(index_data[key]["url"])
nwlandry marked this conversation as resolved.
Show resolved Hide resolved


@lru_cache(maxsize=None)
Expand All @@ -169,3 +162,33 @@ def _request_from_xgi_data_cached(dataset):
"""

return _request_from_xgi_data(dataset)


def _request_json_from_url(url):
"""HTTP request json file and return as dict.

Parameters
----------
url : str
The url where the json file is located.

Returns
-------
dict
A dictionary of the JSON requested.

Raises
------
XGIError
If the connection fails or if there is a bad HTTP request.
"""

try:
r = requests.get(url)
except requests.ConnectionError:
raise XGIError("Connection Error!")

if r.ok:
return r.json()
else:
raise XGIError(f"Error: HTTP response {r.status_code}")