From 5eee23b83134aa8acb7654bd7c6ef1d57f8ae181 Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Wed, 5 Feb 2020 11:11:11 -0800 Subject: [PATCH] factor out common code into a function... --- casimport/__init__.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) 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