From 47609472a6ef5b6c0e308b004ec634c418277cae Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Fri, 7 Feb 2020 16:47:55 -0800 Subject: [PATCH] add support for configuring IPFS via the config file... --- casimport/__init__.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/casimport/__init__.py b/casimport/__init__.py index 8759ccf..0a5fd3c 100644 --- a/casimport/__init__.py +++ b/casimport/__init__.py @@ -141,16 +141,19 @@ class HTTPSCAS(object): return urlfetch(url) class IPFSCAS(object): - gwhost = 'gateway.ipfs.io' - gwhost = 'cloudflare-ipfs.com' + gwurl = 'https://gateway.ipfs.io/ipfs/' + gwurl = 'https://cloudflare-ipfs.com/ipfs/' + + def __init__(self, gw=None): + if gw is not None: + self.gwurl = gw @classmethod def fromconfig(cls, conf): - return cls() + return cls(conf.get('gateway', None)) def make_url(self, url): - return urllib.parse.urlunparse(('https', self.gwhost, - '/ipfs/' + url.netloc) + ('', ) * 3) + return urllib.parse.urljoin(self.gwurl, url.netloc) def fetch_data(self, url): if url.scheme != 'ipfs': @@ -536,6 +539,14 @@ class Test(unittest.TestCase): # and that the second loader is the IPFSCAS self.assertIsInstance(f._loaders[1], IPFSCAS) + def test_ipfsgwfromconfig(self): + gwurl = 'https://www.example.com/somepath/' + ipfsconf = dict(gateway=gwurl) + ipfsobj = IPFSCAS.fromconfig(ipfsconf) + + self.assertEqual(ipfsobj.make_url(urllib.parse.urlparse('ipfs://someobj')), + 'https://www.example.com/somepath/someobj') + def test_defaultinit(self): temphome = self.tempdir / 'home' temphome.mkdir()