Skip to content

Commit a59c00b

Browse files
authored
Update inventory.py
1 parent e2d555e commit a59c00b

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

brainimagelibrary/inventory.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,30 +67,35 @@ def get(dataset_id=None):
6767
"""
6868
Retrieves metadata for a dataset by its ID.
6969
70-
This function fetches metadata for a dataset from a remote server
71-
using its unique identifier. The metadata is retrieved as a JSON
72-
response from the Brain Image Library's API.
73-
7470
Args:
7571
dataset_id (str, optional): The unique identifier for the dataset. Defaults to None.
7672
7773
Returns:
7874
dict: A dictionary containing the dataset metadata if the request is successful.
79-
8075
None: If the request fails or encounters an exception.
81-
82-
Raises:
83-
requests.exceptions.RequestException: If an error occurs during the API request.
8476
"""
85-
77+
if dataset_id is None:
78+
print("Error: dataset_id must be provided.")
79+
return None
80+
8681
filename = f"{dataset_id}.json"
8782
url = f"https://download.brainimagelibrary.org/inventory/datasets/{filename}"
8883

8984
try:
90-
response = requests.get(url)
85+
response = requests.get(url, timeout=30)
86+
87+
# Check if request was successful
88+
if response.status_code != 200:
89+
print(f"Error: received status code {response.status_code} for {url}")
90+
return None
91+
92+
# Ensure we got JSON
93+
try:
94+
return response.json()
95+
except ValueError:
96+
print("Error: Response is not valid JSON.")
97+
return None
9198

92-
response = response.json()
93-
return response
9499
except requests.exceptions.RequestException as e:
95100
print(f"Error making API request: {e}")
96101
return None

0 commit comments

Comments
 (0)