diff --git a/casimport/__init__.py b/casimport/__init__.py index ed3e89a..a07cc98 100644 --- a/casimport/__init__.py +++ b/casimport/__init__.py @@ -105,6 +105,13 @@ def tempattrset(obj, key, value): else: delattr(obj, key) +def urlfetch(url): + with urllib.request.urlopen(url) as req: + if req.status // 100 != 2: + raise RuntimeError('bad fetch') + + return req.read() + class HTTPSCAS(object): def fetch_data(self, url): if url.scheme != 'https': @@ -112,11 +119,7 @@ class HTTPSCAS(object): repr(url.scheme)) url = urllib.parse.urlunparse(url) - with urllib.request.urlopen(url) as req: - if req.status // 100 != 2: - raise RuntimeError('bad fetch') - - return req.read() + return urlfetch(url) class IPFSCAS(object): gwhost = 'gateway.ipfs.io' @@ -132,12 +135,7 @@ class IPFSCAS(object): repr(url.scheme)) gwurl = self.make_url(url) - - with urllib.request.urlopen(gwurl) as req: - if req.status // 100 != 2: - raise RuntimeError('bad fetch') - - return req.read() + return urlfetch(gwurl) class FileDirCAS(object): '''A file loader for CAS that operates on a directory. It looks