Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom adapters to async gen #87

Merged
merged 5 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions offchain/metadata/fetchers/base_fetcher.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Protocol, Union
from typing import Optional, Protocol, Union

from offchain.metadata.adapters.base_adapter import Adapter
from offchain.metadata.adapters.base_adapter import Adapter, AdapterConfig


class BaseFetcher(Protocol):
Expand All @@ -13,8 +13,9 @@ class BaseFetcher(Protocol):

timeout: int
max_retries: int
async_adapter_configs: Optional[list[AdapterConfig]] = None

def __init__(self, timeout: int, max_retries: int) -> None:
def __init__(self, timeout: int, max_retries: int, async_adapter_configs: Optional[list[AdapterConfig]] = None) -> None:
pass

def set_timeout(self, new_timeout: int):
Expand Down
12 changes: 9 additions & 3 deletions offchain/metadata/fetchers/metadata_fetcher.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import cgi
from typing import Tuple, Union
from typing import Optional, Tuple, Union

import httpx
import requests

from offchain.logger.logging import logger
from offchain.metadata.adapters.base_adapter import Adapter
from offchain.metadata.adapters.base_adapter import Adapter, AdapterConfig
from offchain.metadata.fetchers.base_fetcher import BaseFetcher
from offchain.metadata.registries.fetcher_registry import FetcherRegistry

Expand All @@ -24,11 +24,13 @@ def __init__(
self,
timeout: int = 30,
max_retries: int = 0,
async_adapter_configs: Optional[list[AdapterConfig]] = None,
) -> None:
self.timeout = timeout
self.max_retries = max_retries
self.sess = requests.Session()
self.async_sess = httpx.AsyncClient()
self.async_adapter_configs = async_adapter_configs

def register_adapter(self, adapter: Adapter, url_prefix: str):
"""Register an adapter to a url prefix.
Expand Down Expand Up @@ -65,8 +67,12 @@ async def _gen(self, uri: str) -> httpx.Response:
from offchain.metadata.pipelines.metadata_pipeline import (
DEFAULT_ADAPTER_CONFIGS,
)
configs = DEFAULT_ADAPTER_CONFIGS

for adapter_config in 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(**adapter_config.kwargs)
return await adapter.gen_send(url=uri, timeout=self.timeout, sess=self.async_sess)
Expand Down
2 changes: 1 addition & 1 deletion offchain/metadata/pipelines/metadata_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def __init__(
adapter_configs: Optional[list[AdapterConfig]] = None,
) -> None:
self.contract_caller = contract_caller or ContractCaller()
self.fetcher = fetcher or MetadataFetcher()
self.fetcher = fetcher or MetadataFetcher(async_adapter_configs=adapter_configs)
if adapter_configs is None:
adapter_configs = DEFAULT_ADAPTER_CONFIGS
for adapter_config in adapter_configs:
Expand Down
Loading