| @@ -105,6 +105,13 @@ def tempattrset(obj, key, value): | |||||
| else: | else: | ||||
| delattr(obj, key) | 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): | class HTTPSCAS(object): | ||||
| def fetch_data(self, url): | def fetch_data(self, url): | ||||
| if url.scheme != 'https': | if url.scheme != 'https': | ||||
| @@ -112,11 +119,7 @@ class HTTPSCAS(object): | |||||
| repr(url.scheme)) | repr(url.scheme)) | ||||
| url = urllib.parse.urlunparse(url) | 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): | class IPFSCAS(object): | ||||
| gwhost = 'gateway.ipfs.io' | gwhost = 'gateway.ipfs.io' | ||||
| @@ -132,12 +135,7 @@ class IPFSCAS(object): | |||||
| repr(url.scheme)) | repr(url.scheme)) | ||||
| gwurl = self.make_url(url) | 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): | class FileDirCAS(object): | ||||
| '''A file loader for CAS that operates on a directory. It looks | '''A file loader for CAS that operates on a directory. It looks | ||||