kopia lustrzana https://github.com/bugout-dev/moonstream
Add name_normalization function.
rodzic
eac4f812ee
commit
94d30be625
|
|
@ -19,6 +19,7 @@ from bugout.exceptions import BugoutResponseException
|
||||||
from ens.utils import is_valid_ens_name # type: ignore
|
from ens.utils import is_valid_ens_name # type: ignore
|
||||||
from eth_utils.address import is_address # type: ignore
|
from eth_utils.address import is_address # type: ignore
|
||||||
from moonstreamdb.models import EthereumLabel
|
from moonstreamdb.models import EthereumLabel
|
||||||
|
from slugify import slugify # type: ignore
|
||||||
from sqlalchemy import text
|
from sqlalchemy import text
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
from web3 import Web3
|
from web3 import Web3
|
||||||
|
|
@ -531,8 +532,25 @@ def apply_moonworm_tasks(subscription_type: str, abi: Any, address: str,) -> Non
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def name_normalization(query_name: str) -> str:
|
||||||
|
"""
|
||||||
|
return correct url name
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
normalized_query_name = slugify(
|
||||||
|
query_name, max_length=50, regex_pattern=r"[A-Za-z0-9_-]"
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error in query normalization.")
|
||||||
|
raise MoonstreamHTTPException(status_code=500, internal_error=e)
|
||||||
|
|
||||||
|
return normalized_query_name
|
||||||
|
|
||||||
|
|
||||||
def get_query_by_name(query_name: str, token: uuid.UUID) -> str:
|
def get_query_by_name(query_name: str, token: uuid.UUID) -> str:
|
||||||
|
|
||||||
|
query_name = name_normalization(query_name)
|
||||||
|
|
||||||
params = {"type": data.BUGOUT_RESOURCE_QUERY_RESOLVER, "name": query_name}
|
params = {"type": data.BUGOUT_RESOURCE_QUERY_RESOLVER, "name": query_name}
|
||||||
try:
|
try:
|
||||||
resources: BugoutResources = bc.list_resources(token=token, params=params)
|
resources: BugoutResources = bc.list_resources(token=token, params=params)
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ from slugify import slugify # type: ignore
|
||||||
|
|
||||||
|
|
||||||
from .. import data
|
from .. import data
|
||||||
from ..actions import get_query_by_name
|
from ..actions import get_query_by_name, name_normalization
|
||||||
from ..middleware import MoonstreamHTTPException
|
from ..middleware import MoonstreamHTTPException
|
||||||
from ..settings import (
|
from ..settings import (
|
||||||
MOONSTREAM_ADMIN_ACCESS_TOKEN,
|
MOONSTREAM_ADMIN_ACCESS_TOKEN,
|
||||||
|
|
@ -96,11 +96,7 @@ async def create_query_handler(
|
||||||
resource.resource_data["name"] for resource in resources.resources
|
resource.resource_data["name"] for resource in resources.resources
|
||||||
]
|
]
|
||||||
|
|
||||||
try:
|
query_name = name_normalization(query_applied.name)
|
||||||
query_name = slugify(query_applied.name)
|
|
||||||
except ResourceQueryFetchException as e:
|
|
||||||
logger.error(f"Error in query normalization.")
|
|
||||||
raise MoonstreamHTTPException(status_code=500, internal_error=e)
|
|
||||||
|
|
||||||
if query_name in used_queries:
|
if query_name in used_queries:
|
||||||
|
|
||||||
|
|
|
||||||
Ładowanie…
Reference in New Issue