Skip to content

Commit

Permalink
fix #270 (#271)
Browse files Browse the repository at this point in the history
* fixed #270

Fixed #270 as well as other issues.

* add docs

* remove comment

* add a comment
  • Loading branch information
nwlandry authored Jan 24, 2023
1 parent ff39b36 commit f113d4e
Showing 1 changed file with 41 additions and 17 deletions.
58 changes: 41 additions & 17 deletions xgi/readwrite/xgi_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ def load_xgi_data(
The specified dataset does not exist.
"""

# If no dataset is specified, print a list of the available datasets.
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 +121,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)

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"])


@lru_cache(maxsize=None)
Expand All @@ -169,3 +163,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}")

0 comments on commit f113d4e

Please sign in to comment.