55import requests
66
77from offchain .logger .logging import logger
8- from offchain .metadata .adapters . base_adapter import Adapter , AdapterConfig
8+ from offchain .metadata .adapters import Adapter , AdapterConfig , DEFAULT_ADAPTER_CONFIGS
99from offchain .metadata .fetchers .base_fetcher import BaseFetcher
1010from offchain .metadata .registries .fetcher_registry import FetcherRegistry
1111
@@ -24,7 +24,7 @@ def __init__(
2424 self ,
2525 timeout : int = 30 ,
2626 max_retries : int = 0 ,
27- async_adapter_configs : Optional [list [AdapterConfig ]] = None ,
27+ async_adapter_configs : Optional [list [AdapterConfig ]] = DEFAULT_ADAPTER_CONFIGS ,
2828 ) -> None :
2929 self .timeout = timeout
3030 self .max_retries = max_retries
@@ -33,7 +33,8 @@ def __init__(
3333 self .async_adapter_configs = async_adapter_configs
3434
3535 def register_adapter (self , adapter : Adapter , url_prefix : str ): # type: ignore[no-untyped-def] # noqa: E501
36- """Register an adapter to a url prefix.
36+ """Register an adapter to a url prefix. Note this only affects synchronous http
37+ requests (via the requests library).
3738
3839 Args:
3940 adapter (Adapter): an Adapter instance to register.
@@ -57,35 +58,44 @@ def set_timeout(self, timeout: int): # type: ignore[no-untyped-def]
5758 """
5859 self .timeout = timeout
5960
61+ def _get_async_adapter_for_uri (self , uri : str ) -> Optional [Adapter ]:
62+ if self .async_adapter_configs is None :
63+ logger .error ("Async adapter config doesn't exist. This shouldn't happen!" )
64+ return None
65+
66+ for async_adapter_config in self .async_adapter_configs :
67+ if any (
68+ uri .startswith (prefix ) for prefix in async_adapter_config .mount_prefixes
69+ ):
70+ logger .debug (
71+ f"Selected { async_adapter_config .adapter_cls .__name__ } for making async http requests for uri={ uri } " # noqa: E501
72+ )
73+ return async_adapter_config .adapter_cls (
74+ host_prefixes = async_adapter_config .host_prefixes ,
75+ ** async_adapter_config .kwargs ,
76+ )
77+ logger .warning (
78+ f"Unable to selected an adapter for async http requests for uri={ uri } "
79+ )
80+ return None
81+
6082 def _head (self , uri : str ): # type: ignore[no-untyped-def]
6183 return self .sess .head (uri , timeout = self .timeout , allow_redirects = True )
6284
6385 def _get (self , uri : str ): # type: ignore[no-untyped-def]
6486 return self .sess .get (uri , timeout = self .timeout , allow_redirects = True )
6587
6688 async def _gen (self , uri : str , method : Optional [str ] = "GET" ) -> httpx .Response :
67- from offchain .metadata .pipelines .metadata_pipeline import (
68- DEFAULT_ADAPTER_CONFIGS ,
69- )
70-
71- configs = DEFAULT_ADAPTER_CONFIGS
72-
73- if self .async_adapter_configs :
74- configs = self .async_adapter_configs
75-
76- for adapter_config in configs :
77- if any (uri .startswith (prefix ) for prefix in adapter_config .mount_prefixes ):
78- adapter = adapter_config .adapter_cls (
79- host_prefixes = adapter_config .host_prefixes , ** adapter_config .kwargs
89+ async_adapter = self ._get_async_adapter_for_uri (uri )
90+ if async_adapter is not None :
91+ if method == "HEAD" :
92+ return await async_adapter .gen_head (
93+ url = uri , timeout = self .timeout , sess = self .async_sess
94+ )
95+ else :
96+ return await async_adapter .gen_send (
97+ url = uri , timeout = self .timeout , sess = self .async_sess
8098 )
81- if method == "HEAD" :
82- return await adapter .gen_head (
83- url = uri , timeout = self .timeout , sess = self .async_sess
84- )
85- else :
86- return await adapter .gen_send (
87- url = uri , timeout = self .timeout , sess = self .async_sess
88- )
8999 return await self .async_sess .get (
90100 uri , timeout = self .timeout , follow_redirects = True
91101 )
0 commit comments