Skip to content

Commit 729e3ed

Browse files
committed
refactor: allow specification of gateway address via API parameter
1 parent 350c565 commit 729e3ed

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

ipfsspec/async_ipfs.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,17 @@ def gateway_from_file(gateway_path, protocol="ipfs"):
180180

181181

182182
@lru_cache
183-
def get_gateway(protocol="ipfs"):
183+
def get_gateway(protocol="ipfs", gateway_addr=None):
184184
"""
185185
Get IPFS gateway according to IPIP-280
186186
187187
see: https://github.com/ipfs/specs/pull/280
188188
"""
189189

190+
if gateway_addr:
191+
logger.debug("using IPFS gateway as specified via function argument: %s", gateway_addr)
192+
return AsyncIPFSGateway(gateway_addr, protocol)
193+
190194
# IPFS_GATEWAY environment variable should override everything
191195
ipfs_gateway = os.environ.get("IPFS_GATEWAY", "")
192196
if ipfs_gateway:
@@ -263,19 +267,20 @@ class AsyncIPFSFileSystem(AsyncFileSystem):
263267
sep = "/"
264268
protocol = "ipfs"
265269

266-
def __init__(self, asynchronous=False, loop=None, client_kwargs=None, **storage_options):
270+
def __init__(self, asynchronous=False, loop=None, client_kwargs=None, gateway_addr=None, **storage_options):
267271
super().__init__(self, asynchronous=asynchronous, loop=loop, **storage_options)
268272
self._session = None
269273

270274
self.client_kwargs = client_kwargs or {}
271275
self.get_client = get_client
276+
self.gateway_addr = gateway_addr
272277

273278
if not asynchronous:
274279
sync(self.loop, self.set_session)
275280

276281
@property
277282
def gateway(self):
278-
return get_gateway(self.protocol)
283+
return get_gateway(self.protocol, gateway_addr=self.gateway_addr)
279284

280285
@staticmethod
281286
def close_session(loop, session):

test/test_async.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ async def get_client(**kwargs):
2222

2323

2424
@pytest_asyncio.fixture
25-
async def fs(get_client):
25+
async def fs(request, get_client):
2626
AsyncIPFSFileSystem.clear_instance_cache() # avoid reusing old event loop
27-
return AsyncIPFSFileSystem(asynchronous=True, loop=asyncio.get_running_loop(), get_client=get_client)
27+
gateway_addr = getattr(request, "param", None)
28+
return AsyncIPFSFileSystem(asynchronous=True, loop=asyncio.get_running_loop(), get_client=get_client, gateway_addr=gateway_addr)
2829

2930

3031
@pytest.mark.parametrize("gw_host", ["http://127.0.0.1:8080"])

0 commit comments

Comments
 (0)