Skip to content

Commit

Permalink
FIX: Check status code for from_url
Browse files Browse the repository at this point in the history
  • Loading branch information
effigies committed Jun 6, 2022
1 parent 5499b28 commit 962186f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
18 changes: 18 additions & 0 deletions nibabel/cifti2/tests/test_caretspec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from pathlib import Path

from nibabel.cifti2.caretspec import *

from nibabel.testing import data_path


def test_CaretSpecFile():
fsLR = CaretSpecFile.from_filename(Path(data_path) / "fsLR.wb.spec")

assert fsLR.metadata == {}
assert fsLR.version == "1.0"
assert len(fsLR.data_files) == 5

for df in fsLR.data_files:
assert isinstance(df, CaretSpecDataFile)
if df.data_file_type == 'SURFACE':
assert isinstance(df, SurfaceDataFile)
6 changes: 4 additions & 2 deletions nibabel/filebasedimages.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,11 +576,13 @@ def to_bytes(self, **kwargs):
return bio.getvalue()

@classmethod
def from_url(klass, url):
def from_url(klass, url, timeout=5):
""" Fetch image from remote location and construct an image
Requires the ``requests`` library to be installed.
"""
import requests
res = requests.get(url)
res = requests.get(url, timeout=timeout)
if not res.ok:
raise IOError(f"[Error {res.status_code}] Could not retrieve {url}")
return klass.from_bytes(res.content)

0 comments on commit 962186f

Please sign in to comment.