@@ -90,8 +90,9 @@ def _raise_not_found_for_status(self, response, url):
90
90
class AsyncIPFSGateway (AsyncIPFSGatewayBase ):
91
91
resolution = "path"
92
92
93
- def __init__ (self , url ):
93
+ def __init__ (self , url , protocol = "ipfs" ):
94
94
self .url = url
95
+ self .protocol = protocol
95
96
96
97
async def api_get (self , endpoint , session , ** kwargs ):
97
98
res = await session .get (self .url + "/api/v0/" + endpoint , params = kwargs , trace_request_ctx = {'gateway' : self .url })
@@ -106,7 +107,7 @@ async def api_post(self, endpoint, session, **kwargs):
106
107
async def _cid_req (self , method , path , headers = None , ** kwargs ):
107
108
headers = headers or {}
108
109
if self .resolution == "path" :
109
- res = await method (self .url + "/ipfs/" + path , trace_request_ctx = {'gateway' : self .url }, headers = headers , ** kwargs )
110
+ res = await method ("/" . join (( self .url , self . protocol , path )) , trace_request_ctx = {'gateway' : self .url }, headers = headers , ** kwargs )
110
111
elif self .resolution == "subdomain" :
111
112
raise NotImplementedError ("subdomain resolution is not yet implemented" )
112
113
else :
@@ -145,17 +146,17 @@ async def get_client(**kwargs):
145
146
return aiohttp .ClientSession (** kwargs )
146
147
147
148
148
- def gateway_from_file (gateway_path ):
149
+ def gateway_from_file (gateway_path , protocol = "ipfs" ):
149
150
if gateway_path .exists ():
150
151
with open (gateway_path ) as gw_file :
151
152
ipfs_gateway = gw_file .readline ().strip ()
152
153
logger .debug ("using IPFS gateway from %s: %s" , gateway_path , ipfs_gateway )
153
- return AsyncIPFSGateway (ipfs_gateway )
154
+ return AsyncIPFSGateway (ipfs_gateway , protocol = protocol )
154
155
return None
155
156
156
157
157
158
@lru_cache
158
- def get_gateway ():
159
+ def get_gateway (protocol = "ipfs" ):
159
160
"""
160
161
Get IPFS gateway according to IPIP-280
161
162
@@ -166,29 +167,29 @@ def get_gateway():
166
167
ipfs_gateway = os .environ .get ("IPFS_GATEWAY" , "" )
167
168
if ipfs_gateway :
168
169
logger .debug ("using IPFS gateway from IPFS_GATEWAY environment variable: %s" , ipfs_gateway )
169
- return AsyncIPFSGateway (ipfs_gateway )
170
+ return AsyncGateway (ipfs_gateway , protocol )
170
171
171
172
# internal configuration: accept IPFSSPEC_GATEWAYS for backwards compatibility
172
173
if ipfsspec_gateways := os .environ .get ("IPFSSPEC_GATEWAYS" , "" ):
173
174
ipfs_gateway = ipfsspec_gateways .split ()[0 ]
174
175
logger .debug ("using IPFS gateway from IPFSSPEC_GATEWAYS environment variable: %s" , ipfs_gateway )
175
176
warnings .warn ("The IPFSSPEC_GATEWAYS environment variable is deprecated, please configure your IPFS Gateway according to IPIP-280, e.g. by using the IPFS_GATEWAY environment variable or using the ~/.ipfs/gateway file." , DeprecationWarning )
176
- return AsyncIPFSGateway (ipfs_gateway )
177
+ return AsyncGateway (ipfs_gateway , protocol )
177
178
178
179
# check various well-known files for possible gateway configurations
179
180
if ipfs_path := os .environ .get ("IPFS_PATH" , "" ):
180
- if ipfs_gateway := gateway_from_file (Path (ipfs_path ) / "gateway" ):
181
+ if ipfs_gateway := gateway_from_file (Path (ipfs_path ) / "gateway" , protocol ):
181
182
return ipfs_gateway
182
183
183
184
if home := os .environ .get ("HOME" , "" ):
184
- if ipfs_gateway := gateway_from_file (Path (home ) / ".ipfs" / "gateway" ):
185
+ if ipfs_gateway := gateway_from_file (Path (home ) / ".ipfs" / "gateway" , protocol ):
185
186
return ipfs_gateway
186
187
187
188
if config_home := os .environ .get ("XDG_CONFIG_HOME" , "" ):
188
- if ipfs_gateway := gateway_from_file (Path (config_home ) / "ipfs" / "gateway" ):
189
+ if ipfs_gateway := gateway_from_file (Path (config_home ) / "ipfs" / "gateway" , protocol ):
189
190
return ipfs_gateway
190
191
191
- if ipfs_gateway := gateway_from_file (Path ("/etc" ) / "ipfs" / "gateway" ):
192
+ if ipfs_gateway := gateway_from_file (Path ("/etc" ) / "ipfs" / "gateway" , protocol ):
192
193
return ipfs_gateway
193
194
194
195
system = platform .system ()
@@ -213,7 +214,7 @@ def get_gateway():
213
214
candidates = []
214
215
215
216
for candidate in candidates :
216
- if ipfs_gateway := gateway_from_file (candidate ):
217
+ if ipfs_gateway := gateway_from_file (candidate , protocol ):
217
218
return ipfs_gateway
218
219
219
220
# if we reach this point, no gateway is configured
@@ -249,7 +250,7 @@ def __init__(self, asynchronous=False, loop=None, client_kwargs=None, **storage_
249
250
250
251
@property
251
252
def gateway (self ):
252
- return get_gateway ()
253
+ return get_gateway (self . protocol )
253
254
254
255
@staticmethod
255
256
def close_session (loop , session ):
@@ -300,3 +301,7 @@ def open(self, path, mode="rb", block_size=None, cache_options=None, **kwargs):
300
301
def ukey (self , path ):
301
302
"""returns the CID, which is by definition an unchanging identitifer"""
302
303
return self .info (path )["CID" ]
304
+
305
+
306
+ class AsyncIPNSFileSystem (AsyncIPFSFileSystem ):
307
+ protocol = "ipns"
0 commit comments