Browse Source

factor out common code into a function...

main
John-Mark Gurney 5 years ago
parent
commit
5eee23b831
1 changed files with 9 additions and 11 deletions
  1. +9
    -11
      casimport/__init__.py

+ 9
- 11
casimport/__init__.py View File

@@ -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


Loading…
Cancel
Save