Skip to content

Commit

Permalink
BACK-1791: ensure prop IPFS gateway gets used instead of pinata
Browse files Browse the repository at this point in the history
  • Loading branch information
zylora committed Nov 9, 2023
1 parent 199fec0 commit 09a120d
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 131 deletions.
3 changes: 2 additions & 1 deletion offchain/metadata/fetchers/base_fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def set_max_retries(self, new_max_retries: int): # type: ignore[no-untyped-def]
pass

def register_adapter(self, adapter: Adapter, url_prefix: str): # type: ignore[no-untyped-def] # noqa: E501
"""Register an adapter to a url prefix.
"""Register an adapter to a url prefix. Note this only affects synchronous http
requests (via the requests library).
Args:
adapter (Adapter): an Adapter instance to register.
Expand Down
62 changes: 39 additions & 23 deletions offchain/metadata/fetchers/metadata_fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,23 @@ def __init__(
max_retries: int = 0,
async_adapter_configs: Optional[list[AdapterConfig]] = None,
) -> None:
from offchain.metadata.pipelines.metadata_pipeline import (
DEFAULT_ADAPTER_CONFIGS,
)

self.timeout = timeout
self.max_retries = max_retries
self.sess = requests.Session()
self.async_sess = httpx.AsyncClient()
self.async_adapter_configs = async_adapter_configs
self.async_adapter_configs = (
DEFAULT_ADAPTER_CONFIGS
if async_adapter_configs is None
else async_adapter_configs
)

def register_adapter(self, adapter: Adapter, url_prefix: str): # type: ignore[no-untyped-def] # noqa: E501
"""Register an adapter to a url prefix.
"""Register an adapter to a url prefix. Note this only affects synchronous http
requests (via the requests library).
Args:
adapter (Adapter): an Adapter instance to register.
Expand All @@ -57,35 +66,42 @@ def set_timeout(self, timeout: int): # type: ignore[no-untyped-def]
"""
self.timeout = timeout

def _get_async_adapter_for_uri(self, uri: str) -> Optional[Adapter]:
if self.async_adapter_configs is not None:
for async_adapter_config in self.async_adapter_configs:
if any(
uri.startswith(prefix)
for prefix in async_adapter_config.mount_prefixes
):
logger.debug(
f"Selected {async_adapter_config.adapter_cls.__name__} for making async http requests for uri={uri}" # noqa: E501
)
return async_adapter_config.adapter_cls(
host_prefixes=async_adapter_config.host_prefixes,
**async_adapter_config.kwargs,
)
logger.warning(
f"Unable to selected an adapter for async http requests for uri={uri}"
)
return None

def _head(self, uri: str): # type: ignore[no-untyped-def]
return self.sess.head(uri, timeout=self.timeout, allow_redirects=True)

def _get(self, uri: str): # type: ignore[no-untyped-def]
return self.sess.get(uri, timeout=self.timeout, allow_redirects=True)

async def _gen(self, uri: str, method: Optional[str] = "GET") -> httpx.Response:
from offchain.metadata.pipelines.metadata_pipeline import (
DEFAULT_ADAPTER_CONFIGS,
)

configs = DEFAULT_ADAPTER_CONFIGS

if self.async_adapter_configs:
configs = self.async_adapter_configs

for adapter_config in configs:
if any(uri.startswith(prefix) for prefix in adapter_config.mount_prefixes):
adapter = adapter_config.adapter_cls(
host_prefixes=adapter_config.host_prefixes, **adapter_config.kwargs
async_adapter = self._get_async_adapter_for_uri(uri)
if async_adapter is not None:
if method == "HEAD":
return await async_adapter.gen_head(
url=uri, timeout=self.timeout, sess=self.async_sess
)
else:
return await async_adapter.gen_send(
url=uri, timeout=self.timeout, sess=self.async_sess
)
if method == "HEAD":
return await adapter.gen_head(
url=uri, timeout=self.timeout, sess=self.async_sess
)
else:
return await adapter.gen_send(
url=uri, timeout=self.timeout, sess=self.async_sess
)
return await self.async_sess.get(
uri, timeout=self.timeout, follow_redirects=True
)
Expand Down
12 changes: 6 additions & 6 deletions offchain/metadata/pipelines/metadata_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@
kwargs={"pool_connections": 100, "pool_maxsize": 1000, "max_retries": 0},
),
AdapterConfig(adapter_cls=DataURIAdapter, mount_prefixes=["data:"]),
AdapterConfig(
adapter_cls=HTTPAdapter,
mount_prefixes=["https://", "http://"],
kwargs={"pool_connections": 100, "pool_maxsize": 1000, "max_retries": 0},
),
AdapterConfig(
adapter_cls=IPFSAdapter,
mount_prefixes=[
Expand All @@ -46,6 +41,11 @@
host_prefixes=["https://gateway.pinata.cloud/ipfs/"],
kwargs={"pool_connections": 100, "pool_maxsize": 1000, "max_retries": 0},
),
AdapterConfig(
adapter_cls=HTTPAdapter,
mount_prefixes=["https://", "http://"],
kwargs={"pool_connections": 100, "pool_maxsize": 1000, "max_retries": 0},
),
]

DEFAULT_PARSERS = (
Expand All @@ -66,7 +66,7 @@ class MetadataPipeline(BasePipeline):
mime type, and size by making network requests.
parsers (list[BaseParser], optional): a list of parser instances for parsing token metadata.
adapter_configs: (list[AdapterConfig], optional): a list of adapter configs used to register adapters
to specified url prefixes.
to specified url prefixes. This configuration affects both sync and async requests.
""" # noqa: E501

def __init__(
Expand Down
2 changes: 1 addition & 1 deletion offchain/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async def wrapped(*args, **kwargs): # type: ignore[no-untyped-def]
logger.error(msg)
if not silent:
raise
logger.warn(msg)
logger.warning(msg)
await asyncio.sleep(retry_delay)
return None

Expand Down
Loading

0 comments on commit 09a120d

Please sign in to comment.