| 1 | from typing import Any |
| 2 | from typing import Dict |
| 3 | from typing import Optional |
| 4 | |
| 5 | from loguru import logger |
| 6 | from sqlalchemy.ext.asyncio import AsyncSession |
| 7 | from sqlalchemy.future import select |
| 8 | |
| 9 | from app.connectors.models import Connectors |
| 10 | from app.connectors.schema import ConnectorResponse |
| 11 | |
| 12 | |
| 13 | # ! New with Async |
| 14 | async def get_connector_info_from_db( |
| 15 | connector_name: str, |
| 16 | db: AsyncSession, |
| 17 | ) -> Optional[Dict[str, Any]]: |
| 18 | """ |
| 19 | Fetches connector information from the database based on the given connector name. |
| 20 | |
| 21 | Args: |
| 22 | connector_name (str): The name of the connector to fetch. |
| 23 | db (AsyncSession): The database session. |
| 24 | |
| 25 | Returns: |
| 26 | Optional[Dict[str, Any]]: A dictionary containing the connector information if found, |
| 27 | otherwise None. |
| 28 | """ |
| 29 | logger.info(f"Fetching connector {connector_name} from database") |
| 30 | query = select(Connectors).where(Connectors.connector_name == connector_name) |
| 31 | result = await db.execute(query) |
| 32 | connector = result.scalars().first() |
| 33 | if connector: |
| 34 | connector_pydantic = ConnectorResponse.from_orm(connector) |
| 35 | return connector_pydantic.model_dump() |
| 36 | else: |
| 37 | logger.warning("No connector found.") |
| 38 | return None |
| 39 | |
| 40 | |
| 41 | async def is_connector_verified(connector_name: str, db: AsyncSession) -> bool: |
| 42 | """ |
| 43 | Checks if a connector is verified. |
| 44 | |
| 45 | Args: |
| 46 | connector_name (str): The name of the connector to check. |
| 47 | db (AsyncSession): The database session. |
| 48 | |
| 49 | Returns: |
| 50 | bool: True if the connector is verified, otherwise False. |
| 51 | """ |
| 52 | logger.info(f"Checking if connector {connector_name} is verified") |
| 53 | query = select(Connectors).where(Connectors.connector_name == connector_name) |
| 54 | result = await db.execute(query) |
| 55 | connector = result.scalars().first() |
| 56 | if connector: |
| 57 | return connector.connector_verified |
| 58 | else: |
| 59 | logger.warning("No connector found.") |
| 60 | return False |